写点什么

Jib 使用小结 (Maven 插件版)(1),深入理解 java 虚拟机第四版 pdf 百度云

用户头像
极客good
关注
发布于: 刚刚

[](

)小结四:提升构建速度


在使用命令 mvn compile jib:dockerBuild 构建本地镜像时,每次构建的过程中都会提示以下信息:


[INFO] Containerizing application to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT...


[INFO] The base image requires auth. Trying again for openjdk:8-jdk-stretch...


[INFO] Executing tasks:


[INFO] [========= ] 30.0% complete


[INFO] > pulling base image manifest


每次构建都会显示上面的内容,也就是说每次都去远程拉取 base 镜像的 manifest(pulling base image manifest),这部分时间导致整体构架时间变长,以下是构建结果,可见用了 10 秒:


[INFO] Built image to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT


[INFO] Executing tasks:


[INFO] [==============================] 100.0% complete


[INFO]


[INFO] ------------------------------------------------------------------------


[INFO] BUILD SUCCESS


[INFO] ------------------------------------------------------------------------


[INFO] Total time: 10.722 s


[INFO] Finished at: 2019-09-01T08:55:09+08:00


[INFO] ------------------------------------------------------------------------


首先想到的是执行命令 docker pull openjdk:8-jdk-stretch 将 base 镜像下载到本地仓库,再尝试构建,遗憾的是 jib 依旧会远程获取 base 镜像的 manifest,还是很慢;


如果能避免远程拉取 base 镜像的 manifest,镜像构建速度应该会快一些;基于此推论,优化构建速度的步骤如下:


  1. 在本机创建 registry(docker 镜像仓库服务);

  2. 将 base 镜像 openjdk:8-jdk-stretch 放入本机 registry;

  3. 修改 pom.xml 中 base 镜像的配置,改为本机 registry 的镜像;

  4. 如此一来,每次都会从本机 registry 取得 base 镜像的 manifest,不走远程请求响应,构建时间会有提升;


接下按照上述步骤进行操作:


  1. 确认当前电脑的 IP 地址,我这里是 192.168.121.131;

  2. 设置本地 docker 服务支持 http:修改 docker 配置文件:/etc/docker/daemon.json,在 json 中增加内容"insecure-registries": [“192.168.121.131:5000”]

  3. 重启 docker 使配置生效:


systemctl restart docker


  1. 在本地创建一个镜像仓库服务:


docker run --name docker-registry -d -p 5000:5000 registry


  1. 查看本地镜像 openjdk:8-jdk-stretch 的 ID 为 08ded5f856cc;

  2. 用 tag 命令将本地镜像 openjdk:8-jdk-stretch 改名,命令如下所示,"192.168.121.131"是当前电脑的 IP 地址:


docker tag 08ded5f856cc 192.168.121.131:5000/openjdk:8-jdk-stretch


  1. 再次查看镜像,发现多了个 192.168.121.131:5000/openjdk:8u212-jdk-stretch:


[root@maven hellojib]# docker tag 08ded5f856cc 192.168.121.131:5000/openjdk:8-jdk-stretch


[root@maven hellojib]# docker images


REPOSITORY TAG IMAGE ID CREATED SIZE


bolingcavalry/hellojib 0.0.1-SNAPSHOT 6601ef5a767d 3 minutes ago 505 MB


192.168.121.131:5000/openjdk 8-jdk-stretch 08ded5f856cc 2 weeks ago 488 MB


docker.io/openjdk 8-jdk-stretch 08ded5f856cc 2 weeks ago 488 MB


  1. 将 192.168.121.131:5000/openjdk:8-jdk-stretch 推送到远程仓库,命令如下所示,由于镜像名前缀是 192.168.121.131:5000,镜像会被推送到我们刚刚创建的 registry:


docker push 192.168.121.131:5000/openjdk:8-jdk-stretch


  1. 修改 java 工程的 pom.xml,将 base 镜像由之前的 openjdk:8-jdk-stretch 改为 192.168.121.131:5000/openjdk:8-jdk-stretch

  2. 修改 java 工程的 pom.xml,增加 allowInsecureRegistries,使 jib 插件支持 http 协议连接镜像仓库(安全起见,默认是关闭的),整个插件的配置信息如下:


<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>1.3.0</version>


<configuration>


<from>


<image>192.168.121.131:5000/openjdk:8-jdk-stretch</image>


</from>


<to>


<image>bolingcavalry/hellojib:${project.version}</image>


</to>


<container>


<jvmFlags>


<jvmFlag>-Xms4g</jvmFlag>


<jvmFlag>-Xmx4g</jvmFlag>


</jvmFlags>


<ports>


<port>8080</port>


</ports>


<useCurrentTimestamp>true</useCurrentTimestamp>


</container>


<allowInsecureRegistries>true</allowInsecureRegistries>


</configuration>


</plugin>


