SoFunction
Updated on 2025-03-09

Implementation of Spring Boot using Docker

The development of Docker technology has provided a more convenient environment for the implementation of microservices. It is actually very simple to deploy Spring Boot using 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.

A simple Spring Boot project

In , use Spring Boot 2.0 related dependencies

<parent>
  <groupId></groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.</version>
</parent>

Add web and test dependencies

<dependencies>
   <dependency>
  <groupId></groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

Create a DockerController, there is an index() method in it, and it returns: Hello Docker when accessed!

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

Startup class

@SpringBootApplication
public class DockerApplication {

  public static void main(String[] args) {
    (, args);
  }
}

After adding, start the project. After successful startup, the browser will ask: http://localhost:8080/, the page returns: Hello Docker!, which means that the Spring Boot project configuration is normal.

Add Docker support for Spring Boot projects

Add Docker image name in -properties

<properties>
  <>springboot</>
</properties>

Add Docker build plugins to plugins:

<build>
  <plugins>
    <plugin>
      <groupId></groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <!-- Docker maven plugin -->
    <plugin>
      <groupId></groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>1.0.0</version>
      <configuration>
        <imageName>${}/${}</imageName>
        <dockerDirectory>src/main/docker</dockerDirectory>
        <resources>
          <resource>
            <targetPath>/</targetPath>
            <directory>${}</directory>
            <include>${}.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 illustrate how to build an image.

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-boot-docker-1. 
ENTRYPOINT ["java","-=file:/dev/./urandom","-jar","/"]

This Dockerfile file is very simple. To build the Jdk basic environment, add Spring Boot Jar to the image, let's briefly explain:

  • FROM means using Jdk8 environment as the basic 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 , in order to shorten the startup time of Tomcat, the added system attribute points to /dev/urandom as ENTRYPOINT

In this way, adding Docker dependencies to the Spring Boot project is completed.

Build a packaging environment

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

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 boot:

service docker start
chkconfig docker on

#LCTT Translation Note: The old sysv syntax is adopted here, such as the new systemd syntax supported in CentOS 7, as follows:systemctl start 
systemctl enable 

Using Docker China Accelerator

vi /etc/docker/

#After adding:{
  "registry-mirrors": [""],
  "live-restore": true
}

Restart docker

systemctl restart docker

Enter docker version to return version information and install it normally.

Install JDK

yum -y install java-1.8.0-openjdk*

Configure environment variables Open vim /etc/profile to add 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 take effect

source /etc/profile

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

Install MAVEN

download:/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.

## Decompressiontar vxf apache-maven-3.5.
## movemv apache-maven-3.5.2 /usr/local/maven3

Modify the environment variables and add the following lines in /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 variable take effect.

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

In this way, the entire build environment is configured.

Deploy Spring Boot Projects with Docker

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

#Packmvn package
#start upjava -jar target/spring-boot-docker-1.

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.

mvn package docker:build

The first build may be a bit slow, and it indicates that 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. 
 ---> a20fe75963ab
Removing intermediate container 593ee5e1ea51
Step 4 : ENTRYPOINT java -=file:/dev/./urandom -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, and the next step is to run the image

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

After the startup is completed, 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 -"  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.:8080/, return

Hello Docker!

Instructions Deploying Spring Boot project using Docker was successful!

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.