Previously, we used Docker to build services related to the development environment, and also to build dependency services in the development and testing environment, and then also used Nexus to build Docker’s self-service, so this time we came together to deploy SpringBoot applications with Docker. Step by step to all the Dockerization march.

Build SpringBoot project

The project is relatively simple, no data interaction, no complex business, just a print statement.

Using any method you are familiar with, create a Maven project, modify the pom.xml file and add SpringBoot dependencies.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
    </parent>
    <groupId>net.mooko.demos</groupId>
    <artifactId>docker-spring-boot</artifactId>
    <version>0.1.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

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

The following is the content of the Application file, including the Controller together with the definition.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package net.mooko.demos.dockerspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @GetMapping("/")
    public String home() {
        return "Hello docker world";
    }
}

At this point, execute the application, visit http://localhost:8080/ through your browser, and you will see the following sentence printed on the screen.

1
Hello docker world

Using Docker to package images

Add Maven plug-ins to package Docker images through Maven commands, currently used more, there are three plug-ins.

  • spotify/docker-maven-plugin: an early Maven plugin, you can not have DockerFile, all the parameters are configured in pom.xml, and then the author felt that this is not good, and wrote a new one, but also we choose this dockerfile-maven-plugin.
  • fabric8io/docker-maven-plugin: super powerful, but the use, but also some trouble, interested in trying.
  • spotify/dockerfile-maven-plugin: new version of spotify/docker-maven-plugin, easier to use, write your own DockerFile, after that you can automatically package and publish.

Add Dockerfile file to the project root directory

1
2
3
4
FROM openjdk:8-jdk-alpine
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]
EXPOSE 8080

The content is relatively simple.

  • Use OpenJdk as the base image
  • Copy the jar package under Target as app.jar
  • Execute the command
  • Expose the port

Modify the plugins section of the pom.xml configuration file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>dockerfile-maven-plugin</artifactId>
        <version>1.4.13</version>
        <executions>
            <execution>
                <id>default</id>
                <goals>
                    <goal>build</goal>
                    <goal>push</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <username>puras</username>
            <password>123456</password>
            <repository>10.211.55.8:6000/${project.artifactId}</repository>
            <tag>${project.version}</tag>
            <buildArgs>
                <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
            </buildArgs>
        </configuration>
    </plugin>
</plugins>

Introduce the new plugin in, the specific configuration can be found in https://github.com/spotify/dockerfile-maven, where.

  • username and password are the account and password of your Docker self-service, which can be ignored if your self-service is allowing anyone to upload
  • repository is the address prefix of your server, plus the name of your Docker image
  • tag is latest by default if not specified

After adding the above plugins, you can automatically generate the Docker image when you execute the package command again. If you don’t want this effect, you can comment out goal.

1
mvn clean package

According to the official statement, with this plugin, you don’t need to execute it step by step, just execute deploy and you can publish it with one click, but I have some problems here, if it doesn’t work once, then execute it multiple times.

This will allow us to publish our SpringBoot application to the self-service repository. Pull this image from another environment or repull it yourself, run it and visit http://localhost:8080 and you will see the following.

1
Hello docker world

Reasons to recommend

  • Use a unified environment, packaged and ready to execute in any Docker
  • Testers no longer need to install JDK and other such dependencies, get the image can be deployed directly, reducing the problems caused by the environment
  • Combined with Jenkins for automatic deployment, making automatic build more convenient