Maven 插件
dockerfile-maven-plugin:1.4.9,前生是docker-maven-plugin,是一个快速构建docker镜像的maven插件,github地址:https://github.com/spotify/dockerfile-maven
配置setting.xml
setting.xml文件一般在用户目录下的.m2文件夹下
- 在
中添加neuxs和docker推送的认证信息
neuxs
admin
admin123
192.168.30.205:5000
admin
admin123
- 在
中添加远程仓库的配置信息
mario-nexus
mario nexus repository
mario nexus repository
http://192.168.30.205:8081/repository/maven-public/
true
true
- 在
中激活上面的配置
mario-nexus
配置dockerfile-maven-plugin
- 在父pom.xml 中添加docker镜像的仓库地址和nexus的仓库地址
...
192.168.30.206:5000
....
nexus
http://192.168.30.205:8081/repository/maven-releases/
nexus
http://192.168.30.205:8081/repository/maven-snapshots/
2.在discovery中的pom.xml配置镜像信息
......
com.spotify
dockerfile-maven-plugin
default
build
push
${docker.repository}/${project.artifactId}
${project.version}
true
${project.build.finalName}.jar
会使用settting.xml中的认证信息,通过docker仓库的地址匹配
可以设置自定义参数,给Dockerfile使用
- 编写Dockerfile文件
在discovery的pom.xml同目录下新建一个Dockerfile文件
FROM openjdk:8-jre-alpine
ARG JAR_FILE
COPY target/${JAR_FILE} discovery.jar
EXPOSE 8761
ENTRYPOINT ["java","-jar","/discovery.jar"]
4.修改discovery,provider-user,provider-order的application.yml
中的defaultZone属性,因为在一个宿主机的不同容器间需要通过容器名,并且配置network
才能通讯,之间是通过--link
,新版本已不再推荐
//defaultZone: http://locahost:8761/eureka/
defaultZone: http://discovery:8761/eureka/
FROM
设置基础镜像,openjdk:8-jre-alpine,一个小的基础jdk镜像,会有时区问题,可以自行修改
ARG
使用pom.xml定义的参数
构建并推送
- 为了本地能识别远程docker仓库,还需要将仓库地址配置到本地的insecure-registries列表中
- linux 在
/etc/docker/
新建或修改daemon.json
,编辑保存
{
"insecure-registries":["http://192.168.30.205:5000"]
}
重启docker systemctl restart docker
- docker桌面版,直接在设置->Daemon中配置后重启
- 登录仓库
docker login 192.168.30.206:5000
,输入用户名和密码
Username: admin
Password:
Login Succeeded
- 在discovery目录下执行
mvn clean deploy
,插件会自动执行打包,构建,推送
控制会打印执行过程,比如build
[INFO] Image will be built as 192.168.30.205:5000/discovery:0.0.1-SNAPSHOT
[INFO]
[INFO] Step 1/5 : FROM openjdk:8-jre-alpine
[INFO]
[INFO] Pulling from library/openjdk
[INFO] Digest: sha256:e8a689c4b2913f07e401e5e9325d66cecc33d30738aadf1dbe3db5af70997742
[INFO] Status: Image is up to date for openjdk:8-jre-alpine
[INFO] ---> 2e01f547f003
[INFO] Step 2/5 : ARG JAR_FILE
[INFO]
[INFO] ---> Using cache
[INFO] ---> d3f32052650f
[INFO] Step 3/5 : COPY target/${JAR_FILE} discovery.jar
[INFO]
[INFO] ---> cf105f53d1b7
[INFO] Step 4/5 : EXPOSE 8761
[INFO]
[INFO] ---> Running in ca9b00054168
[INFO] Removing intermediate container ca9b00054168
[INFO] ---> 80706e9991d6
[INFO] Step 5/5 : ENTRYPOINT ["java","-jar","/discovery.jar"]
[INFO]
[INFO] ---> Running in 099810346b7c
[INFO] Removing intermediate container 099810346b7c
[INFO] ---> 2010ed4e1fcb
[INFO] Successfully built 2010ed4e1fcb
[INFO] Successfully tagged 192.168.30.205:5000/discovery:0.0.1-SNAPSHOT
push
[INFO] --- dockerfile-maven-plugin:1.4.9:push (default) @ discovery ---
[INFO] The push refers to repository [192.168.30.205:5000/discovery]
[INFO] Image 1d56fe2feaed: Preparing
[INFO] Image 9bca1faaa73e: Preparing
[INFO] Image 0c3170905795: Preparing
[INFO] Image df64d3292fd6: Preparing
[INFO] Image df64d3292fd6: Layer already exists
[INFO] Image 9bca1faaa73e: Layer already exists
[INFO] Image 0c3170905795: Layer already exists
[INFO] Image 1d56fe2feaed: Pushing
[INFO] Image 1d56fe2feaed: Pushed
[INFO] 0.0.1-SNAPSHOT: digest: sha256:0a311489626a96f2ee922cbba3e62bd6161922442c49d5e627399d032bbc314d size: 1159
访问 http://192.168.30.205:8081/,选择docker-hosted,可以看到镜像已经上传成功
其他两个服务provider-user,provider-order 也是类似的操作,最终可以看到三个服务都已上传成功
下一篇 Docker Swarm + SpringCloud(四)docker-compose编排镜像