Reprinted from "pure smile": https://blog.csdn.net/ityouknow/article/details/79606926
Docker technology development services to micro landing provides a more convenient environment, using Spring Boot Docker deployment is very simple, in this article we take a simple learning.
First, build a simple Spring Boot project, and then add Docker support to the project, and finally deploying the project.
A simple Spring Boot project
In pom.xml
using the depending Spring Boot 2.0
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
- 1
- 2
- 3
- 4
- 5
Adding web testing and dependence
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
Create a DockerController, in which there is a index()
method that returns the visit to:Hello Docker!
@RestController
public class DockerController {
@RequestMapping("/")
public String index() {
return "Hello Docker!";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
Startup class
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
After completion of the project started, after a successful start to put the browser to ask: http://localhost:8080/
page return: Hello Docker!
, Description Spring Boot project is properly configured.
Spring Boot project to add support for Docker
In pom.xml-properties
adding image name in Docker
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>
- 1
- 2
- 3
plugins added Docker build plug-ins:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</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>
<!-- Docker maven plugin -->
</plugins>
</build>
- 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
In the directory src/main/docker
created Dockerfile files, Dockerfile file that explains how to build the image.
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-boot-docker-1.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
- 1
- 2
- 3
- 4
This Dockerfile file is very simple, basic building Jdk environment, adding to the mirror in Spring Boot Jar, explain briefly:
- FROM, indication Jdk8 environment based mirroring, if the mirror is not local will download from DockerHub
- VOLUME, VOLUME points to a
/tmp
directory, because the Spring Boot using the built-in Tomcat container, Tomcat is used by default/tmp
as the working directory. The effect of this command is: in the host/var/lib/docker
creates a temporary file directory and put it in the container linked to the/tmp
directory - ADD, copy and rename files
- EntryPoint, Tomcat in order to shorten the startup time, adding
java.security.egd
system properties directed/dev/urandom
as EntryPoint
Add this Spring Boot project Docker dependence is complete.
Construction of packing Environment
We need to have a Docker environment to package Spring Boot project, set up in the Windows environment Docker lot of trouble, so I am here as an example to Centos 7.
Docker installation environment
installation
yum install docker
- 1
After installation is complete, use the following command to start the docker services, and set it to boot:
ervice docker start
chkconfig docker on
#LCTT 译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:
systemctl start docker.service
systemctl enable docker.service
- 1
- 2
- 3
- 4
- 5
- 6
Use Docker China Accelerator
vi /etc/docker/daemon.json
#添加后:
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"live-restore": true
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Restart docker
systemctl restart docker
- 1
Enter docker version
returns the version information is properly installed.
Install the JDK
yum -y install java-1.8.0-openjdk*
- 1
Configuration environment variable
to open the vim /etc/profile
Add about content
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64
export PATH=$PATH:$JAVA_HOME/bin
- 1
- 2
After modifications are complete, to take effect
source /etc/profile
- 1
Enter java -version
returns the version information is properly installed.
MAVEN installation
download:http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
## 解压
tar vxf apache-maven-3.5.2-bin.tar.gz
## 移动
mv apache-maven-3.5.2 /usr/local/maven3
- 1
- 2
- 3
- 4
Modify environment variables, in /etc/profile
, add the following lines
MAVEN_HOME=/usr/local/maven3
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin
- 1
- 2
- 3
Remember execution source /etc/profile
environment variables to take effect.
Enter mvn -version
returns the version information is properly installed.
So that the entire build environment to configure complete.
Use Docker deployment Spring Boot project
The program spring-boot-docker
copies the server, packaged into the project path under test.
#打包
mvn package
#启动
java -jar target/spring-boot-docker-1.0.jar
- 1
- 2
- 3
- 4
After seeing the Spring Boot boot environment configuration log shows that there is no problem, then we use DockerFile build a mirror.
mvn package docker:build
- 1
The first building may be a bit slow, when you see the following when building a successful show:
...
Step 1 : FROM openjdk:8-jdk-alpine
---> 224765a6bdbe
Step 2 : VOLUME /tmp
---> Using cache
---> b4e86cc8654e
Step 3 : ADD spring-boot-docker-1.0.jar app.jar
---> a20fe75963ab
Removing intermediate container 593ee5e1ea51
Step 4 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar
---> Running in 85d558a10cd4
---> 7102f08b5e95
Removing intermediate container 85d558a10cd4
Successfully built 7102f08b5e95
[INFO] Built springboot/spring-boot-docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 54.346 s
[INFO] Finished at: 2018-03-13T16:20:15+08:00
[INFO] Final Memory: 42M/182M
[INFO] ------------------------------------------------------------------------
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
Use docker images
the command to view the constructed image:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
springboot/spring-boot-docker latest 99ce9468da74 6 seconds ago 117.5 MB
- 1
- 2
- 3
springboot/spring-boot-docker
Is that we build a good image, the next step is to run the Mirror
docker run -p 8080:8080 -t springboot/spring-boot-docker
- 1
After the startup is complete we use docker ps
to view mirror are running:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
049570da86a9 springboot/spring-boot-docker "java -Djava.security" 30 seconds ago Up 27 seconds 0.0.0.0:8080->8080/tcp determined_mahavira
- 1
- 2
- 3
We can see the vessel being built in the running, visit your browser: http://192.168.0.x:8080/
return
Hello Docker!
- 1
Instructions Docker deployment Spring Boot project success!
reference
The Boot Docker with the Spring
Docker: the Spring application release to the Boot Docker