Deploy Spring Boot project in Docker

Microservices are now very popular in Internet companies, and many HR phone interviews before job hunting asked if they had any contact with microservices.Micro-service and Docker can be perfectly combined to make the architecture of micro-service more convenient.As the SpringBoot framework for microservices, today we'll look at how to run a SpringBoot application in a Docker container.

Create Spring Boot Program

In this article, we'll run a simple SpringBoot Web application in the Docker container, which is shown below in the initial pom.xml.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


    <description>Demo project for Spring Boot</description>

        <relativePath/> <!-- lookup parent from repository -->





Add a

public class HelloController {

    public String hello() {
        return "Hello Docker.";

Okay, so far we've built a simple web application that runs locally and looks to make sure it's correct.

Configure Docker

Docker provides the maven-built plugin docker-maven-plugin, which we only need to add in our pom.xml and then do some simple configuration to OK.

        <!-- Here is the final generated docker Mirror Name -->
        <!-- Base Mirror, run one springboot Applications require only basic java Environment is OK -->
        <!-- docker Commands to execute at startup -->
        <entryPoint>["java", "-jar", "/${}.jar"]</entryPoint>

That's right. By now our entire project has been built. The rest of the work is to copy the project into our linux environment to generate a docker image and run it.

Build Mirror

Once the copy project is under linux, it enters the project directory and executes the following commands in sequence to generate a docker image.

mvn clean
# -Dmaven.test.skip=true is skip test code
mvn package -Dmaven.test.skip=true
mvn docker:build

Of course, you can also execute three commands together

mvn clean package docker:build -Dmaven.test.skip=true

Then execute docker images to see the docker image in the system for success.

REPOSITORY                                           TAG                 IMAGE ID            CREATED             SIZE
itweknow/springboot-docker                           latest              f03b689cfc33        10 seconds ago      660MB

Run Docker Container

# -d is the specified background operation
# --name is the specified container name
# -p 8080:8080 refers to the mapping of port 8080 of a container to port 8080 of a host in the format host (host) port: container port
docker run -d --name test -p 8080:8080 itweknow/springboot-docker

Execute docker ps to see the running container

CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                    NAMES
652fd3ccac89        itweknow/springboot-docker   "java -jar /springbo..."   3 seconds ago       Up 2 seconds>8080/tcp   test

Our project has successfully run in the docker container and we can test it by visiting http://virtual machine IP:8080/hello.


This article is just a brief introduction to how to deploy our Spring Boot project with Docker. We will slowly learn about Docker later.This article Full implementation You can find it on Github.

PS: Learning never stops, never stops!If you like my articles, pay attention to me!

Keywords: PHP Docker SpringBoot Maven Spring

Added by andymt on Fri, 02 Aug 2019 04:11:13 +0300