用Jib配置idea的maven項目中,實現springboot、tomcat 來build鏡像
2. Jib+Maven+Springboot 實踐代碼例子
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)
<!--jib 構建插件-->
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.0.1</version>
<configuration>
<from>
<image>openjdk:alpine</image>
<!--直接指定憑證(替代credHelper)-->
<auth>
<username>${registry_username}</username>
<password>${registry_password}</password>
</auth>
</from>
<to>
<image>${registry_url}/${project.artifactId}</image>
<tags>
<tag>latest</tag>
</tags>
</to>
<container>
<mainClass>com.ecdata.CmpApplication</mainClass>
<!--使用當前時間-->
<useCurrentTimestamp>true</useCurrentTimestamp>
<!--容器在運行時暴露的端口-->
<ports>
<port>8088</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 運行效果展示:
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ cmp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/ws/dev/sourceCode/ideaProjects/sw/cmp/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ cmp ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 13 source files to /Users/ws/dev/sourceCode/ideaProjects/sw/cmp/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ cmp ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ cmp ---
[INFO] Building jar: /Users/ws/dev/sourceCode/ideaProjects/sw/cmp/target/cmp-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.4.RELEASE:repackage (default) @ cmp ---
[INFO]
[INFO] --- jib-maven-plugin:1.0.1:dockerBuild (default) @ cmp ---
[WARNING] Setting image creation time to current time; your image may not be reproducible.
[INFO]
[INFO] Containerizing application to Docker daemon as 31.16.14.55/szy/cmp, 31.16.14.55/szy/cmp...
[INFO] Getting base image openjdk:alpine...
[INFO] Building dependencies layer...
[INFO] Building resources layer...
[INFO] Building classes layer...
[INFO] The base image requires auth. Trying again for openjdk:alpine...
[INFO] Retrieving registry credentials for registry.hub.docker.com...
[INFO]
[INFO] Container entrypoint set to [java, -cp, /app/resources:/app/classes:/app/libs/*, com.ecdata.CmpApplication]
[INFO] Loading to Docker daemon...
[INFO]
[INFO] Built image to Docker daemon as 31.16.14.55/szy/cmp, 31.16.14.55/szy/cmp
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.268 s
[INFO] Finished at: 2019-02-27T09:55:16+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
2.3 驗證生成好的鏡像:
sai:~ ws$ docker images | grep 31.16.14.55/szy/cmp
31.16.14.55/szy/cmp latest 70440c0ad660 6 minutes ago 213MB
sai:~ ws$
2.4 push鏡像到鏡像倉庫:
sai:~ ws$ docker push31.16.14.55/szy/cmp
</div>
原文地址:https://blog.csdn.net/shenhonglei1234/article/details/87967088