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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.itweknow</groupId>
    <artifactId>springboot-docker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboot-docker</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Add a HelloController.java

@RestController
public class HelloController {

    @RequestMapping("/hello")
    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.

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.0</version>
    <configuration>
        <!-- Here is the final generated docker Mirror Name -->
        <imageName>itweknow/${project.artifactId}</imageName>
        <!-- Base Mirror, run one springboot Applications require only basic java Environment is OK -->
        <baseImage>java:8</baseImage>
        <!-- docker Commands to execute at startup -->
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <resources>
        <resource>
            <targetPath>/</targetPath>
            <directory>${project.build.directory}</directory>
            <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

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        0.0.0.0:8080->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.

Summary

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