spring boot用docker打包部署
1.添加依赖
引入dockerfile-maven-plugin组件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
2.添加文件Dockerfile
Dockerfile和pom.xml在同一级目录
FROM java:8 EXPOSE 8080 ARG JAR_FILE ADD target/${JAR_FILE} /demo-0.0.jar ENTRYPOINT ["java", "-jar","/demo-0.0.jar"]
3.打包
>mvn package -DskipTests [INFO] Scanning for projects... [INFO] [INFO] --------------------------< com.example:demo >-------------------------- [INFO] Building demo 1.0 [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 7 resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 14 source files to E:\java\demo\target\classes [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory E:\java\demo\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to E:\java\demo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ demo --- [INFO] Building jar: E:\java\demo\target\demo-1.0.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.2.5.RELEASE:repackage (repackage) @ demo --- [INFO] Replacing main artifact with repackaged archive [INFO] [INFO] --- dockerfile-maven-plugin:1.4.13:build (default) @ demo --- [INFO] dockerfile: null [INFO] contextDirectory: E:\java\demo [INFO] Building Docker context E:\java\demo [INFO] Path(dockerfile): null [INFO] Path(contextDirectory): E:\java\demo [INFO] [INFO] Image will be built as springdemo/demo:1.0 [INFO] [INFO] Step 1/5 : FROM java:8 [INFO] [INFO] Pulling from library/java [INFO] Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d [INFO] Status: Image is up to date for java:8 [INFO] ---> d23bdf5b1b1b [INFO] Step 2/5 : EXPOSE 8080 [INFO] [INFO] ---> Using cache [INFO] ---> 8b6cab01e7f6 [INFO] Step 3/5 : ARG JAR_FILE [INFO] [INFO] ---> Using cache [INFO] ---> 7ba55f2e8bba [INFO] Step 4/5 : ADD target/${JAR_FILE} /demo-0.0.jar [INFO] [INFO] ---> 7263886dec7b [INFO] Step 5/5 : ENTRYPOINT ["java", "-jar","/demo-0.0.jar"] [INFO] [INFO] ---> Running in 12b670b157c4 [INFO] Removing intermediate container 12b670b157c4 [INFO] ---> 172f4efb3acd [INFO] Successfully built 172f4efb3acd [INFO] Successfully tagged springdemo/demo:1.0 [INFO] [INFO] Detected build of image with id 172f4efb3acd [INFO] Building jar: E:\java\demo\target\demo-1.0-docker-info.jar [INFO] Successfully built springdemo/demo:1.0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:18 min [INFO] Finished at: 2020-04-16T09:04:50+08:00 [INFO] ------------------------------------------------------------------------
说明:
命令执行完后,镜像文件就会在服务端
java:8是镜像依赖的layer,如果不存在,会在执行mvn dockerfile:build命令的时候pull下来的
4.部署
登录docker服务器
sudo docker images
运行
docker run --name springdemo -p 8080:8080 -d springdemo/demo:1.0
注:
版本号不是last需要指定版本号
测试
GET http://192.168.99.100:8080/hello
返回
Hello World!