使用jib打包docker镜像实战
序
本文主要研究一下如何使用google的jib打包docker镜像
jib 是谷歌最新开源的 java 应用的 docker 镜像生成工具,可以通过 gradle 或 maven 直接生成镜像并上传到仓库而不需要 dockerfile 文件或者其他插件;jib 支持将资源文件和类分层打包,可以大幅度提升生成镜像的速度
有一些其他的插件也可以通过 docker 实现生成镜像,如com.palantir.docker等,但是都需要额外配置 dockerfile, 如果应用仅需要通过 dockerfile 构建镜像,建议使用 jib 来提升构建和上传速度
maven
<plugin> <groupid>com.google.cloud.tools</groupid> <artifactid>jib-maven-plugin</artifactid> <version>0.9.9</version> </plugin>
配置
<properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <maven.build.timestamp.format>yyyymmdd</maven.build.timestamp.format> </properties> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <plugin> <groupid>com.google.cloud.tools</groupid> <artifactid>jib-maven-plugin</artifactid> <version>0.9.9</version> <configuration> <container> <jvmflags> <jvmflag>-xms512m</jvmflag> </jvmflags> <ports> <port>8080</port> </ports> <usecurrenttimestamp>true</usecurrenttimestamp> </container> <from> <image>java:8u172-jre-alpine</image> </from> <to> <image>jib-demo:${maven.build.timestamp}</image> </to> <allowinsecureregistries>true</allowinsecureregistries> <extradirectory>${project.basedir}/src/main/jib</extradirectory> </configuration> </plugin> </plugins> </build>
这里指定base image为java:8u172-jre-alpine
同时指定生成的image名称为jib-demo:${maven.build.timestamp}
container标签可以指定相关参数,比如jvmflags指定jvm参数,ports指定expose的端口号
extradirectory用于指定要添加的目录,默认为${project.basedir}/src/main/jib,即该目录下的文件夹会在docker镜像里头同步创建,目录层级关系以jib下目录层级关系为准。比如该目录下有app/resources/demo.txt,则对应该镜像目录为/app/resources/demo.txt
打包
mvn compile jib:dockerbuild -x
使用jib:dockerbuild是在本地打包镜像,不push到远程,-x参数是显示debug信息
如果使用jib:build命令,则打包之后push到远程
输出日志实例:
[debug] timing building image to docker daemon [debug] running building image to docker daemon [info] getting base image java:8u172-jre-alpine... [debug] timing pulling base image manifest [debug] running pulling base image manifest [debug] timing building application layers [debug] running building application layers [info] building dependencies layer... [debug] timing building dependencies layer [debug] running building dependencies layer [info] building resources layer... [debug] timing building resources layer [debug] running building resources layer [info] building classes layer... [debug] timing building classes layer [debug] running building classes layer [info] building extra files layer... [debug] timing building extra files layer [debug] running building extra files layer [debug] timed building application layers : 3.498 ms [debug] building application layers : 3.498 ms [debug] building extra files layer built sha256:0a9321c621ee2c138d9a93f4ba47f825e20dd7aca0e9206f30bf560122fdaa31 [debug] building resources layer built sha256:933116c8da4db70000355b750af2f3b5c4545e74db7b5eb0a5e37689ef86d44f [debug] building classes layer built sha256:a99dbde7d015ba3d15b9265ac38f6536d65b00dd2a864b7003a34742f483939b [debug] timed building extra files layer : 33.718 ms [debug] timed building classes layer : 33.901 ms [debug] building extra files layer : 33.718 ms [debug] timed building resources layer : 34.062 ms [debug] building resources layer : 34.062 ms [debug] building classes layer : 33.901 ms [debug] building dependencies layer built sha256:a19961ec08095eb70ca3a070945223dcf51500a255a745272276139bf0a29f4e [debug] timed building dependencies layer : 1047.83 ms [debug] building dependencies layer : 1047.83 ms [info] the base image requires auth. trying again for java:8u172-jre-alpine... //...... [debug] timed pulling base image manifest : 8959.891 ms [debug] pulling base image manifest : 8959.891 ms [debug] timing setting up base image caching [debug] running setting up base image caching [debug] timed setting up base image caching : 0.824 ms [debug] setting up base image caching : 0.824 ms [debug] timing pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827 [debug] running pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827 [debug] timing pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca [debug] timing pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6 [debug] running pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6 [debug] timing pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28 [debug] running pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28 [debug] running pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca [debug] timed pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827 : 612.524 ms [debug] pulling base image layer sha256:500c0ac4cdc58aee9a008e70466eeec8a4b74b25c29d003764635d2fe1b42827 : 612.524 ms [debug] timed pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28 : 9985.702 ms [debug] pulling base image layer sha256:ff3a5c916c92643ff77519ffa742d3ec61b7f591b6b7504599d95a4a41134e28 : 9985.702 ms [debug] timed pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca : 27267.144 ms [debug] pulling base image layer sha256:aa38cd8c77d7320c7489327775e074580db5f23467198aab3e79c7db5cdfbdca : 27267.144 ms [debug] timed pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6 : 54734.971 ms [debug] pulling base image layer sha256:cb56d62302ec31640b40ca15843e7af6be145966a1064c78ea7afd6f10ce49e6 : 54734.971 ms [debug] timing building container configuration [debug] running building container configuration [info] finalizing... [debug] timed building container configuration : 0.462 ms [debug] building container configuration : 0.462 ms [info] loading to docker daemon... [debug] timed building image to docker daemon : 65862.956 ms [debug] building image to docker daemon : 65862.956 ms [info] [info] container entrypoint set to [java, -xms512m, -cp, /app/resources/:/app/classes/:/app/libs/*, com.example.jibdemoapplication] [info] [info] built image to docker daemon as jib-demo:20180826 [info] [info] ------------------------------------------------------------------------ [info] build success [info] ------------------------------------------------------------------------ [info] total time: 01:07 min [info] finished at: 2018-08-26t21:38:11+08:00 [info] final memory: 26m/493m [info] ------------------------------------------------------------------------
查看镜像
docker inspect jib-demo:20180826 [ { "id": "sha256:7554492dde84091bc6d015b29ec4bf854bb841b9ec4d8f7bef8b51a8ec59ef94", "repotags": [ "jib-demo:20180826" ], "repodigests": [], "parent": "", "comment": "", "created": "2018-08-26t13:37:05.65z", "container": "", "containerconfig": { "hostname": "", "domainname": "", "user": "", "attachstdin": false, "attachstdout": false, "attachstderr": false, "tty": false, "openstdin": false, "stdinonce": false, "env": null, "cmd": null, "image": "", "volumes": null, "workingdir": "", "entrypoint": null, "onbuild": null, "labels": null }, "dockerversion": "", "author": "", "config": { "hostname": "", "domainname": "", "user": "", "attachstdin": false, "attachstdout": false, "attachstderr": false, "exposedports": { "8080/tcp": {} }, "tty": false, "openstdin": false, "stdinonce": false, "env": [ "path=/opt/jdk1.8.0_172/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "java_home=/opt/jdk1.8.0_172" ], "cmd": [], "image": "", "volumes": null, "workingdir": "", "entrypoint": [ "java", "-xms512m", "-cp", "/app/resources/:/app/classes/:/app/libs/*", "com.example.jibdemoapplication" ], "onbuild": null, "labels": {} }, "architecture": "amd64", "os": "linux", "size": 186079594, "virtualsize": 186079594, "graphdriver": { "data": null, "name": "aufs" }, "rootfs": { "type": "layers", "layers": [ "sha256:cd7100a72410606589a54b932cabd804a17f9ae5b42a1882bd56d263e02b6215", "sha256:317e0b42ef29551507adc792c2dbc389af85c5d68900cf0f08abe907cbd4e199", "sha256:d6fd5302a9ee64c43b46153e9678153819d6d0e247381acbbe6cbe6ed9de0366", "sha256:417c6bd0930eb69d17ae5d601898a6a012900ca33c69e612840a3042b2451cbb", "sha256:8309a5eed5158c5639acb664312ddc7027a2ecb172744f548d6d9fb5f918a744", "sha256:6ef8c795d7a5e2a07f51d572a90b820e089f89adf30798193f4f3f410a261415", "sha256:9990447f3d37e3f2dc70718d0ea6602cf7843091d8bbe395f552c9c5dd10551c", "sha256:ff7910aaeee962d1d86e00742030378a2758da59d8c8cda3fe44314bbddd8081" ] }, "metadata": { "lasttagtime": "0001-01-01t00:00:00z" } } ]
可以看到java -cp指定了/app/libs/、/app/resources/、/app/classes/这三个文件夹
小结
jib默认执行如下操作
copy libs /app/libs/ copy resources /app/resources/ copy classes /app/classes/
也就是将libs拷贝到/app/libs,将resources拷贝到/app/resources,将classes拷贝到/app/classes.
doc
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: ASP系列讲座(二)ASP 的新功能
下一篇: 你怎么知道的