springboot+Jib+Maven+Idea+Docker 实践


用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>
                &lt;/to&gt;
                &lt;container&gt;
                    &lt;mainClass&gt;com.ecdata.CmpApplication&lt;/mainClass&gt;
                    &lt;!--使用当前时间--&gt;
                    &lt;useCurrentTimestamp&gt;true&lt;/useCurrentTimestamp&gt;
                    &lt;!--容器在运行时暴露的端口--&gt;
                    &lt;ports&gt;
                        &lt;port&gt;8088&lt;/port&gt;
                    &lt;/ports&gt;
                &lt;/container&gt;
                &lt;!--如果私有镜像仓库没有启用https,设置allowInsecureRegistries参数为true--&gt;
                &lt;allowInsecureRegistries&gt;false&lt;/allowInsecureRegistries&gt;
            &lt;/configuration&gt;
            &lt;!--绑定jib:build到 Maven生命周期,例如package--&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;phase&gt;package&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;dockerBuild&lt;/goal&gt;
                    &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;

已绑定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

原文地址:https://www.cnblogs.com/jpfss/p/10930472.html