Build a Spring Boot application image using the Maven plugin

Use the Maven plugin to build a Docker image of a Spring Boot application.

Environmental preparation

1. Linux system
2. Install JDK, Maven
3. Install Docker

applied practice

1. Add the Dockerfile file in the application root directory

and edit the build content in the Dockerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

2. Add the docker image build plugin dockerfile-maven-plugin to the project pom.xml

<build>
    <plugins>
        <!-- 使用Maven插件直接将应用打包为一个Docker镜像 -->
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.6</version>
            <configuration>
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
                <buildArgs>
                    <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                </buildArgs>
            </configuration>
        </plugin>
    </plugins>
</build>

Apparently, 2 parameters are configured in the plugin dockerfile-maven-plugin:

  • <repository>: Specify the mirror repository information and mirror name, such as: springio/test-springboot
  • <buildArgs>: Set the build parameters, which can be used directly in the Dockerfile (but must be declared with the ARG instruction in the Dockerfile)

3. Execute the build

Execute the build in the Spring Boot application root directory:

mvn install dockerfile:build

You will see the following build log:

...
[INFO] Building Docker context /home/chench/workspace/java/test-springboot
[INFO] 
[INFO] Image will be built as springio/test-springboot:latest
[INFO] 
[INFO] Step 1/5 : FROM openjdk:8-jdk-alpine
[INFO] 
[INFO] Pulling from library/openjdk
[INFO] Image ff3a5c916c92: Pulling fs layer
[INFO] Image 5de5f69f42d7: Pulling fs layer
[INFO] Image fd869c8b9b59: Pulling fs layer
[INFO] Image 5de5f69f42d7: Downloading
[INFO] Image 5de5f69f42d7: Verifying Checksum
[INFO] Image 5de5f69f42d7: Download complete
[INFO] Image ff3a5c916c92: Downloading
[INFO] Image fd869c8b9b59: Downloading
[INFO] Image ff3a5c916c92: Verifying Checksum
[INFO] Image ff3a5c916c92: Download complete
[INFO] Image ff3a5c916c92: Extracting
[INFO] Image ff3a5c916c92: Pull complete
[INFO] Image 5de5f69f42d7: Extracting
[INFO] Image 5de5f69f42d7: Pull complete
[INFO] Image fd869c8b9b59: Verifying Checksum
[INFO] Image fd869c8b9b59: Download complete
[INFO] Image fd869c8b9b59: Extracting
[INFO] Image fd869c8b9b59: Pull complete
[INFO] Digest: sha256:e82316151c501a2a0f73b3089da8dc867816470a464fcb191db9f88c2343ad53
[INFO] Status: Downloaded newer image for openjdk:8-jdk-alpine
[INFO]  ---> 224765a6bdbe
[INFO] Step 2/5 : VOLUME /tmp
[INFO] 
[INFO]  ---> Running in 99fc0b2bbcf7
[INFO] Removing intermediate container 99fc0b2bbcf7
[INFO]  ---> 74c89740c46e
[INFO] Step 3/5 : ARG JAR_FILE
[INFO] 
[INFO]  ---> Running in 54d9bf00c9c6
[INFO] Removing intermediate container 54d9bf00c9c6
[INFO]  ---> 81d324efe92b
[INFO] Step 4/5 : COPY ${JAR_FILE} app.jar
[INFO] 
[INFO]  ---> f11354b41c33
[INFO] Step 5/5 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
[INFO] 
[INFO]  ---> Running in 74d40fc4f99c
[INFO] Removing intermediate container 74d40fc4f99c
[INFO]  ---> fbc5e8d75beb
[INFO] Successfully built fbc5e8d75beb
[INFO] Successfully tagged springio/test-springboot:latest
[INFO] 
[INFO] Detected build of image with id fbc5e8d75beb
[INFO] Building jar: /home/chench/workspace/java/test-springboot/target/test-springboot-0.0.1-dev-docker-info.jar
[INFO] Successfully built springio/test-springboot:latest
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:41 min
[INFO] Finished at: 2018-05-07T10:41:05+08:00
[INFO] Final Memory: 47M/132M
[INFO] ------------------------------------------------------------------------

It can be clearly seen from the build log that the dependency configuration of Maven is downloaded at the beginning, and then the image is customized using the build instructions in the Dockerfile, and the project root path is used as the build context.
In addition, a directory named docker will be generated in the target directory of the project:

The following files exist in the docker directory: image-id, image-name, repository, and tag, which store the newly constructed image information.

After the build is complete, check whether there is a corresponding mirror in the local mirror warehouse:

$docker image ls
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
springio/test-springboot   latest              fbc5e8d75beb        49 seconds ago      138MB

4. Verify that the image is correct

$docker run --name test-springboot -d -p 8080:8080 springio/test-springboot
438246b5f8e8ed7808b3127ed8fce867cd162f050135369c02f09cb2cdc8d49d
$docker container ps
CONTAINER ID        IMAGE                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
438246b5f8e8        springio/test-springboot   "java -Djava.securit…"   5 seconds ago       Up 4 seconds        0.0.0.0:8080->8080/tcp   test-springboot

Moreover, you can also directly docker runpass environment variables in the command to set the running environment of the Spring Boot application, such as: -e "SPRING_PROFILES_ACTIVE=test".

$docker run --name test-springboot-env2 -e "SPRING_PROFILES_ACTIVE=test" -p 8080:8080 -d springio/test-springboot

5. Push the image to the repository

There are two ways to push the built Spring Boot application image to the mirror repository:
(1) Manual push after the build is complete

mvn dockerfile:push

(2) Configure the Maven plugin to automatically push after the build is complete

<!-- 使用Maven插件直接将应用打包为一个Docker镜像 -->
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.3.6</version>
    <configuration>
        <repository>${docker.image.prefix}/${project.artifactId}</repository>
        <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
    <!-- 镜像构建完毕之后自动推送到仓库 -->
    <executions>
        <execution>
            <id>default</id>
            <phase>install</phase>
            <goals>
                <goal>build</goal>
                <goal>push</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Precautions

1. You can use the Maven plugin to easily build the Docker image of the Spring Boot application, but the core is how to write the Dockerfile build script.
2. When using the Maven plugin to build a Spring Boot application image, the application root path is used as the build context.

【参考】
https://spring.io/guides/gs/spring-boot-docker/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325585149&siteId=291194637