如果Dockerfile文件需要maven构建参数(比如需要构建后的打包文件名等),则使用@@占位符(如@project.build.finalName@)原因是Sping Boot 的pom将resource插件的占位符由${}改为@@,非继承Spring Boot 的pom文件,则使用${}占位符
如果不需要动态生成Dockerfile文件,则可以将Dockerfile资源拷贝部分放入docker-maven-plugin插件的<resources>配置里
spring-boot-maven-plugin插件一定要在其他构建插件之上,否则打包文件会有问题。
Dockerfile文件
# Version 0.0.1
FROM java:8
MAINTAINER Ryuu KK "ryuu_kk@163.com"
# 环境变量
ENV WORK_PATH /home/project/dockerdemo
ENV APP_NAME @project.build.finalName@.@project.packaging@
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@.@project.packaging@"]
3、settings.xml配置私有库的访问
首先使用你的私有仓库访问密码生成主密码:
mvn --encrypt-master-password <password>
其次在settings.xml文件的同级目录创建settings-security.xml文件,将主密码写入:
<?xml version="1.0" encoding="UTF-8"?>
<settingsSecurity>
<master>{Ns0JM49fW9gHMTZ44n*****************=}</master>
</settingsSecurity>
最后使用你的私有仓库访问密码生成服务密码,将生成的密码写入到settings.xml的<services>中:
mvn --encrypt-password <password>
<server>
<id>docker-aliyun</id>
<username>***pro@gmail.com</username>
<password>{D9YIyWYvtYsHayLjIenj***********=}</password>
<configuration>
<email>***pro@gmail.com</email>
</configuration>
</server>
.m2文件夹
更多settings.xml配置参考:
4、执行maven install
如果<pushImage>false</pushImage>则install阶段将不提交Docker镜像,只有maven的deploy阶段才提交。
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 "ryuu_kk@163.com"
---> 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摘要与日志的摘要一致
Paste_Image.png
5、运行私有仓库的dockerdemo镜像
使用SecureCRT 登录客户端
sudo docker login --username=***pro@gmail.com registry.cn-hangzhou.aliyuncs.com
登录Docker私有仓库
下载仓库镜像
sudo docker pull registry.cn-hangzhou.aliyuncs.com/viiso/dockerdemo:[镜像版本号]