This article explains how to build a Docker image for a SpringBoot program. Docker is an open source application container engine, which is based on the Go language and complies with the Apache2.0 protocol. Docker allows developers to package their applications and dependencies into a lightweight, portable container that can then be distributed to any popular Linux machine, as well as virtualization. Containers are completely sandboxed, have no interfaces with each other (like iPhone apps), and most importantly, have very low performance overhead.

The preparatory work

Environment:

  • Linux or MAC, not Windows
  • jdk 8
  • Maven 3.0
  • docker

If you don’t know anything about Docker, read docker tutorials.

Create a Springboot project

Import the Web startup dependency and create a Controler:

1
2
3
4
5
6
7
8
9
10
11
12
@SpringBootApplication
@RestController
public
class
SpringbootWithDockerApplication {

@RequestMapping
(
"/"
)

public
String home() {

return
"Hello Docker World"
;

}

public
static
void
main(String[] args) {

SpringApplication.run(SpringbootWithDockerApplication.
class
, args);

}
}

  

Containerize the Springboot project

Docker has a simple dockerfile file as the layer to specify the image. Let’s create a dockerFile:

src/main/docker/Dockerfile:

1
2
3
4
5
6
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD springboot-with-docker-
0.0
.
1
-SNAPSHOT.jar app.jar
RUN sh -c
'touch /app.jar'
ENV JAVA_OPTS=
""
ENTRYPOINT [
"sh"
.
"-c"
.
"java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar"
]

  

We use Maven to build the Docker image.

In Maven’s POM directory, add the plugin built by the Docker image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<properties>

<docker.image.prefix>springio</docker.image.prefix>
</properties>
<build>

<plugins>

<plugin>

<groupId>com.spotify</groupId>

<artifactId>docker-maven-plugin</artifactId>

<version>
0.4
.
11
</version>

<configuration>

<imageName>${docker.image.prefix}/${project.artifactId}</imageName>

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

Using the maven command:

Step 1: MVN Clean

Step 2: MVN package docker:bulid as follows:

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
Step
2
/
6
: VOLUME /tmp
- > Running in a98be3878053
- > 8286 e98b54c5
Removing intermediate container a98be3878053
Step
3
/
6
: ADD springboot-with-docker-
0.0
.
1
-SNAPSHOT.jar app.jar
- > c6ce13e50bbd
Removing intermediate container a303a3058869
Step
4
/
6
: RUN sh -c 'touch /app.jar'
- > Running in cf231afe700e
- > 9 a0ec8936c00
Removing intermediate container cf231afe700e
Step
5
/
6
: ENV JAVA_OPTS ""
- > Running in e192597fc881
- > 2 cb0d73bbdb0
Removing intermediate container e192597fc881
Step
6
/
6
: ENTRYPOINT sh -c java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar
- > Running in ab85f53fcdd8
- > 60 fdb5c61692
Removing intermediate container ab85f53fcdd8
Successfully built 60fdb5c61692
[INFO] Built forezp/springboot-with-docker
[the INFO] -- -- -- -- -- -- -- -- -- -- -- --
[INFO] BUILD SUCCESS
[the INFO] -- -- -- -- -- -- -- -- -- -- -- --
[INFO] Total time:
01
:
45
min
[INFO] Finished at:
2017
-
04
-19T05:
37
:
44
-
07
:
00
[INFO] Final Memory: 19M/48M
[the INFO] -- -- -- -- -- -- -- -- -- -- -- --

The architecture code is as follows:

Source address of data and source code

Spring Cloud large enterprise distributed micro services Cloud architecture source code please add penguin beg: 1791743380