此文已由作者赵计刚薪授权网易云社区发布。
欢迎访问网易云社区,了解更多网易技术产品运营经验。
回顾上一章的整个部署过程:
本章使用docker-maven-plugin实现。
一、Dockerfile:与上一章完全相同
二、pom.xml
引入docker-maven-plugin插件
1 <plugin> 2 <groupId>com.spotify</groupId> 3 <artifactId>docker-maven-plugin</artifactId> 4 <version>0.4.13</version> 5 <configuration> 6 <!-- ${project.artifactId}:即artifactId,myservice1-docker ${project.version}:即version,1.0-SNAPSHOT --> 7 <imageName>${project.artifactId}:${project.version}</imageName><!-- 镜像名称 --> 8 <dockerDirectory>${basedir}/src/main/docker</dockerDirectory><!-- Dockerfile文件地址 --> 9 <resources> 10 <resource> 11 <!-- <targetPath>/</targetPath> --> 12 <!-- ${project.build.directory},项目构建输出目录,默认为target/ --> 13 <directory>${project.build.directory}</directory> 14 <!-- ${project.build.directory},打包出来的jar名称,默认为${project.artifactId}-${project.version} --> 15 <include>${project.build.finalName}.jar</include> 16 </resource> 17 </resources> 18 </configuration> 19 </plugin>
执行下边的命令后,
三、运行
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-SNAPSHOT 10 Step 1 : FROM zhaojigang/jdk8:c7_j8 11 ---> e7b4cf41e531 12 Step 2 : ADD myservice1-docker-1.0-SNAPSHOT.jar /opt/jar/ 13 ---> 771c895968f6 14 Removing intermediate container 12392489f5a4 15 Step 3 : ENV JAVA_HOME /opt/jdk 16 ---> Running in a81ffe207dfa 17 ---> 7aded06bc5be 18 Removing intermediate container a81ffe207dfa 19 Step 4 : ENV PATH $PATH:$JAVA_HOME/bin 20 ---> Running in b2598b5eee7c 21 ---> 5c282af98ab3 22 Removing intermediate container b2598b5eee7c 23 Step 5 : CMD java -jar /opt/jar/myservice1-docker-1.0-SNAPSHOT.jar 24 ---> Running in 51881dd70cd2 25 ---> 5e86ecc155d8 26 Removing intermediate container 51881dd70cd2 27 Successfully built 5e86ecc155d8
说明:
四、查看镜像
1 REPOSITORY TAG IMAGE ID CREATED SIZE 2 myservice1-docker 1.0-SNAPSHOT 5e86ecc155d8 21 seconds ago 575.8 MB
五、启动镜像
浏览器访问:localhost:8014/health
注意:
1 REPOSITORY TAG IMAGE ID CREATED SIZE 2 myservice1-docker 1.0-SNAPSHOT 5e86ecc155d8 21 seconds ago 575.8 MB 3 <none> <none> f0f5b0df8ecf 4 minutes ago 575.8 MB
参考:https://github.com/spotify/docker-maven-plugin
更多网易技术、产品、运营经验分享请点击。