Containerlize Springboot Application

Containerlize by com.spotify Docker Plugins

Refer: https://www.cnblogs.com/ityouknow/p/8599093.html

Add Docker Support to pom.xml

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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
...
# 指定打包方式和包名
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
...
<properties>
# 指定 docker 鏡像使用的前綴 (imageName: emmettwoo/demo)
<docker.image.prefix>emmettwoo</docker.image.prefix>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
# Dockerfile 在項目中的位置
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>

Add Dockerfile

1
2
3
4
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD ./target/demo-0.0.1-SNAPSHOT.jar /app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Build your image

1
2
3
4
# Build
mvn package docker:build
# Check
docker images | grep emmettwoo/demo

Start a new container

1
docker run -p 8080:8080 -d emmettwoo/demo

Containerlize by Server Image

Add Dockerfile

  • FROM a server image, for example tomcat or jboss.
  • ADD your distribute jar/war files.
  • ENTRYPOINT could be re-defined if needed.
  • run docker build to generate the new image.

Containerlize Springboot Application
https://tech.initialize.in/docker_2022/docker_2022_002_springboot2container/
作者
Emmett Woo
发布于
2023年8月14日
许可协议