springboot工程docker-maven-plugin构建docker镜像并push到docker-hub

springboot工程docker -maven-plugin构建docker镜像并push到docker-hub

1. 环境、软件准备
Docker:version 18.09.2
Maven:version 3.6.0
Java: version 1.8.0_91
docker-maven-plugin:1.8.0_191
没有Docker的自行安装
2. pom-docker.xml构建
将工程pom.xml复制重命名为pom-docker.xml,加入以下内容:

 <build>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!-- tag::plugin[] -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <!--docker镜像相关的配置信息-->
                <executions>
                    <execution>
                        <id>build-image</id>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--镜像名,这里用工程名 -->
                    <imageName>docker-hub的登录名/${project.artifactId}</imageName>
                    <!--Dockerfile文件所在目录-->
                    <dockerDirectory>${project.basedir}/src/main/resources</dockerDirectory>
                    <!--TAG,这里用工程版本号-->
                    <imageTags>
                        <imageTag>${project.version}</imageTag>
                    </imageTags>
                    <forceTags>true</forceTags>
                    <!--<baseImage>java</baseImage>-->
                    <!--<entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>-->
                    <!--构建镜像的配置信息-->
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.artifactId}‐${project.version}.jar</include>
                        </resource>
                    </resources>
                    <!-- 以下两行是为了docker push到DockerHub使用的。 -->
                    <serverId>docker-hub</serverId>
                    <registryUrl>https://index.docker.io/v1/</registryUrl>
                </configuration>

            </plugin>

        </plugins>
    </build>

3. dockerfile配置

FROM java:8
EXPOSE 9005
VOLUME /tmp
COPY system-service-1.0-SNAPSHOT.jar system-service.jar
RUN sh -c 'touch /system-service.jar'
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar /system-service.jar" ]

system-service-1.0-SNAPSHOT.jar跟dockerfile都放置到resources下面

4. maven settings.xml设置

 <server>
    <id>docker-hub</id>
    <username>你的docker-hub的用户名</username>
    <password>你的docker-hub的密码</password>
     <configuration>
        <email>你的docker-hub的邮箱</email>
    </configuration>
  </server>
<pluginGroup>com.spotify</pluginGroup>

5. DpushImage
mvn -f pom-docker.xml clean package -DpushImage
成功有如下信息:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:16 min
[INFO] Finished at: 2019-07-23T17:43:34+08:00
[INFO] ------------------------------------------------------------------------

在docker-hub仓库中也能看到镜像
6. 注意事项
(1)将pom-docker.xml中的docker-hub的用户名替换成自己的
(2)先maven生成jar包,保证jar的可用
(3)settings.xml 两条配置信息
(4)这是在mac上测试的,window上没有测试

docker maven plugin 是个简单的可以管理Docker容器maven插件,这个插件将会根据你的配置,在构建时启动容器,构建结束时停止容器并删除,如果本地找不到镜像Docker会自动去中央仓库下载。 简单示例:     com.ofbizian     docker-maven-plugin     1.0.0                                            busybox                                                      start-docker             pre-integration-test                              start                            所有可能配置的完整示例:     com.ofbizian     docker-maven-plugin     1.0.0              http://localhost:4243                                       dockerfile/redis                                      <![CDATA[                     {"Hostname":"",                         "PortSpecs":null,                         "User":"",                         "Tty":false,                         "OpenStdin":false,                         "StdinOnce":false,                         "Memory":0,                         "MemorySwap":0,                         "CpuShares":0,                         "AttachStdin":false,                         "AttachStdout":false,                         "AttachStderr":false,                         "Env":null,                         "Cmd":null,                         "Dns":null,                         "Volumes":null,                         "VolumesFrom":"",                         "Entrypoint":[                         ],                         "NetworkDisabled":false,                         "Privileged":false,                         "WorkingDir":"",                         "Domainname":"",                         "ExposedPorts":null,                         "OnBuild":null}                     ]]>                                                       <![CDATA[                     {"ContainerIDFile": null, "LxcConf": null, "Links": null, "PortBindings": {                         "6379/tcp": [                             {                                 "HostIp": "0.0.0.0",                                 "HostPort": "6379"                             }                         ]                     }, "Privileged": false, "PublishAllPorts": false}                     ]]>                                                            busybox                                                      start-docker             pre-integration-test                              start                                            stop-docker             post-integration-test                              stop                            标签:Docker
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值