ããã¯ããªã«ããããã¦æ¸ãããã®ï¼
以åãJib Coreã使ã£ã¦Dockerã¤ã¡ã¼ã¸ãä½ã£ã¦ã¿ã¾ããã
Jib Coreで、Dockerコンテナイメージを作ってみる - CLOVER🍀
ããã§Jibã®åºæ¬çãªã¨ããã¯ããã£ãâ¦ãã¨ã«ãã¦ãä»åº¦ã¯Jib Maven Pluginã使ã£ã¦Dockerã¤ã¡ã¼ã¸ãä½ã£ã¦ã¿ããã¨æãã¾ãã
Jib Maven Plugin
æåéããJibã®æ©è½ã使ããMaven Pluginã§ãDockerã¾ãã¯OCIã¤ã¡ã¼ã¸ãä½ããã¨ãã§ãã¾ãã
https://github.com/GoogleContainerTools/jib/tree/master/jib-maven-plugin
使ããã¤ã¡ã¼ã¸ã¯ãã³ã³ããã¬ã¸ã¹ããªã«ã¢ãããã¼ãããããDockerãã¼ã¢ã³ã«éã£ãããtarãã¡ã¤ã«ã«ãããã§ãã¾ãã
ããã§ã¯ã使ã£ã¦ãã£ã¦ã¿ã¾ãããã
ç°å¢
ä»åã®ç°å¢ã¯ããã¡ãã§ãã
$ java --version openjdk 11.0.6 2020-01-14 OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1) OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing) $ mvn --version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: $HOME/.sdkman/candidates/maven/current Java version: 11.0.6, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64 Default locale: ja_JP, platform encoding: UTF-8 OS name: "linux", version: "4.18.0-25-generic", arch: "amd64", family: "unix"
ã¾ããJib Maven Pluginã¯2.1.0ã使ç¨ãã¾ãã
ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³
å ã«ããé¡ã¨ãªããµã³ãã«ã¢ããªã±ã¼ã·ã§ã³ãå¿ è¦ã§ããã
ãã¡ãã§ä½ã£ããã®ãæµç¨ãã¾ãã
Jib Coreで、Dockerコンテナイメージを作ってみる - CLOVER🍀
pomãRESTEasyã¯ãææ°çã«ãã¦ããã¾ããã
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.littlewings</groupId> <artifactId>jib-simple-jaxrs-server</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jdk-http</artifactId> <version>4.5.3.Final</version> </dependency> </dependencies> </project>
èµ·åã¯ã©ã¹ï¼JAX-RSãªã½ã¼ã¹ã¯ã©ã¹ã
src/main/java/org/littlewings/jaxrs/Server.java
package org.littlewings.jaxrs; import java.io.IOException; import java.net.InetSocketAddress; import java.util.Optional; import java.util.concurrent.TimeUnit; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import com.sun.net.httpserver.HttpServer; import org.jboss.logging.Logger; import org.jboss.resteasy.plugins.server.sun.http.HttpContextBuilder; public class Server { public static void main(String... args) throws IOException { Logger logger = Logger.getLogger(Server.class); HttpServer server = HttpServer.create(new InetSocketAddress(8080), 10); try { HttpContextBuilder builder = new HttpContextBuilder(); builder.getDeployment().getActualResourceClasses().add(HelloResource.class); builder.bind(server); server.start(); logger.info("server start."); while (true) { try { TimeUnit.SECONDS.sleep(1L); } catch (InterruptedException e) { // ignore } } } finally { server.stop(0); logger.info("server stop."); } } @Path("hello") public static class HelloResource { @GET @Produces(MediaType.TEXT_PLAIN) public String message(@QueryParam("value") String value) { return "Hello " + Optional.ofNullable(value).orElse("World") + "!!"; } } }
ãã¡ãããJibã使ã£ã¦Dockerã¤ã¡ã¼ã¸ã«ãã¦ããã¾ãããã
Jib Maven Pluginãçµã¿è¾¼ã
ã¾ãã¯ããªã«ãèããã«ãã©ã°ã¤ã³ãçµã¿è¾¼ãã§ã¿ã¾ãã
<build> <plugins> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <to> <image>kazuhira/jib-simple-jaxrs-server</image> </to> </configuration> </plugin> </plugins> </build>
ãjib:dockerBuildãã§Dockerã¤ã¡ã¼ã¸ã使ãã¾ãããã
$ mvn compile jib:dockerBuild
ã©ãããããgcr.io/distroless/java:11ãããã¼ã¹ã«ãã¦ã¤ã¡ã¼ã¸ãä½ã£ãã¿ããã§ãã
[INFO] Containerizing application to Docker daemon as kazuhira/jib-simple-jaxrs-server... [WARNING] Base image 'gcr.io/distroless/java:11' does not use a specific image digest - build may not be reproducible [INFO] Using base image with digest: sha256:c94feda039172152495b5cd60a350a03162fce4f8986b560ea555de4d276ce19 [INFO] [INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, org.littlewings.jaxrs.Server] [INFO] [INFO] Built image to Docker daemon as kazuhira/jib-simple-jaxrs-server [INFO] Executing tasks: [INFO] [==============================] 100.0% complete [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 8.328 s [INFO] Finished at: 2020-04-05T19:43:05+09:00 [INFO] ------------------------------------------------------------------------
確èªã50å¹´åã®ã¿ã¤ã ã¹ã¿ã³ãã®Dockerã¤ã¡ã¼ã¸ãã§ãã¾ããã
$ docker image ls | grep simple kazuhira/jib-simple-jaxrs-server latest cf8664e3692c 50 years ago 199MB
Dockerã¤ã¡ã¼ã¸ã®æ¹ã確èªãã¦ã¿ã¾ãããã
$ docker container run -it --rm -p 8080:8080 kazuhira/jib-simple-jaxrs-server:latest Apr 05, 2020 10:50:26 AM org.littlewings.jaxrs.Server main INFO: server start.
OKã§ããã
$ curl localhost:8080/hello Hello World!!
ã§ã¯ãå°ããã¤è¨å®ãå¤ãã¦ãã£ã¦ã¿ã¾ããããJib Coreã®æã«ç¿ãæããã¾ãã
ãã¨ãExampleãåèã«ããã¨ããã§ãããã
ã¿ã°ãæå®ãã
ããã¯ãåç´ã«ã¤ã¡ã¼ã¸åã®é£ã«ã:ãã§åºåã£ã¦ã¿ã°ãæå®ããã°OKã§ãã
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <to> <image>kazuhira/jib-simple-jaxrs-server:0.0.1</image> </to> </configuration> </plugin>
ãã«ãå¾ã
$ docker image ls | grep simple kazuhira/jib-simple-jaxrs-server 0.0.1 cf8664e3692c 50 years ago 199MB
ãã¼ã¹ã¤ã¡ã¼ã¸ã夿´ãã
æåã¯ããã©ã«ãã§ä½¿ããããã¼ã¹ã¤ã¡ã¼ã¸ï¼Distrolessï¼ã使ãã¾ãããããã¼ã¹ã¤ã¡ã¼ã¸ã夿´ãã¦ã¿ã¾ãããã
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <from> <image>adoptopenjdk:11-jre-hotspot</image> </from> <to> <image>kazuhira/jib-simple-jaxrs-server:0.0.1</image> </to> </configuration> </plugin>
fromãimageã§æå®ãã¾ããä»åã¯AdoptOpenJDKã«ãã¾ããã
ã¨ããã§ãREADME.mdãè¦ã¦ããã¨ããªã«ãæå®ããªãå ´åã®ãã¼ã¹ã¤ã¡ã¼ã¸ã¯ãgcr.io/distroless/javaãã使ããã¦ãã¨æ¸ããã¦
ããå²ã«ã¯æåã«ãã¦ã³ãã¼ããããã®ã¯ãgcr.io/distroless/java:11ãã§ããã
ã©ããªã£ã¦ãããã§ãããï¼
ã©ããããJavaã®ãã¼ã¸ã§ã³ãè¦ã¦ãã¼ã¹ã¤ã¡ã¼ã¸ã決ãã¦ããããã§ããã
Java VMã«æ¸¡ããªãã·ã§ã³ãè¨å®ãã
Jib Maven Pluginã使ã£ãå ´åã¯ãENTRYPOINTãç´æ¥æå®ãããã¨ã¯å°ãªãã¨æãã¾ãã
代ããã«ãjvmFlagãmainClassãªã©ã使ç¨ãã¾ãã
entrypointã¨ããè¨å®é ç®èªä½ã¯ããã®ã§ãããjvmFlagsãªã©ãè¨å®ãã¦ãç¡è¦ããããããªã
The command to start the container with (similar to Docker's ENTRYPOINT instruction). If set, then jvmFlags and mainClass are ignored. You may also set
INHERIT to indicate that the entrypoint and args should be inherited from the base image.*
ã§ã¯ãjvmFlagã使ã£ã¦ã¿ã¾ããããã-Xmxããæå®ãã¦ã¿ã¾ãã
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <from> <image>adoptopenjdk:11-jre-hotspot</image> </from> <to> <image>kazuhira/jib-simple-jaxrs-server:0.0.1</image> </to> <container> <jvmFlags> <jvmFlag>-Xmx256m</jvmFlag> </jvmFlags> </container> </configuration> </plugin>
æå®ããæã¯ãcontaineré ä¸ã«ããããã§ããã
èµ·åå¾ã«ã³ã³ããå ã«å ¥ã£ã¦ç¢ºèªãã¦ã¿ãã¨ãJava VMã¸ã®å¼æ°ã追å ããã¦ãããã¨ã確èªã§ãã¾ããã
$ docker container exec -it [ã³ã³ããå] bash root@5de3f4e084f2:/# ps -ef | grep java root 1 0 27 11:20 pts/0 00:00:01 java -Xmx256m -cp /app/resources:/app/classes:/app/libs/* org.littlewings.jaxrs.Server root 36 26 0 11:20 pts/1 00:00:00 grep --color=auto java
ãªããå®è¡æã«Java VMã¸ã®å¼æ°ãæå®ãããå ´åã¯ãJAVA_TOOL_OPTIONSç°å¢å¤æ°ã使ãããã§ãã
â»ãã¼ã¹ã¤ã¡ã¼ã¸ãdistroless/javaã®æã«éãã¨ã¯æ¸ãã¦ãã¾ããâ¦ããã¯Javaã®ããã¥ã¡ã³ãã«ãè¨è¼ã®ããç°å¢å¤æ°ã§ããâ¦
mainClassã«ã¤ãã¦ã¯èªåã§æ¤åºããããã§ãããèªåã§æå®ãããã¨ãå¯è½ã§ãã
ãã¨ãããã°ã©ã èªä½ã®èµ·å弿°ã«ã¤ãã¦ã¯argsã§æå®ãã¾ãã
ãã¼ããEXPOSEãã
ãã¼ããEXPOSEããã«ã¯ãportsãportã§æå®ãã¾ãã
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <from> <image>adoptopenjdk:11-jre-hotspot</image> </from> <to> <image>kazuhira/jib-simple-jaxrs-server:0.0.1</image> </to> <container> <jvmFlags> <jvmFlag>-Xmx256m</jvmFlag> </jvmFlags> <ports> <port>8080</port> </ports> </container> </configuration> </plugin>
確èªã
$ docker container ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd8aa0836e4d kazuhira/jib-simple-jaxrs-server:0.0.1 "java -Xmx256m -cp /â¦" 5 seconds ago Up 2 seconds 8080/tcp busy_carson
ã¤ã¡ã¼ã¸ã®ä½ææå»ã夿´ãã
使æå»ã50å¹´åã«ãªã£ã¦ããã®ã夿´ããã«ã¯ãcreationTimeã使ç¨ãã¾ãã
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <from> <image>adoptopenjdk:11-jre-hotspot</image> </from> <to> <image>kazuhira/jib-simple-jaxrs-server:0.0.1</image> </to> <container> <jvmFlags> <jvmFlag>-Xmx256m</jvmFlag> </jvmFlags> <creationTime>USE_CURRENT_TIMESTAMP</creationTime> <ports> <port>8080</port> </ports> </container> </configuration> </plugin>
ãUSE_CURRENT_TIMESTAMPãã使ç¨ããã¨ãç¾å¨æå»ã§ã¤ã¡ã¼ã¸ã使ããã¾ãã
$ docker image ls | grep simple kazuhira/jib-simple-jaxrs-server 0.0.1 36ac8b04db85 2 minutes ago 228MB
ãã ããç¾å¨æå»ã使ã£ã¦ãã¾ãã¨åç¾æ§ã失ããã¨ã«ãªãç¹ã«æ³¨æãã¦ãã ããã
Why is my image created 48+ years ago?
ã¾ããISO 8601ãã©ã¼ãããã§æå®ãããã¨ãã§ãã¾ãã
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <configuration> <from> <image>adoptopenjdk:11-jre-hotspot</image> </from> <to> <image>kazuhira/jib-simple-jaxrs-server:0.0.1</image> </to> <container> <jvmFlags> <jvmFlag>-Xmx256m</jvmFlag> </jvmFlags> <creationTime>2020-01-15T10:30:45+09:00</creationTime> <ports> <port>8080</port> </ports> </container> </configuration> </plugin>
å®ä½ã¨ãã¦ã¯ãDateTimeFormatter.ISO_DATE_TIMEã§ãã¼ã¹ã§ããå¿ è¦ãããã¾ãã
çµæã
$ docker image ls | grep simple kazuhira/jib-simple-jaxrs-server 0.0.1 bf6479b69280 2 months ago 228MB
ãã«ãããã»ã¹ã«çµã¿è¾¼ã
Mavenãã©ã°ã¤ã³ã¨ãã¦ä½¿ã£ã¦ããã®ã§ãããpackageæã«Dockerã¤ã¡ã¼ã¸ãä½ã£ã¦æ¬²ãããã®ã§ãã
以ä¸ãåèã«ãMavenã®ãã«ãããã»ã¹ã«çµã¿è¾¼ãã§ã¿ã¾ãããã
Profileãdockerãã«ãã¦ãé常ã®ããã±ã¼ã¸ã³ã°ã¨ã¯å¥ã«ãã¦ã¿ã¾ããã
<profiles> <profile> <id>docker</id> <build> <plugins> <plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>jib-maven-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>dockerBuild</goal> </goals> </execution> </executions> <configuration> <from> <image>adoptopenjdk:11-jre-hotspot</image> </from> <to> <image>kazuhira/jib-simple-jaxrs-server:0.0.1</image> </to> <container> <jvmFlags> <jvmFlag>-Xmx256m</jvmFlag> </jvmFlags> <creationTime>2020-01-15T10:30:45+09:00</creationTime> <ports> <port>8080</port> </ports> </container> </configuration> </plugin> </plugins> </build> </profile> </profiles>
ãpackageãã´ã¼ã«å®è¡æã«ããdockerBuildãããããã«ãã¦ããã¾ãã
<executions> <execution> <phase>package</phase> <goals> <goal>dockerBuild</goal> </goals> </execution> </executions>
ãã¨ã¯ãProfileãæå®ãã¦ãã«ããã¾ãããã
$ mvn -Pdocker package
ãããªã¨ããã§ããããã
ã¾ã¨ã
Jib Maven Pluginã使ã£ã¦ãDockerã¤ã¡ã¼ã¸ãä½ã£ã¦ã¿ã¾ããã
ãã®ä»ãWARã«å¯¾ãã¦é©ç¨ã§ããããã¾ãããè¨å®ãä»ã«ããããããã®ã§ãããæ £ããã°README.mdãè¦ã¤ã¤ä½¿ããããªæ°ããã¾ããã
ä»åã¯ããããªã¨ããã§ã