Experience SpringBoot (2.3) application making Docker image (official solution)

About the "SpringBoot-2.3 Containerization Technology" series

  • The "SpringBoot-2.3 Containerization Technology" series aims to learn and practice the latest containerization technology brought by version 2.3 together with everyone, so that our Java applications can be more adapted to the containerized environment, keeping up with the mainstream in the cloud computing era, and maintaining competitiveness ;
  • The whole series of articles is divided into two parts, the topic and the auxiliary part, the topic part is as follows:
  1. "Experiencing SpringBoot (2.3) Application Making Docker Image (Official Solution)" ;
  2. "Detailed explanation of SpringBoot (2.3) application making Docker image (official solution)" ;
  3. "Mastering SpringBoot-2.3 Container Probes: Basics" ;
  4. "Mastering SpringBoot-2.3 Container Probes: In-Depth Chapter" ;
  5. "Mastering the Container Probe of SpringBoot-2.3: Actual Combat" ;
  • The auxiliary part is a summary of some reference materials and memos, as follows:
  1. "Why do SpringBoot-2.3 mirroring solutions have multiple layers" ;
  2. "Set a non-root account to directly execute docker commands without sudo" ;
  3. "Development phase, rapid deployment of SpringBoot applications to K8S" ;

Introduction

  1. The theme of this article is hands-on combat, and strive to experience the official mirror production plan as quickly as possible;
  2. This article will not involve theoretical knowledge, these are left to the next chapter;
  3. Many questions may arise after one pass. There are a few typical questions left at the end of the article. You can think about it before proceeding to the next chapter to reveal;

SpringBoot application source code

  1. This actual combat uses a normal SpringBoot project. If you don't want to write code, the source code can be downloaded on GitHub. The address and link information are shown in the following table (https://github.com/zq2599/blog_demos):
name link Remarks
Project homepage https://github.com/zq2599/blog_demos The project's homepage on GitHub
git warehouse address (https) https://github.com/zq2599/blog_demos.git The warehouse address of the project source code, https protocol
git warehouse address (ssh) [email protected]:zq2599/blog_demos.git The warehouse address of the source code of the project, ssh protocol
  1. There are multiple folders in this git project. The application of this chapter is under the dockerlayerdemo folder, as shown in the red box in the following figure:
    Insert picture description here

Version Information

  1. SpringBoot:2.3.0.RELEASE
  2. JDK:1.8.0_121
  3. Maven:3.3.9
  4. Docker:19.03.8
  5. Operating system: MacBook pro 13 inches, macOS Catalina 10.15.4

Build a mirror image

  1. Modify pom.xml to add sub-nodes to the configuration of spring-boot-maven-plugin plug-in, the value of enabled is true , as shown in the red box in the following figure:
    Insert picture description here
  2. Add a Dockerfile file in the directory where the pom.xml file is located , with the following content:
# 指定基础镜像,这是分阶段构建的前期阶段
FROM openjdk:8u212-jdk-stretch as builder
# 执行工作目录
WORKDIR application
# 配置参数
ARG JAR_FILE=target/*.jar
# 将编译构建得到的jar文件复制到镜像空间中
COPY ${JAR_FILE} application.jar
# 通过工具spring-boot-jarmode-layertools从application.jar中提取拆分后的构建结果
RUN java -Djarmode=layertools -jar application.jar extract

# 正式构建镜像
FROM openjdk:8u212-jdk-stretch
WORKDIR application
# 前一阶段从jar中提取除了多个文件,这里分别执行COPY命令复制到镜像空间中,每次COPY都是一个layer
COPY --from=builder application/dependencies/ ./
COPY --from=builder application/spring-boot-loader/ ./
COPY --from=builder application/snapshot-dependencies/ ./
COPY --from=builder application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
  1. Execute the following command to compile and build the project:
mvn clean package -U -DskipTests
  1. After compiling and building, make sure that there are jar files generated in the target directory ;
  2. In the directory where the Dockerfile is located, execute the following command to build the image (please adjust the image name according to your actual situation):
docker build -t dockerlayerdemo:0.0.1 .
  1. The prompt for successful mirroring is as follows:
    Insert picture description here

verification

  1. Execute the following commands to create and start the container:
docker run --rm -p 8080:8080 dockerlayerdemo:0.0.1
  1. Console information for successful startup:
    Insert picture description here
  2. Browser access: http://localhost:8080/hello, as shown below, everything is normal:
    Insert picture description here
  3. Look at the hierarchical information of the image and execute the command:
docker history dockerlayerdemo:0.0.1
  1. As shown in the figure below, the contents of the entire jar, such as classes, dependent libraries, dependent resources, etc., are copied to the mirror space multiple times, so if you only change the class in the future, when you update the mirror, you only need to download the layer of the class ( Other layers can directly use the previously cached locally):
    Insert picture description here

Questions left

At this point, the mirror construction method officially recommended by SpringBoot-2.3.0.RELEASE has been implemented, but some questions remain:

  1. What is the difference between the recommended image construction scheme of version 2.3 and the old version?
  2. What exactly did the new parameters of spring-boot-maven-plugin in pom.xml do?
  3. In the Dockerfile, what does the operation java -Djarmode=layertools -jar application.jar extract mean?

These issues are left to the next article for further study.

Reference Information

Official document address:
https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/reference/pdf/spring-boot-reference.pdf, chapter 4.31

Welcome to my GitHub

Welcome to follow my public account: programmer Xin Chen

Insert picture description here

Guess you like

Origin blog.csdn.net/boling_cavalry/article/details/106597358