使用dockerfile-maven-extension插件打包springboot项目Docker镜像

修改POM文件

添加dockerfile-maven-extension插件,并按照例子修改项目构建配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<build>
<!--jar包名称 -->
<finalName>EncSys</finalName>
<plugins>
<plugin>
<!--Springboot插件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.10</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<!--docker镜像名 -->
<repository>EncSys</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-extension</artifactId>
<version>1.4.10</version>
</extension>
</extensions>
</build>

修改Dockerfile

使用基础的openjdk8进行构建,只需要将maven构建出的jar包添加进image,设置好ENTRYPOINT即可.

1
2
3
4
5
6
FROM openjdk:8-jdk
MAINTAINER coden00b <[email protected]>
WORKDIR /opt/EncSyc
EXPOSE 8000
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/opt/EncSyc/EncSys.jar"]
ADD target/EncSys.jar /opt/EncSyc/EncSys.jar

尝试打包

使用mvn package

1
mvn clean package

构建成功后可以docker run创建image的容器进行测试