Jib - Containerize your Maven project - Tomcat & jib-maven-plugin& WAR Projects
Jib - Containerize your Maven project
用Jib配置idea的maven项目中,实现springboot、tomcat 来build镜像
1.官方文档说明例子- War Projects
Jib还包含WAR项目。如果Maven项目使用的war-包装型,吊臂将默认使用distroless码头为基地的图像部署项目WAR。除了具有包装类型之外,不需要额外的配置war。
请注意,对于来自JAR项目的WAR项目,Jib的工作方式略有不同:
并被忽略。
WAR将被展开/jetty/webapps/ROOT,这是distroless Jetty基本图像的预期WAR位置。
要使用不同的Servlet引擎的基本图像,你可以自定义,和。如果您没有设置entrypoint或者args,吊臂将继承ENTRYPOINT和CMD基本图像的,所以在很多情况下,你可能不需要进行配置。但是,您很可能必须根据基本图像设置到正确的位置。以下是使用Tomcat映像的示例:
<configuration>
<from>
<image>tomcat:8.5-jre8-alpine</image>
</from>
<container>
<!--
For demonstration only: this directory in the base image contains a Tomcat default
app (welcome page), so you may first want to delete this directory in the base image.
-->
<appRoot>/usr/local/tomcat/webapps/ROOT</appRoot>
</container>
</configuration>
2. Jib+Maven+Tomcat 实践代码例子
2.1在maven项目中的pom.xml文件中配置Jib插件
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.1</version>
</plugin>
pluginManagement & plugins 如果在idea右边不显示插件jib,注意把pluginManagement注释了,就可以正常显示了
2.2 配置详情
1.这里有一个验证直接使用用户名和密码,来替代credHelper使用。验证成功后可以直接从Docker hub或者网易hub.c.163.com上获取官方镜像(依据个人喜好配置对应的auth)
<plugins>
<!--jib 构建插件-->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.1</version>
<configuration>
<!--base image from:打包docker的基础镜像,默认镜像是:gcr.io/distroless/java ,需要翻墙to:默认push到dockerhub指定仓库credHelper:docker认证,这个就有点复杂了-->
<from>
<!--<image>freddyshen/tomcat8-5-cst:latest</image>-->
<image>hub.c.163.com/public/tomcat:7.0.28</image>
<!--直接指定凭证(替代credHelper)-->
<auth>
<!--输入网易或者docker hub自己的用户名和密码-->
<username>test</username>
<password>123456</password>
</auth>
</from>
<!--目标镜像registry地址,为了方便测试,你需要换成自己的地址-->
<to>
<image>${registry_url}/${project.artifactId}</image>
<tags>
<!--可以做多个tag-->
<tag>latest</tag>
</tags>
</to>
<container>
<!--容器上放置应用程序内容的根目录。特别适用于WAR打包项目,通过指定放置exploded WAR内容的位置来处理不同的Servlet引擎基础映像-->
<appRoot>/usr/local/tomcat/webapps/ROOT</appRoot>
<!--使用当前时间-->
<useCurrentTimestamp>true</useCurrentTimestamp>
<!--容器在运行时暴露的端口-->
<ports>
<port>8080</port>
</ports>
</container>
<!--如果私有镜像仓库没有启用https,设置allowInsecureRegistries参数为true-->
<allowInsecureRegistries>false</allowInsecureRegistries>
</configuration>
<!--绑定jib:build到 Maven生命周期,例如package-->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>dockerBuild</goal>
</goals>
</execution>
</executions>
</plugin>
已绑定jib:build到 Maven生命周期package,所以直接运行package
2.2 运行效果展示:
/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/ws/dev/SourceTree/demo4shl -Dmaven.home=/Users/ws/dev/maven/apache-maven-3.5.3 -Dclassworlds.conf=/Users/ws/dev/maven/apache-maven-3.5.3/bin/m2.conf "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=50970:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/ws/dev/maven/apache-maven-3.5.3/boot/plexus-classworlds-2.5.2.jar org.codehaus.classworlds.Launcher -Didea.version=2018.1.1 --update-snapshots -s /Users/ws/dev/maven/apache-maven-3.5.3/conf/settings.xml -Dmaven.repo.local=/Users/ws/dev/maven/apache-maven-3.5.3/repository -DskipTests=true package -f pom.xml
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.shl:demo4shl >--------------------------
[INFO] Building demo4shl Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ demo4shl ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/ws/dev/SourceTree/demo4shl/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ demo4shl ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ demo4shl ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/ws/dev/SourceTree/demo4shl/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo4shl ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ demo4shl ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ demo4shl ---
[INFO] Packaging webapp
[INFO] Assembling webapp [demo4shl] in [/Users/ws/dev/SourceTree/demo4shl/target/demo4shl]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/ws/dev/SourceTree/demo4shl/src/main/webapp]
[INFO] Webapp assembled in [23 msecs]
[INFO] Building war: /Users/ws/dev/SourceTree/demo4shl/target/demo4shl.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO]
[INFO] --- jib-maven-plugin:1.0.1:dockerBuild (default) @ demo4shl ---
[WARNING] Setting image creation time to current time; your image may not be reproducible.
[INFO]
[INFO] Containerizing application to Docker daemon as hub.c.163.com/honglei666/demo4shl, hub.c.163.com/honglei666/demo4shl...
[INFO] Getting base image hub.c.163.com/public/tomcat:7.0.28...
[INFO] Building resources layer...
[INFO] The base image requires auth. Trying again for hub.c.163.com/public/tomcat:7.0.28...
[INFO] Retrieving registry credentials for hub.c.163.com...
[INFO] Loading to Docker daemon...
[INFO]
[INFO] Built image to Docker daemon as hub.c.163.com/honglei666/demo4shl, hub.c.163.com/honglei666/demo4shl
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.784 s
[INFO] Finished at: 2019-02-26T21:09:07+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
2.3 验证生成好的镜像:
sai:demo4shl ws$ docker images | grep hub.c.163.com/honglei666/demo4shl
hub.c.163.com/honglei666/demo4shl latest e6c9506e0077 16 minutes ago 267MB
hub.c.163.com/honglei666/demo4shl <none> 96901cfce0b2 20 minutes ago 267MB
sai:demo4shl ws$
2.4 push镜像到镜像仓库:
sai:~ ws$ docker push hub.c.163.com/honglei666/demo4shl
The push refers to repository [hub.c.163.com/honglei666/demo4shl]
7ece48607915: Pushed
a0f9bae65944: Pushed
5f70bf18a086: Layer already exists
ff07b305fdfa: Pushed
c81b5c199370: Pushed
72b520f75c37: Pushed
8730a9c82887: Pushed
9ef641aa2eea: Pushed
fb0c06d8457d: Pushed
2ffc517b9e28: Pushed
84cf9f092c7a: Pushed
bae03cbf1131: Pushed
59dbd0a38594: Pushed
2712458540cb: Pushed
f8cfb55c251b: Pushed
bcaa3b393a28: Pushed
41eaba0ec3cd: Pushed
04ec86136189: Pushed
latest: digest: sha256:adc21ab8e0323322c6784edabb932cbd67bba6ba03aa4cd57e47a11db7cb4a7e size: 5504