The development of Docker technology provides a more convenient environment for micro services to land on the ground. Deploying Spring Boot with Docker is actually very simple. Let's take a look at this article.
First, build a simple Spring Book project, then add Docker support to the project, and finally deploy the project.
A simple Spring Book project
In pom.xml, use Spring Boot 2.0 dependencies
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent>
Adding 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 with an index() method that returns when accessed: Hello Docker!
@RestController public class DockerController { @RequestMapping("/") public String index() { return "Hello Docker!"; } }
Startup class
@SpringBootApplication public class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); } }
Start the project after adding, browser questions after successful start: http://localhost:8080/, page return: Hello Docker!, indicating that the Spring Book project configuration is normal.
Adding Docker support to Spring Book project
Add the Docker image name to pom.xml-properties
<properties> <docker.image.prefix>springboot</docker.image.prefix> </properties>
Add Docker build plug-in to plugins:
<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>
Create a Docker file under the directory src/main/docker, which 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"]
This Dockerfile file is very simple. It builds the Jdk base environment, adds Spring Boot Jar to the image, and explains briefly:
- FROM, which means using the Jdk8 environment as the base image, will be downloaded from DockerHub if the image is not local
- VOLUME, VOLUME points to a / tmp directory. Because Spring Boot uses the built-in Tomcat container, Tomcat defaults to / tmp as the working directory. The effect of this command is to create a temporary file in the host's / var/lib/docker directory and link it to the container's / tmp directory
- ADD, copy files and rename them
- ENTRYPOINT, in order to shorten the starting time of Tomcat, add the system attribute of java.security.egd to point to / dev/urandom as ENTRYPOINT
This completes the Spring Book project by adding Docker dependencies.
Building a Packaging Environment
We need a Docker environment to package the Spring Book project. It's very difficult to build a Docker environment on Windows, so I'll take Centos 7 as an example.
Installing Docker Environment
install
yum install docker
After installation, use the following command to start the docker service and set it to boot:
service docker start chkconfig docker on #LCTT Note: The old sysv grammar is used here, such as the new system D grammar supported in CentOS 7, as follows: systemctl start docker.service systemctl enable docker.service
Using Docker China Accelerator
vi /etc/docker/daemon.json #After adding: { "registry-mirrors": ["https://registry.docker-cn.com"], "live-restore": true }
Restart docker
systemctl restart docker
Enter docker version and return version information to install properly.
Installing JDK
yum -y install java-1.8.0-openjdk*
Configure environment variables to open vim/etc/profile and add something
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
After the modification is completed, make it effective
source /etc/profile
Enter java-version and return version information to install properly.
Installation of MAVEN
Download: http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
## decompression tar vxf apache-maven-3.5.2-bin.tar.gz ## move mv apache-maven-3.5.2 /usr/local/maven3
Modify the environment variables and add the following lines to / etc/profile
MAVEN_HOME=/usr/local/maven3 export MAVEN_HOME export PATH=${PATH}:${MAVEN_HOME}/bin
Remember to execute source/etc/profile to make environment variables valid.
Enter mvn-version and return version information to install properly.
This completes the configuration of the entire build environment.
Deploying Spring Book Project with Docker
Put the project spring-boot-docker copy server into the project path for packaging testing.
#Pack mvn package #start-up java -jar target/spring-boot-docker-1.0.jar
Seeing Spring Boot's startup log shows that the environment configuration is okay, let's use DockerFile to build the image.
mvn package docker:build
The first build may be a bit slow, indicating success when you see the following:
... 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] ------------------------------------------------------------------------
Use the docker images command to view the built image:
docker images REPOSITORY TAG IMAGE ID CREATED SIZE springboot/spring-boot-docker latest 99ce9468da74 6 seconds ago 117.5 MB
springboot/spring-boot-docker is the mirror we built, and the next step is to run it.
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 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
You can see that the container we built is running, access the browser: http://192.168.0.x:8080/, return
Hello Docker!
Explain the success of deploying Spring Boot project with Docker!