目标:Spring Boot项目打包成可执行的jar文件后,使用docker-maven-plugin(spotify)插件自动构建Docker后上传到阿里云Docker镜像仓库(其他仓库同理)
引用参考:
- docker-maven-plugin插件:https://github.com/spotify/docker-maven-plugin
- Maven提交服务密码加密:https://maven.apache.org/guides/mini/guide-encryption.html
- Maven的settings.xml:http://maven.apache.org/ref/3.3.9/maven-settings/settings.html
准备工作:
1、 构建Spring Boot项目
2、 pom.xml文件中 加入docker-maven-plugin插件
完整pom文件:
4.0.0
com.example
dockerdemo
0.0.1
jar
dockerdemo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE
UTF-8
UTF-8
1.8
registry.cn-hangzhou.aliyuncs.com
viiso
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-starter-test
test
src/main/docker
true
**/Dockerfile
../docker
org.springframework.boot
spring-boot-maven-plugin
com.spotify
docker-maven-plugin
0.4.13
package
build
tag-image
package
tag
${docker.registry.name}/${project.artifactId}:${project.version}
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
push-image
deploy
push
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
docker-aliyun
registry.cn-hangzhou.aliyuncs.com
true
target/docker
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
${git.commit.id.abbrev}
latest
/
${project.build.directory}
${project.build.finalName}.jar
说明:
Dockerfile文件
# Version 0.0.1
FROM java:8
MAINTAINER Ryuu KK "[email protected]"
# 环境变量
ENV WORK_PATH /home/project/dockerdemo
ENV APP_NAME @project.build.finalName@[email protected]@
ENV APP_VERSION @project.version@
EXPOSE 8080
#USER
#USER user:group
#VOLUME
VOLUME ["/home/project", "/tmp/data"]
#ADD
#COPY
COPY $APP_NAME $WORK_PATH/
#LABEL
#STOPSIGNAL
#ARG
#ONBUILD
# WORKDIR
WORKDIR $WORK_PATH
# ENTRYPOINT
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom"]
# CMD
CMD ["-jar", "@project.build.finalName@[email protected]@"]
3、settings.xml配置私有库的访问
首先使用你的私有仓库访问密码生成主密码:
mvn --encrypt-master-password
其次在settings.xml文件的同级目录创建settings-security.xml文件,将主密码写入:
{Ns0JM49fW9gHMTZ44n*****************=}
最后使用你的私有仓库访问密码生成服务密码,将生成的密码写入到settings.xml的
mvn --encrypt-password
docker-aliyun
***[email protected]
{D9YIyWYvtYsHayLjIenj***********=}
***[email protected]
更多settings.xml配置参考:http://maven.apache.org/ref/3.3.9/maven-settings/settings.html
4、执行maven install
如果mvn clean install -Dmaven.test.skip=true -s ~/.m2/settings.xml
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building dockerdemo 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ dockerdemo ---
[INFO] Deleting /home/tony/git/dockerdemo/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ dockerdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource to ../docker
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ dockerdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/tony/git/dockerdemo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ dockerdemo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ dockerdemo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ dockerdemo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ dockerdemo ---
[INFO] Building jar: /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:repackage (default) @ dockerdemo ---
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:build (default) @ dockerdemo ---
[INFO] Copying /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar -> /home/tony/git/dockerdemo/target/docker/dockerdemo-0.0.1.jar
[INFO] Copying target/docker/dockerdemo-0.0.1.jar -> /home/tony/git/dockerdemo/target/docker/dockerdemo-0.0.1.jar
[INFO] Copying target/docker/Dockerfile -> /home/tony/git/dockerdemo/target/docker/Dockerfile
[INFO] Building image registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
Step 1 : FROM java:8
---> a001fc27db5a
Step 2 : MAINTAINER Ryuu KK "[email protected]"
---> Using cache
---> 9142f19f66e4
Step 3 : ENV WORK_PATH /home/project/dockerdemo
---> Using cache
---> f804b2685414
Step 4 : ENV APP_NAME dockerdemo-0.0.1.jar
---> Using cache
---> 449b34e1b059
Step 5 : ENV APP_VERSION 0.0.1
---> Using cache
---> 434e7a21767b
Step 6 : EXPOSE 8080
---> Using cache
---> b4f12840a5fd
Step 7 : VOLUME /home/project /tmp/data
---> Using cache
---> 6dc6a16375f8
Step 8 : ADD $APP_NAME $WORK_PATH/
---> fb9ca6c55101
Removing intermediate container 0e7d77507c01
Step 9 : COPY . $WORK_PATH/
---> 1f3848c151a5
Removing intermediate container 3ffe74084b83
Step 10 : WORKDIR $WORK_PATH
---> Running in 98819e023aec
---> 14d8dda96d7c
Removing intermediate container 98819e023aec
Successfully built 14d8dda96d7c
[INFO] Built registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
[INFO] Tagging registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1 with latest
[INFO] Pushing registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo]
d8c8f704105a: Pushed
cfb84949120c: Pushed
2071795f387c: Layer already exists
046002f6f2ea: Layer already exists
9425eace6ed9: Layer already exists
9c39e87731b7: Layer already exists
f7ed56f3fce3: Layer already exists
53c779688d06: Layer already exists
60a0858edcd5: Layer already exists
b6ca02dfe5e6: Layer already exists
0.0.1: digest: sha256:e743bfd839a6457a4db7fd22edcb3758ffc39e4e7816c8d00bc24ccf0ede13a5 size: 2424
null: null
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:tag (tag-image) @ dockerdemo ---
[INFO] Creating tag registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1 from registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
[INFO] Pushing registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo]
d8c8f704105a: Layer already exists
cfb84949120c: Layer already exists
2071795f387c: Layer already exists
046002f6f2ea: Layer already exists
9425eace6ed9: Layer already exists
9c39e87731b7: Layer already exists
f7ed56f3fce3: Layer already exists
53c779688d06: Layer already exists
60a0858edcd5: Layer already exists
b6ca02dfe5e6: Layer already exists
0.0.1: digest: sha256:e23d5c731170fd2980b688c34b5a844fac66d9547ba48a41417bd1684425958f size: 2424
null: null
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ dockerdemo ---
[INFO] Installing /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar to /home/tony/.m2/repository/com/example/dockerdemo/0.0.1/dockerdemo-0.0.1.jar
[INFO] Installing /home/tony/git/dockerdemo/pom.xml to /home/tony/.m2/repository/com/example/dockerdemo/0.0.1/dockerdemo-0.0.1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54.660 s
[INFO] Finished at: 2017-01-08T15:48:33+08:00
[INFO] Final Memory: 43M/303M
[INFO] ------------------------------------------------------------------------
查看阿里云Docker镜像仓库,SHA256摘要与日志的摘要一致
5、运行私有仓库的dockerdemo镜像
使用SecureCRT 登录客户端
sudo docker login --username=***[email protected] registry.cn-hangzhou.aliyuncs.com
下载仓库镜像
sudo docker pull registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:[镜像版本号]
运行镜像,将Docker容器的8080端口映射本机80端口
docker run -i -t -p 80:8080 registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
6、测试
使用服务器ip进行测试
curl http://ip/health
{"status":"UP","diskSpace":{"status":"UP","total":42139451392,"free":38050045952,"threshold":10485760}}
dockerdemo项目代码:https://code.aliyun.com/ryuu.pro/dockerdemo.git
------------- 2017/03/21 更新------------------
在多Module项目中使用该插件发现的几个问题
相对路径在多模块中出现“basedir target does not exist ”问题,使用
指定编译路径
修改Dockerfile文件的生成路径默认到target路径,由于Dockerfile里使用COPY
无法改变文件路径,如COPY ../xxxx.jar /xxx
时将出现"Permission denied"的问题
src/main/docker
true
**/Dockerfile
${project.build.directory}
原文 使用
缺少插件
pl.project13.maven
git-commit-id-plugin
false
如果不需要git属性插件,可以直接使用
目标:Spring Boot项目打包成可执行的jar文件后,使用docker-maven-plugin(spotify)插件自动构建Docker后上传到阿里云Docker镜像仓库(其他仓库同理)
引用参考:
- docker-maven-plugin插件:https://github.com/spotify/docker-maven-plugin
- Maven提交服务密码加密:https://maven.apache.org/guides/mini/guide-encryption.html
- Maven的settings.xml:http://maven.apache.org/ref/3.3.9/maven-settings/settings.html
准备工作:
1、 构建Spring Boot项目
2、 pom.xml文件中 加入docker-maven-plugin插件
完整pom文件:
4.0.0
com.example
dockerdemo
0.0.1
jar
dockerdemo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE
UTF-8
UTF-8
1.8
registry.cn-hangzhou.aliyuncs.com
viiso
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
org.springframework.boot
spring-boot-starter-test
test
src/main/docker
true
**/Dockerfile
../docker
org.springframework.boot
spring-boot-maven-plugin
com.spotify
docker-maven-plugin
0.4.13
package
build
tag-image
package
tag
${docker.registry.name}/${project.artifactId}:${project.version}
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
push-image
deploy
push
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
docker-aliyun
registry.cn-hangzhou.aliyuncs.com
true
target/docker
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${project.version}
${git.commit.id.abbrev}
latest
/
${project.build.directory}
${project.build.finalName}.jar
说明:
Dockerfile文件
# Version 0.0.1
FROM java:8
MAINTAINER Ryuu KK "[email protected]"
# 环境变量
ENV WORK_PATH /home/project/dockerdemo
ENV APP_NAME @project.build.finalName@[email protected]@
ENV APP_VERSION @project.version@
EXPOSE 8080
#USER
#USER user:group
#VOLUME
VOLUME ["/home/project", "/tmp/data"]
#ADD
#COPY
COPY $APP_NAME $WORK_PATH/
#LABEL
#STOPSIGNAL
#ARG
#ONBUILD
# WORKDIR
WORKDIR $WORK_PATH
# ENTRYPOINT
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom"]
# CMD
CMD ["-jar", "@project.build.finalName@[email protected]@"]
3、settings.xml配置私有库的访问
首先使用你的私有仓库访问密码生成主密码:
mvn --encrypt-master-password
其次在settings.xml文件的同级目录创建settings-security.xml文件,将主密码写入:
{Ns0JM49fW9gHMTZ44n*****************=}
最后使用你的私有仓库访问密码生成服务密码,将生成的密码写入到settings.xml的
mvn --encrypt-password
docker-aliyun
***[email protected]
{D9YIyWYvtYsHayLjIenj***********=}
***[email protected]
更多settings.xml配置参考:http://maven.apache.org/ref/3.3.9/maven-settings/settings.html
4、执行maven install
如果mvn clean install -Dmaven.test.skip=true -s ~/.m2/settings.xml
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building dockerdemo 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ dockerdemo ---
[INFO] Deleting /home/tony/git/dockerdemo/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ dockerdemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource to ../docker
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ dockerdemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/tony/git/dockerdemo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ dockerdemo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ dockerdemo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ dockerdemo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ dockerdemo ---
[INFO] Building jar: /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.3.RELEASE:repackage (default) @ dockerdemo ---
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:build (default) @ dockerdemo ---
[INFO] Copying /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar -> /home/tony/git/dockerdemo/target/docker/dockerdemo-0.0.1.jar
[INFO] Copying target/docker/dockerdemo-0.0.1.jar -> /home/tony/git/dockerdemo/target/docker/dockerdemo-0.0.1.jar
[INFO] Copying target/docker/Dockerfile -> /home/tony/git/dockerdemo/target/docker/Dockerfile
[INFO] Building image registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
Step 1 : FROM java:8
---> a001fc27db5a
Step 2 : MAINTAINER Ryuu KK "[email protected]"
---> Using cache
---> 9142f19f66e4
Step 3 : ENV WORK_PATH /home/project/dockerdemo
---> Using cache
---> f804b2685414
Step 4 : ENV APP_NAME dockerdemo-0.0.1.jar
---> Using cache
---> 449b34e1b059
Step 5 : ENV APP_VERSION 0.0.1
---> Using cache
---> 434e7a21767b
Step 6 : EXPOSE 8080
---> Using cache
---> b4f12840a5fd
Step 7 : VOLUME /home/project /tmp/data
---> Using cache
---> 6dc6a16375f8
Step 8 : ADD $APP_NAME $WORK_PATH/
---> fb9ca6c55101
Removing intermediate container 0e7d77507c01
Step 9 : COPY . $WORK_PATH/
---> 1f3848c151a5
Removing intermediate container 3ffe74084b83
Step 10 : WORKDIR $WORK_PATH
---> Running in 98819e023aec
---> 14d8dda96d7c
Removing intermediate container 98819e023aec
Successfully built 14d8dda96d7c
[INFO] Built registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
[INFO] Tagging registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1 with latest
[INFO] Pushing registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo]
d8c8f704105a: Pushed
cfb84949120c: Pushed
2071795f387c: Layer already exists
046002f6f2ea: Layer already exists
9425eace6ed9: Layer already exists
9c39e87731b7: Layer already exists
f7ed56f3fce3: Layer already exists
53c779688d06: Layer already exists
60a0858edcd5: Layer already exists
b6ca02dfe5e6: Layer already exists
0.0.1: digest: sha256:e743bfd839a6457a4db7fd22edcb3758ffc39e4e7816c8d00bc24ccf0ede13a5 size: 2424
null: null
[INFO]
[INFO] --- docker-maven-plugin:0.4.13:tag (tag-image) @ dockerdemo ---
[INFO] Creating tag registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1 from registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
[INFO] Pushing registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo]
d8c8f704105a: Layer already exists
cfb84949120c: Layer already exists
2071795f387c: Layer already exists
046002f6f2ea: Layer already exists
9425eace6ed9: Layer already exists
9c39e87731b7: Layer already exists
f7ed56f3fce3: Layer already exists
53c779688d06: Layer already exists
60a0858edcd5: Layer already exists
b6ca02dfe5e6: Layer already exists
0.0.1: digest: sha256:e23d5c731170fd2980b688c34b5a844fac66d9547ba48a41417bd1684425958f size: 2424
null: null
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ dockerdemo ---
[INFO] Installing /home/tony/git/dockerdemo/target/dockerdemo-0.0.1.jar to /home/tony/.m2/repository/com/example/dockerdemo/0.0.1/dockerdemo-0.0.1.jar
[INFO] Installing /home/tony/git/dockerdemo/pom.xml to /home/tony/.m2/repository/com/example/dockerdemo/0.0.1/dockerdemo-0.0.1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54.660 s
[INFO] Finished at: 2017-01-08T15:48:33+08:00
[INFO] Final Memory: 43M/303M
[INFO] ------------------------------------------------------------------------
查看阿里云Docker镜像仓库,SHA256摘要与日志的摘要一致
5、运行私有仓库的dockerdemo镜像
使用SecureCRT 登录客户端
sudo docker login --username=***[email protected] registry.cn-hangzhou.aliyuncs.com
下载仓库镜像
sudo docker pull registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:[镜像版本号]
运行镜像,将Docker容器的8080端口映射本机80端口
docker run -i -t -p 80:8080 registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:0.0.1
6、测试
使用服务器ip进行测试
curl http://ip/health
{"status":"UP","diskSpace":{"status":"UP","total":42139451392,"free":38050045952,"threshold":10485760}}
dockerdemo项目代码:https://code.aliyun.com/ryuu.pro/dockerdemo.git
------------- 2017/03/21 更新------------------
在多Module项目中使用该插件发现的几个问题
相对路径在多模块中出现“basedir target does not exist ”问题,使用
指定编译路径
修改Dockerfile文件的生成路径默认到target路径,由于Dockerfile里使用COPY
无法改变文件路径,如COPY ../xxxx.jar /xxx
时将出现"Permission denied"的问题
src/main/docker
true
**/Dockerfile
${project.build.directory}
原文 使用
缺少插件
pl.project13.maven
git-commit-id-plugin
false
如果不需要git属性插件,可以直接使用