Implementation method of deploying SpringBoot project with Docker
- 2021-07-18 09:34:04
- OfStack
The development of Docker technology provides a more convenient environment for micro-service landing. It is actually very simple to deploy Spring Boot with Docker. Let's learn this article briefly.
First build a simple Spring Boot project, then add Docker support to the project, and finally deploy the project.
1 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>
Creates an DockerController with an index () method that returns: 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);
}
}
After adding, start the project. After starting successfully, the browser visits: http://localhost: 8080/, and the page returns: Hello Docker! The Spring Boot project is configured correctly.
Spring Boot project adds Docker support
Add Docker mirror name in pom. xml-properties
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>
Add the 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 the Dockerfile file under the directory src/main/docker, and the Dockerfile file is used to explain 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, build the basic environment of Jdk, add Spring Boot Jar to the image, and explain briefly 1:
FROM means using Jdk8 environment as the basic image. If the image is not local, it will be downloaded from DockerHub VOLUME and VOLUME point to the/tmp directory. Since Spring Boot uses the built-in Tomcat container, Tomcat defaults to/tmp as its working directory. The effect of this command is to create a temporary file in the/var/lib/docker directory of the host and link it to the/tmp directory in the container ADD, copy file and rename ENTRYPOINT, in order to shorten the startup time of Tomcat, add the system attribute of java. security. egd pointing to/dev/urandom as ENTRYPOINTThis completes the Spring Boot project adding the Docker dependency.
Building a Packaging Environment
We need to have an Docker environment to package the Spring Boot project. It is very troublesome to build an Docker environment in Windows, so I take Centos 7 as an example here.
Installing the Docker environment
Installation
yum install docker
After the installation is complete, start the docker service with the following command and set it to boot:
service docker start
chkconfig docker on
#LCTT The old style is adopted here sysv Grammar, such as using CentOS 7 New styles supported in systemd Syntax, 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
<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>
0
Enter docker version to return version information and install normally.
Installing JDK
<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
Configure the environment variable to open vim/etc/profile Add 1
<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>
2
After the modification is completed, make it effective
source /etc/profile
Enter java-version to return version information and install normally.
Installing MAVEN
Download: http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5. 2/binaries/apache-maven-3. 5.2-bin.tar.gz
<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>
4
Modify the environment variable to add the following lines in/etc/profile
<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>
5
Remember to execute source/etc/profile to validate the environment variables.
Enter mvn-version to return version information and install normally.
In this way, the whole build environment is configured.
Deploying the Spring Boot project with Docker
Copy the project spring-boot-docker to the server, and enter the project path for packaging test.
<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>
6
After seeing the startup log of Spring Boot, it shows that there is no problem with the environment configuration. Next, we use DockerFile to build the image.
<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>
7
The first build may be a little slow, and it is a 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:
<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>
9
springboot/spring-boot-docker is the image we built, and the next step is to run it
docker run -p 8080:8080 -t springboot/spring-boot-docker
After startup 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, visit the browser: http://192.168. 0. x: 8080/, and return
Hello Docker!
Explain the successful deployment of Spring Boot project using Docker!
Sample code: https://github.com/ityouknow/spring-boot-examples