springboot+Jib+Maven+Idea+Docker 实践

5 篇文章 0 订阅
2 篇文章 0 订阅


用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 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[shenhonglei]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值