How to deploy SpringBoot project using Docker

How to deploy SpringBoot project using Docker

The development of Docker technology provides a more convenient environment for the implementation of microservices. It is actually very simple to deploy Spring Boot using Docker. Let's learn it briefly in this article.

First, build 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 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 successful startup, visit http://localhost:8080/ in the browser. The page returns: Hello Docker!, indicating that the Spring Boot project is configured normally.

Spring Boot project adds Docker support

Add the Docker image name in pom.xml-properties

<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 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 Dockerfile file in the directory src/main/docker. 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 is very simple. It builds the JDK basic environment and adds the Spring Boot Jar to the image. Here is a brief explanation:

  • FROM means using the Jdk8 environment as the base image. If the image is not local, it will be downloaded from DockerHub.
  • VOLUME, VOLUME points to a /tmp directory. Since Spring Boot uses the built-in Tomcat container, Tomcat uses /tmp as the working directory by default. The effect of this command is: 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 it
  • ENTRYPOINT, to shorten Tomcat startup time, add the java.security.egd system property to point to /dev/urandom as ENTRYPOINT

This completes adding Docker dependencies to the Spring Boot project.

Build a packaging environment

We need a Docker environment to package the Spring Boot project. It is troublesome to build a Docker environment on Windows, so I will take Centos 7 as an example.

Install Docker Environment

Install

yum install docker

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

service docker start
chkconfig docker on
 
#LCTT Annotation: The old sysv syntax is used here. For example, the new systemd syntax supported in CentOS 7 is 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. If the version information is returned, the installation is successful.

Install JDK

yum -y install java-1.8.0-openjdk*

Configure environment variables Open vim /etc/profile and add the following 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

After the modification is completed, make it effective

source /etc/profile

If the version information is returned when you enter java -version, the installation is successful.

Installing MAVEN

Download: http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz

## Unzip 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 the environment variables take effect.

Enter mvn -version and if the version information is returned, the installation is successful.

This completes the configuration of the entire build environment.

Deploy Spring Boot project using Docker

Copy the spring-boot-docker project to the server and enter the project path for packaging and testing.

#Packaging mvn package
#Start java -jar target/spring-boot-docker-1.0.jar

After seeing the Spring Boot startup log, it shows that there is no problem with the environment configuration. Next, we use DockerFile to build the image.

mvn package docker:build

The first build may be a bit slow, and the build is successful 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 image we built. The next step is to run the image.

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

After the 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!

This shows that the Spring Boot project was successfully deployed using Docker!

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to deploy SpringBoot project using Dockerfile
  • Example of deploying Spring Boot application using Docker
  • How to deploy spring-boot maven application using Docker
  • Deploy the springboot project to docker based on idea
  • Deploy springBoot project to Docker on Mac (demo)
  • How to deploy microservices using Spring Boot and Docker
  • Steps to deploy Spring Boot project using Docker

<<:  Implementation of multi-environment configuration (.env) of vue project

>>:  5 ways to migrate from MySQL to ClickHouse

Recommend

Docker generates images through containers and submits DockerCommit in detail

Table of contents After creating a container loca...

js to achieve image fade-in and fade-out effect

This article shares the specific code of js to ac...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

Simple usage example of MySQL 8.0 recursive query

Preface This article uses the new features of MyS...

Timeline implementation method based on ccs3

In web projects we often use the timeline control...

Explanation of the execution priority of mySQL keywords

As shown below: from table where condition group ...

Example code of html formatting json

Without further ado, I will post the code for you...

Detailed discussion of several methods for deduplicating JavaScript arrays

Table of contents 1. Set Deduplication 2. Double ...

Understanding of web design layout

<br />A contradiction arises. In small works...

Looping methods and various traversal methods in js

Table of contents for loop While Loop do-while lo...

Detailed steps to install and uninstall Apache (httpd) service on centos 7

uninstall First, confirm whether it has been inst...

Right align multiple elements in the same row under div in css

Method 1: float:right In addition, floating will ...

Detailed steps for smooth transition from MySQL to MariaDB

1. Introduction to MariaDB and MySQL 1. Introduct...