原文地址:http://blog.csdn.net/boling_cavalry/article/details/78934391
在上一章《maven构建docker镜像三部曲之二:编码和构建镜像》的实战中,我们将spring boot的web工程构建成docker镜像并在本地启动容器成功,今天我们把docker-maven-plugin插件的推送功能也用上,这样编译、构建、推送都能一次性完成了;
本次实战的java web工程源码和环境都沿用上一章的,源码我已经上传到github上,地址是:[email protected]:zq2599/blog_demos.git,浏览器访问地址是:https://github.com/zq2599/blog_demos,这里面有多个工程,本次实战用到的是mavendockerplugindemo,如下图红框所示:
本次实战有以下两个地方可以存放镜像,我们每个都要试试:
接下来我们分别推送到上述两个仓库,先推送到内网的私有仓库吧。
用SecureCRT登录虚拟机,在工程的目录下新建一个pom_3_inner_server.xml文件,内容和pom_1_by_param.xml文件的一模一样,然后对pom_3_inner_server.xml文件做下列修改:
<registryUrl>192.168.119.148:5000registryUrl>
<pushImage>truepushImage>
<imageName>192.168.119.148:5000/${project.artifactId}:${project.version}imageName>
执行以下命令开始构建:
mvn -f pom_3_inner_server.xml clean package -DskipTests docker:build
构建完毕后发现输出了报错信息,如下:
[INFO] Building image 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
Step 1/3 : FROM java:8u111-jdk
---> d23bdf5b1b1b
Step 2/3 : ADD /mavendockerplugindemo-0.0.1-SNAPSHOT.jar //
---> 634afbb47de5
Removing intermediate container 8c165fad6a2c
Step 3/3 : ENTRYPOINT java -jar /mavendockerplugindemo-0.0.1-SNAPSHOT.jar
---> Running in 1840b6345566
---> ff415c0f2d72
Removing intermediate container 1840b6345566
Successfully built ff415c0f2d72
[INFO] Built 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
[INFO] Tagging 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT with 0.0.1-SNAPSHOT
[INFO] Pushing 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [192.168.119.148:5000/mavendockerplugindemo]
[WARNING] Failed to push 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT, retrying in 10 seconds (1/5).
[INFO] Pushing 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [192.168.119.148:5000/mavendockerplugindemo]
[WARNING] Failed to push 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT, retrying in 10 seconds (2/5).
[INFO] Pushing 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [192.168.119.148:5000/mavendockerplugindemo]
[WARNING] Failed to push 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT, retrying in 10 seconds (3/5).
[INFO] Pushing 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [192.168.119.148:5000/mavendockerplugindemo]
[WARNING] Failed to push 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT, retrying in 10 seconds (4/5).
[INFO] Pushing 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [192.168.119.148:5000/mavendockerplugindemo]
[WARNING] Failed to push 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT, retrying in 10 seconds (5/5).
[INFO] Pushing 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [192.168.119.148:5000/mavendockerplugindemo]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:00 min
[INFO] Finished at: 2017-12-28T17:26:46-08:00
[INFO] Final Memory: 32M/90M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.spotify:docker-maven-plugin:0.4.12:build (default-cli) on project mavendockerplugindemo: Exception caught: Put http://192.168.119.148:5000/v1/repositories/mavendockerplugindemo/: dial tcp 192.168.119.148:5000: getsockopt: connection refused -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
错误信息中有这么一句:Failed to push …,看起来是连不上私有仓库,登录到私有仓库的机器上去看了以下,果然,docker容器没有启动呢…
执行命令docker start docker-registry将私有仓库的服务容器启动,再回到构建服务的机器上,再执行一次构建命令,这次成功了,如下:
[INFO] Building image 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
Step 1/3 : FROM java:8u111-jdk
---> d23bdf5b1b1b
Step 2/3 : ADD /mavendockerplugindemo-0.0.1-SNAPSHOT.jar //
---> a033c632d0b0
Removing intermediate container 1d702736fae4
Step 3/3 : ENTRYPOINT java -jar /mavendockerplugindemo-0.0.1-SNAPSHOT.jar
---> Running in c3e4a2a41936
---> 649774e8dc21
Removing intermediate container c3e4a2a41936
Successfully built 649774e8dc21
[INFO] Built 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
[INFO] Tagging 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT with 0.0.1-SNAPSHOT
[INFO] Pushing 192.168.119.148:5000/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [192.168.119.148:5000/mavendockerplugindemo]
7b4f222745df: Pushed
35c20f26d188: Layer already exists
c3fe59dd9556: Layer already exists
6ed1a81ba5b6: Layer already exists
a3483ce177ce: Layer already exists
ce6c8756685b: Layer already exists
30339f20ced0: Layer already exists
0eb22bfb707d: Layer already exists
a2ae92ffcd29: Layer already exists
0.0.1-SNAPSHOT: digest: sha256:4c8d5489bcb99ec1c0884adfdeeb25e5706de8f940c406c5a560243681b01831 size: 2212
null: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.760 s
[INFO] Finished at: 2017-12-28T17:36:25-08:00
[INFO] Final Memory: 32M/88M
[INFO] ------------------------------------------------------------------------
在用于构建镜像的虚拟机和作为镜像仓库的虚拟机上,分别执行curl -X GET http://192.168.119.148:5000/v2/_catalog,查看私有仓库的镜像信息(192.168.119.148是私有仓库虚拟机的IP),都能见到镜像mavendockerplugindemo,如下图:
推送到内网私有仓库的实战已经完成,接下来我们试试推送到阿里云镜像仓库吧;
使用阿里云镜像仓库前,需要在阿里云网站注册一个账号,请您自行完成注册登录,此处不赘述了;
回到虚拟机上,打开文件/opt/apache-maven-3.3.3/conf/settings.xml,找到
<server>
<id>docker-aliyunid>
<username>[email protected]username>
<password>abcdefpassword>
<configuration>
<email>[email protected]email>
configuration>
server>
请参照注释的提示,将您在阿里云上的账号密码都填写到对应位置;
在java web工程的目录下新建一个pom_4_ali_server.xml文件,内容和pom_1_by_param.xml文件的一模一样,然后再对pom_4_ali_server.xml文件做以下修改:
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<docker.repostory>registry.cn-shenzhen.aliyuncs.comdocker.repostory>
<docker.registry.name>bolingcavalrydocker.registry.name>
properties>
<serverId>docker-aliyunserverId>
<registryUrl>${docker.repostory}registryUrl>
<pushImage>truepushImage>
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
执行以下命令开始构建:
mvn -f pom_4_ali_server.xml clean package -DskipTests docker:build
构建完成后的输出信息如下,表示推送成功了:
[INFO] Building image registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT
Step 1/3 : FROM java:8u111-jdk
---> d23bdf5b1b1b
Step 2/3 : ADD /mavendockerplugindemo-0.0.1-SNAPSHOT.jar //
---> f912a6c9134c
Removing intermediate container a05fb50d6235
Step 3/3 : ENTRYPOINT java -jar /mavendockerplugindemo-0.0.1-SNAPSHOT.jar
---> Running in 1c71545bdd60
---> fa4eee94dae6
Removing intermediate container 1c71545bdd60
Successfully built fa4eee94dae6
[INFO] Built registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT
[INFO] Tagging registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT with 0.0.1-SNAPSHOT
[INFO] Pushing registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT
The push refers to a repository [registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo]
1efab0de4a7d: Pushed
35c20f26d188: Pushed
c3fe59dd9556: Pushed
6ed1a81ba5b6: Pushed
a3483ce177ce: Pushed
ce6c8756685b: Pushed
30339f20ced0: Pushed
0eb22bfb707d: Pushed
a2ae92ffcd29: Pushed
0.0.1-SNAPSHOT: digest: sha256:89062f267fe22df3acc5d6ca7405140162e43076aeda5f7584dbf80fcdc0c58b size: 2212
null: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:28 min
[INFO] Finished at: 2017-12-29T02:45:05-08:00
[INFO] Final Memory: 34M/83M
[INFO] ------------------------------------------------------------------------
docker pull registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT
输出信息如下,表示下载成功:
root@docker-app:/usr/local/work/mavendockerplugindemo# docker pull registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT
0.0.1-SNAPSHOT: Pulling from bolingcavalry/mavendockerplugindemo
e12c678537ae: Already exists
8d9ed335b7db: Already exists
3318dd58ae60: Already exists
624ba6156166: Already exists
c7a02d193df7: Already exists
813b62320378: Already exists
81cf5426393a: Already exists
0a2b7222259b: Already exists
fadb940064b4: Pull complete
Digest: sha256:89062f267fe22df3acc5d6ca7405140162e43076aeda5f7584dbf80fcdc0c58b
Status: Downloaded newer image for registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT
docker run --name demo003 -p 8080:8080 registry.cn-shenzhen.aliyuncs.com/bolingcavalry/mavendockerplugindemo:0.0.1-SNAPSHOT
输出信息会提示启动成功,这时候在当前电脑上打开浏览器,输入"http://192.168.119.155:8080/",可以看见web工程的controller的返回信息了,如下图:
至此,maven构建docker镜像的三部曲就全部结束了,希望对您熟悉和使用docker-maven-plugin插件有所帮助,另外,本文中用到的pom_3_inner_server.xml和pom_4_ali_server.xml文件,我已上传到git上本次实战的web工程中,地址是:[email protected]:zq2599/blog_demos.git,浏览器访问地址是:https://github.com/zq2599/blog_demos,这里面有多个工程,本次实战用到的是mavendockerplugindemo,如下图红框所示: