Application example of deploying Spring Boot using Docker

  • 2020-10-23 20:21:58
  • OfStack

The development of Docker technology has provided a more convenient environment for micro-service landing. Spring Boot is actually very simple to deploy using Docker, so let's take a look at this article.

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 ES16en. xml, Spring Boot 2.0 dependency is used


<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 1 DockerController, in which there is 1 index() method, return: Hello Docker!


@RestController
public class DockerController {
 
  @RequestMapping("/")
  public String index() {
    return "Hello Docker!";
  }
}

Start the class


@SpringBootApplication
public class DockerApplication {

 public static void main(String[] args) {
 SpringApplication.run(DockerApplication.class, args);
 }
}

After the project is added, the browser asks: http://localhost:8080/, and the page returns: Hello Docker! , indicating that the Spring Boot project configuration is normal.

Spring Boot project adds Docker support

Add the Docker image name to pom.xml-ES52en


<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 the Dockerfile file in the directory src/main/docker. The Dockerfile file is used to illustrate 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 Jdk base environment and add Spring Boot Jar to the image.

FROM means that the Jdk8 environment is used as the base image. If the image is not local, it will be downloaded from DockerHub VOLUME, VOLUME points to 1 /tmp directory. Since 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 /tmp directory in the container ADD, copy the file and rename ENTRYPOINT, to shorten the startup time of Tomcat, add java.security.egd's system attribute pointing to /dev/urandom as ENTRYPOINT

This completes the Spring Boot project by adding Docker dependencies.

Build the packaging environment

We need one Docker environment to package the Spring Boot project. It is very troublesome to set up Docker environment in Windows, so I take Centos 7 as an example.

Install the Docker environment

The installation


yum install docker

After the installation is complete, use the following command to start the docker service and set it to boot at boot:


ervice docker start
chkconfig docker on

#LCTT  The old style is used here  sysv  Grammar, as adopted CentOS 7 New style supported by China  systemd  Syntax, as follows: 
systemctl start docker.service
systemctl enable docker.service

Docker China accelerator was used


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 the installation is normal.

Install 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

The configuration environment variable opens vim /etc/profile to add the contents under 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 complete, make it effective


source /etc/profile

Enter java-version to return version information and the installation is normal.

Install 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 for environment variables to take effect.

Enter mvn-version to return version information and the installation is normal.

This completes the configuration of the entire build environment.

Deploy the Spring Boot project with Docker

Put the copy server of project ES196en-ES197en-ES198en into 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 for Spring Boot that the environment configuration is ok, let's build the image using DockerFile.


<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 bit slow, indicating a successful build 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/ ES217en-ES218en-ES219en is the image we built, and the next step is to run the image


docker run -p 8080:8080 -t springboot/spring-boot-docker

After startup, we used 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

Can see that we build the container is running, access to the browser: http: / / 192.168.0. x: 8080 / return


Hello Docker!

Successful deployment of Spring Boot using Docker!

Example code: https: / / github com ityouknow/spring boot -- examples


Related articles: