Docker technology has been developed to provide a more convenient environment for micro-service landing, the use of Docker deployment Spring Boot is very simple, this article we come to a simple study.
Start by building a simple Spring Boot project, then add Docker support to the project, and finally deploy the project.
A simple Spring Boot project
In pom.xml
, use Spring Boot 2.0 related dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version></parent>
Add Web and test dependencies
<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>
Create a Dockercontroller, in which there is a index()
method that is returned when accessed:Hello Docker!
@RestControllerpublic class DockerController { @RequestMapping("/") public String index() { return "Hello Docker!"; }}
Start class
@SpringBootApplicationpublic class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); }}
After the start of the project, after the successful launch, the browser puts the question: http://localhost:8080/
, the page returns: Hello Docker!
, the Spring boot project configuration is OK.
Spring Boot Project Add Docker support
pom.xml-properties
Add a Docker image name in
<properties> <docker.image.prefix>springboot</docker.image.prefix></properties>
Add the Docker Build plugin in plugins:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--Docker Mave N Plugin-<plugin> <groupId>com.spotify</groupId> <artifactid>d Ocker-maven-plugin</artifactid> <version>1.0.0</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <dockerdirec Tory>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>
Create the src/main/docker
Dockerfile file in the directory, and the Dockerfile file is used to illustrate how to build the image.
FROM openjdk:8-jdk-alpineVOLUME /tmpADD spring-boot-docker-1.0.jar app.jarENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
This Dockerfile file is simple, build the JDK infrastructure, add the Spring Boot Jar to the image, and simply explain:
- From, which indicates that the JDK8 environment is used as the base image, and if the mirror is not local, it will be downloaded from DockerHub
- VOLUME, VOLUME points to a
/tmp
directory, and because Spring Boot uses the built-in Tomcat container, Tomcat is used /tmp
as the working directory by default. The effect of this command is to /var/lib/docker
create a temporary file in the host directory and link it to the directory in the container. /tmp
- ADD, copy files and rename
- EntryPoint, in order to shorten the boot time of Tomcat, the added
java.security.egd
system attribute points to the /dev/urandom
entrypoint
This is done by adding Docker dependencies to the Spring Boot project.
Build a packaged environment
We need a Docker environment to package the Spring Boot project, and it's a hassle to build a docker environment in Windows, so here's the Centos 7 example.
Installing the Docker Environment
Installation
yum install docker
After the installation is complete, use the following command to start the Docker service and set it to boot:
ervice docker startchkconfig docker on#LCTT 译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:systemctl start docker.servicesystemctl enable docker.service
Using the Docker China accelerator
vi /etc/docker/daemon.json#添加后:{ "registry-mirrors": ["https://registry.docker-cn.com"], "live-restore": true}
Restart Docker
systemctl restart docker
Input docker version
return version information is installed properly.
Installing the JDK
yum -y install java-1.8.0-openjdk*
Configuring Environment variables
Open itvim /etc/profile
Add a bit of content
After the modification is complete, make it effective
source /etc/profile
Input java -version
return version information is installed properly.
Installing MAVEN
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
Modify the environment variables to /etc/profile
add the following lines in
MAVEN_HOME=/usr/local/maven3export MAVEN_HOMEexport PATH=${PATH}:${MAVEN_HOME}/bin
Remember to perform source /etc/profile
the environment variables in effect.
Input mvn -version
return version information is installed properly.
The entire build environment is configured to complete.
Deploying the Spring Boot project using Docker
On the project spring-boot-docker
copy server, go to the project path to package the test.
#打包mvn package#启动java -jar target/spring-boot-docker-1.0.jar
Seeing the boot log for Spring boot indicates that the environment is not in trouble, we then use DockerFile to build the image.
mvn package docker:build
The first build may be a bit slow, and when you see the following, it shows that the build was successful:
...Step 1 : FROM openjdk:8-jdk-alpine ---> 224765a6bdbeStep 2 : VOLUME /tmp ---> Using cache ---> b4e86cc8654eStep 3 : ADD spring-boot-docker-1.0.jar app.jar ---> a20fe75963abRemoving intermediate container 593ee5e1ea51Step 4 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /app.jar ---> Running in 85d558a10cd4 ---> 7102f08b5e95Removing intermediate container 85d558a10cd4Successfully 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] ------------------------------------------------------------------------
Use docker images
the commands to view the built-in mirrors:
docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEspringboot/spring-boot-docker latest 99ce9468da74 6 seconds ago 117.5 MB
springboot/spring-boot-docker
Is the image we built, the next step is to run the image
docker run -p 8080:8080 -t springboot/spring-boot-docker
After the boot is complete we use docker ps
to view the running image:
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES049570da86a9 springboot/spring-boot-docker "java -Djava.security" 30 seconds ago Up 27 seconds 0.0.0.0:8080->8080/tcp determined_mahavira
You can see that the container we are building is running, accessing the browser: http://192.168.0.x:8080/
, returning
Hello Docker!
Description successful using Docker to deploy Spring Boot project!
Sample Code-github
Sample code-Cloud Code
Reference
Spring Boot with Docker
Docker:spring Boot app released to Docker
Spring Boot 2.0 (iv): Deploy Spring boot with Docker