jib 使用教程
原文链接:https://blog.csdn.net/boling_cavalry/article/details/94355659
参考链接: https://blog.csdn.net/boling_cavalry/article/details/100179709
主要的命令:
<!--打包 jib-image.tar 文件-->
mvn compile jib:buildTar
<!--加载到本地docker 镜像中 -->
docker load < target/jib-image.tar
<!--查看镜像列表-->
docker images
docker rm containerId
docker rmi imageId
<!-- 清理 none 镜像 -->
docker image prune
<!--构建到仓库中-->
mvn compile jib:build -Djib.to.auth.username=bolingcavalry -Djib.to.auth.password=123456
添加maven插件
<!--使用jib插件-->
<plugin>
<!-- https://mvnrepository.com/artifact/com.google.cloud.tools/jib-maven-plugin -->
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.6.1</version>
<configuration>
<!--from节点用来设置镜像的基础镜像,相当于Docerkfile中的FROM关键字-->
<from>
<!--使用openjdk官方镜像-->
<image>openjdk:alpine</image>
</from>
<to>
<!--镜像名称和tag,使用了mvn内置变量${project.version},表示当前工程的version-->
<!-- <image>helloworld/alfred:jibtest:${project.version}</image>-->
<image>helloworld/alfred:jibtest</image>
</to>
<!--容器相关的属性-->
<container>
<!--jvm内存参数-->
<jvmFlags>
<jvmFlag>-Xms4g</jvmFlag>
<jvmFlag>-Xmx4g</jvmFlag>
</jvmFlags>
<!--要暴露的端口-->
<ports>
<port>8083</port>
</ports>
<creationTime>USE_CURRENT_TIMESTAMP</creationTime>
</container>
</configuration>
</plugin>
a. from节点用来设置基础镜像,一般使用openjdk官方镜像,如果您想了解更多请参考《openjdk镜像的tag说明》;
c. to节点用于指定镜像名称和tag;
d. container节点用来设置容器的属性,例如对外暴露的端口、jvm参数等;
利用Jib,我们可以将java应用做成docker镜像并生成tar格式的文件,操作步骤如下:
在上述java工程的pom.xml文件所在目录,执行以下命令:
mvn compile jib:buildTar
2 构建成功后,控制台提示以下信息:
[root@kylo-2 hellojib]# mvn compile jib:buildTar
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.guan.demo:hellojib >-----------------------
[INFO] Building hellojib 1.0-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] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /apps/my_test2019/hellojib/target/classes
[INFO]
[INFO] --- jib-maven-plugin:1.6.1:buildTar (default-cli) @ hellojib ---
[WARNING] Setting image creation time to current time; your image may not be reproducible.
[INFO]
[INFO] Containerizing application to file at '/apps/my_test2019/hellojib/target/jib-image.tar'...
[INFO] The base image requires auth. Trying again for openjdk:alpine...
[INFO]
[INFO] Container entrypoint set to [java, -Xms4g, -Xmx4g, -cp, /app/resources:/app/classes:/app/libs/*, com.Application]
[INFO]
[INFO] Built image tarball at /apps/my_test2019/hellojib/target/jib-image.tar
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.892 s
[INFO] Finished at: 2019-11-19T13:13:18+08:00
[INFO] ------------------------------------------------------------------------
在java工程的target目录下,发现生成了名为jib-image.tar的文件,
在java工程的target目录下执行命令docker load < jib-image.tar即可将该tar文件中的镜像加载到本地镜像仓库:
[root@kylo-2 hellojib]# docker load < target/jib-image.tar
73046094a9b8: Loading layer [==================================================>] 2.207MB/2.207MB
298c3bb2664f: Loading layer [==================================================>] 239B/239B
93351e248e6e: Loading layer [==================================================>] 70.58MB/70.58MB
64c0b8425948: Loading layer [==================================================>] 14.91MB/14.91MB
9789a0aea7b2: Loading layer [==================================================>] 208B/208B
52de304c5a37: Loading layer [==================================================>] 985B/985B
Loaded image: guanpeizuo/alfred:jibtest
执行命令docker images可见镜像加载成功:
[root@kylo-2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld/alfred jibtest eaa495b99508 11 minutes ago 119MB
openjdk 8-jdk-slim 971671e78456 4 weeks ago 284MB
openjdk 8-jdk-alpine a3562aa0b991 6 months ago 105MB
registry latest f32a97de94e1 8 months ago 25.8MB
elasticsearch 6.4.3 01e5bee1e059 12 months ago 795MB
ascdc/jdk8 latest 9f04bbc44406 18 months ago 644MB
[root@kylo-2 ~]#
可见通过jib生成的tar是正常的docker镜像文件,可以导入
验证镜像是否可用
执行以下命令,即可启动容器:
docker run --rm -p 8080:8080 bolingcavalry/hellojib:0.0.1-SNAPSHOT
控制台显示springboot的启动信息
构建到本地镜像仓库
上面的操作是将java应用生成tar文件然后再导入到本地镜像仓库,您也可以直接将java应用构建到本地镜像仓库中:
先执行命令docker rmi bolingcavalry/hellojib:0.0.1-SNAPSHOT将前面构建的镜像删除;
在pom.xml文件所在目录执行以下命令即可:
mvn clean compile jib:dockerBuild -U
终端提示如下:
[root@centos7 hellojib]# mvn clean compile jib:dockerBuild -U
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< com.bolingcavalry:hellojib >---------------------
[INFO] Building hellojib 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ hellojib ---
[INFO] Deleting /root/temp/201906/29/hellojib/target
[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] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /root/temp/201906/29/hellojib/target/classes
[INFO]
[INFO] --- jib-maven-plugin:1.3.0:dockerBuild (default-cli) @ hellojib ---
[INFO]
[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]
[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: 20.923 s
[INFO] Finished at: 2019-06-30T22:04:36+08:00
[INFO] ------------------------------------------------------------------------
执行命令docker images可见镜像已经在镜像仓库中了:
[root@centos7 hellojib]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
bolingcavalry/hellojib 0.0.1-SNAPSHOT cf93bd81fbd5 49 years ago 505 MB
构建到hub.docker.com
docker的中央仓库是 hub.docker.com ,jib也可以在镜像构建成功后自动推送到hub.docker.com网站,前提是您已经在该网站注册过,例如我已经注册过,账号是bolingcavalry,因此名为bolingcavalry/XXXXXXXX的镜像都可以推送到 hub.docker.com:
在pom.xml文件所在目录执行以下命令即可自动推送到 hub.docker.com,其中bolingcavalry是我在hub.docker.com上的注册账号,123456是密码:
mvn compile jib:build -Djib.to.auth.username=bolingcavalry -Djib.to.auth.password=123456
终端信息如下:
[root@centos7 hellojib]# mvn compile jib:build -Djib.to.auth.username=bolingcavalry -Djib.to.auth.password=123456
[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:build (default-cli) @ hellojib ---
[INFO]
[INFO] Containerizing application to bolingcavalry/hellojib:0.0.1-SNAPSHOT...
[INFO] The base image requires auth. Trying again for openjdk:8-jdk-stretch...
[INFO]
[INFO] Container entrypoint set to [java, -Xms4g, -Xmx4g, -cp, /app/resources:/app/classes:/app/libs/*, com.bolingcavalry.hellojib.HellojibApplication]
[INFO]
[INFO] Built and pushed image as bolingcavalry/hellojib:0.0.1-SNAPSHOT
[INFO] Executing tasks:
[INFO] [==============================] 100.0% complete
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:47 min
[INFO] Finished at: 2019-06-30T22:16:19+08:00
[INFO] ------------------------------------------------------------------------
在hub.docker.com上已经可以看到此镜像了,如下图(要登录后再看自己的镜像列表,如果没有登录是无法立即查到的):
在这里插入图片描述
至此,Jib的实战已经完成,常用的操作相信您已经熟悉了,希望本文能帮助您做出满意的镜像;
原文链接:https://blog.csdn.net/boling_cavalry/article/details/94355659
网友评论