springboot+docker(maven)

落花浮王杯 提交于 2019-11-27 07:58:21

第三十八章 springboot+docker(maven)

回顾上一章的整个部署过程:

  • 使用"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插件

复制代码

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>

复制代码

执行下边的命令后,

  • 将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-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

复制代码

说明:

  • maven-jar-plugin进行package
  • spring-boot-maven-plugin进行repackage
  • 将jar和Dockerfile拷贝到同一个目录(target/docker/)
  • 执行Dockerfile进行build

四、查看镜像

  • docker images
1 REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
2 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             SIZE
2 myservice1-docker      1.0-SNAPSHOT        5e86ecc155d8        21 seconds ago      575.8 MB
3 <none>                 <none>              f0f5b0df8ecf        4 minutes ago       575.8 MB
  • 使用docker-maven-plugin构建项目,可以避免手工移动jar,并且可以很方便的自己做成archetype
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!