</plugins>


  1. 再次执行命令 mvn compile jib:dockerBuild,如下所示,时间从之前的 10 秒


【一线大厂Java面试题解析+核心总结学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


缩减到 3.9 秒:


[INFO] Built image to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT


[INFO] Executing tasks:


[INFO] [==============================] 100.0% complete


[INFO]


[INFO] ------------------------------------------------------------------------


[INFO] BUILD SUCCESS


[INFO] ------------------------------------------------------------------------


[INFO] Total time: 3.949 s


[INFO] Finished at: 2019-09-01T10:51:50+08:00


[INFO] ------------------------------------------------------------------------


以上就是通过 registry 服务提升 jib 构建速度的方法,在多人开发的时候,registry 可以配置为一个公共的,多人都可使用,毕竟 pom.xml 代码存在公共代码仓库,每个人都去修改成自己的 registry 的地址是不合适的,一旦提交上去会影响其他人的使用,我们这里的做法是将 registry 的地址写成 host,本地维护好 host 和 IP 的映射就可以了。


使用本地 registry 服务,除了提速,在服务器无法连接中央仓库取得 openjdk:8-jdk-stretch 的 manifest 时,这种方式能保证构建依旧能够成功;

[](

)小结五:将 jib 与 mvn 构建的生命周期绑定


  1. 前面的实战中构建命令是 mvn compile jib:dockerBuild,实际上可以做到仅用 mvn compile 就完成镜像构建,这是 maven 插件的通用特性;

  2. 修改 pom.xml 增加 executions 节点,里面设置 compile 触发 jib:dockerBuild,整个插件的内容如下所示:


<plugin>


<groupId>com.google.cloud.tools</groupId>


<artifactId>jib-maven-plugin</artifactId>


<version>1.3.0</version>


<configuration>


<from>


<image>192.168.121.131:5000/openjdk:8-jdk-stretch</image>


</from>


<to>


<image>bolingcavalry/hellojib:${project.version}</image>


</to>


<container>


<jvmFlags>


<jvmFlag>-Xms4g</jvmFlag>


<jvmFlag>-Xmx4g</jvmFlag>


</jvmFlags>


<ports>


<port>8080</port>


</ports>


<useCurrentTimestamp>true</useCurrentTimestamp>


</container>


<allowInsecureRegistries>true</allowInsecureRegistries>


</configuration>


<executions>


<execution>


<phase>compile</phase>


<goals>


<goal>dockerBuild</goal>


</goals>


</execution>


</executions>


</plugin>


  1. 执行命令 mvn compile -DskipTests,如下所示,可以成功构建镜像,与前面的命令结果一致:


[root@maven hellojib]# mvn compile -DskipTests


[INFO] Scanning for projects...


[INFO]


[INFO] ---------------------< com.bolingcavalry:hellojib >---------------------


[INFO] Building hellojib 0.0.1-SNAPSHOT


[INFO] --------------------------------[ jar ]---------------------------------


[INFO]


[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ hellojib ---


[INFO] Using 'UTF-8' encoding to copy filtered resources.


[INFO] Copying 1 resource


[INFO] Copying 0 resource


[INFO]


[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ hellojib ---


[INFO] Nothing to compile - all classes are up to date


[INFO]


[INFO] --- jib-maven-plugin:1.3.0:dockerBuild (default) @ hellojib ---


[WARNING] Setting image creation time to current time; your image may not be reproducible.


[INFO]


[INFO] Containerizing application to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT...


[INFO]


[INFO] Container entrypoint set to [java, -Xms4g, -Xmx4g, -cp, /app/resources:/app/classes:/app/libs/*, com.bolingcavalry.hellojib.HellojibApplication]


[INFO]


[INFO] Built image to Docker daemon as bolingcavalry/hellojib:0.0.1-SNAPSHOT


[INFO] Executing tasks:


[INFO] [==============================] 100.0% complete


[INFO]


[INFO] ------------------------------------------------------------------------


[INFO] BUILD SUCCESS


[INFO] ------------------------------------------------------------------------


[INFO] Total time: 3.721 s


[INFO] Finished at: 2019-09-01T11:43:23+08:00


[INFO] ------------------------------------------------------------------------


[root@maven hellojib]# docker images


REPOSITORY TAG IMAGE ID CREATED SIZE


bolingcavalry/hellojib 0.0.1-SNAPSHOT 5e3f62d13a73 35 seconds ago 505 MB


192.168.121.131:5000/openjdk 8-jdk-stretch 08ded5f856cc 2 weeks ago 488 MB


docker.io/openjdk 8-jdk-stretch 08ded5f856cc 2 weeks ago 488 MB


docker.io/registry latest f32a97de94e1 5 months ago 25.8 MB

用户头像

极客good

关注

还未添加个人签名 2021.03.18 加入

还未添加个人简介

评论

发布
暂无评论
Jib使用小结(Maven插件版)(1),深入理解java虚拟机第四版pdf百度云