回顾上一章的整个部署过程:
- 使用"mvn install"进行打包jar
- 将jar移动到与Dockerfile文件相同的文件夹下
- 编写Dockerfile文件
- 使用"docker build -t="zjg/myservice1docker:v1.1" /Users/enniu1/Desktop/docker-data/file"命令进行打包
- 需要手动指定生成的images的name和tag,以及dockerfile所在的文件夹
- 之后启动镜像
本章使用docker-maven-plugin实现。
一、Dockerfile:与上一章完全相同
- 放在了一个单独的文件夹下src/main/docker/
二、pom.xml
引入docker-maven-plugin插件
12 com.spotify 3docker-maven-plugin 40.4.13 56 7 19${project.artifactId}:${project.version} 8${basedir}/src/main/docker 910 1811 12 13 17${project.build.directory} 14 15${project.build.finalName}.jar 16
执行下边的命令后,
- 将dockerDirectory目录下的内容(这里只有Dockerfile)拷贝到项目的/target/docker/文件夹下(这里就是/target/docker/Dockerfile)
- 将resource中的include下的文件(这里就是${project.build.finalName}.jar)拷贝到/target/docker/文件夹下(这里就是/target/docker/myservice1-docker-1.0-SNAPSHOT.jar)
- 经过上述两个拷贝,将jar与Dockerfile拷贝到同一个文件夹下了
- 之后进行构建过程
三、运行
mvn clean package docker:build
关键日志:
1 [INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ myservice1-docker --- 2 [INFO] Building jar: /Users/enniu1/Desktop/project/myservice1-docker/target/myservice1-docker-1.0-SNAPSHOT.jar 3 [INFO] 4 [INFO] --- spring-boot-maven-plugin:1.3.0.RELEASE:repackage (default) @ myservice1-docker --- 5 [INFO] 6 [INFO] --- docker-maven-plugin:0.4.13:build (default-cli) @ myservice1-docker --- 7 [INFO] Copying /Users/enniu1/Desktop/project/myservice1-docker/target/myservice1-docker-1.0-SNAPSHOT.jar -> /Users/enniu1/Desktop/project/myservice1-docker/target/docker/myservice1-docker-1.0-SNAPSHOT.jar 8 [INFO] Copying /Users/enniu1/Desktop/project/myservice1-docker/src/main/docker/Dockerfile -> /Users/enniu1/Desktop/project/myservice1-docker/target/docker/Dockerfile 9 [INFO] Building image myservice1-docker:1.0-SNAPSHOT10 Step 1 : FROM zhaojigang/jdk8:c7_j811 ---> e7b4cf41e53112 Step 2 : ADD myservice1-docker-1.0-SNAPSHOT.jar /opt/jar/13 ---> 771c895968f614 Removing intermediate container 12392489f5a415 Step 3 : ENV JAVA_HOME /opt/jdk16 ---> Running in a81ffe207dfa17 ---> 7aded06bc5be18 Removing intermediate container a81ffe207dfa19 Step 4 : ENV PATH $PATH:$JAVA_HOME/bin20 ---> Running in b2598b5eee7c21 ---> 5c282af98ab322 Removing intermediate container b2598b5eee7c23 Step 5 : CMD java -jar /opt/jar/myservice1-docker-1.0-SNAPSHOT.jar24 ---> Running in 51881dd70cd225 ---> 5e86ecc155d826 Removing intermediate container 51881dd70cd227 Successfully built 5e86ecc155d8
说明:
- maven-jar-plugin进行package
- spring-boot-maven-plugin进行repackage
- 将jar和Dockerfile拷贝到同一个目录(target/docker/)
- 执行Dockerfile进行build
四、查看镜像
- docker images
1 REPOSITORY TAG IMAGE ID CREATED SIZE2 myservice1-docker 1.0-SNAPSHOT 5e86ecc155d8 21 seconds ago 575.8 MB
五、启动镜像
- docker run -d -p 8014:8080 5e86ecc155d8
浏览器访问:localhost:8014/health
注意:
- 多次指定build去构建的话,之前构建出来的REPOSITORY和TAG都会变为<none>
1 REPOSITORY TAG IMAGE ID CREATED SIZE2 myservice1-docker 1.0-SNAPSHOT 5e86ecc155d8 21 seconds ago 575.8 MB3f0f5b0df8ecf 4 minutes ago 575.8 MB
- 使用docker-maven-plugin构建项目,可以避免手工移动jar,并且可以很方便的自己做成archetype
参考: