Detailed explanation of SpringBoot (2.3) application to make 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

In the foregoing , quickly experience the official recommended docker image production program, but also had a few questions:

  1. What is the difference between the recommended image construction scheme of SpringBoot-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?

The goal of this article is to answer the above questions, continue to fill up knowledge points in the process of finding answers, and improve yourself;

Key knowledge points: mirror layer

What is the mirror layer mentioned many times in the previous article, and why is there a multi-layer layer? It is necessary to consolidate this knowledge point first, please refer to the article "Why do the SpringBoot-2.3 mirroring scheme have multiple layers"

The official solution of the old version of SpringBoot

Take SpringBoot-2.2.0.RELEASE version as an example, the official document (
https://docs.spring.io/spring-boot/docs/2.2.0.RELEASE/reference/pdf/spring-boot-reference.pdf) gives The approach is as follows:

  1. Compile and build the SpringBoot project, and get the jar in the target directory;
  2. Create a dependency folder in the target directory;
  3. Unzip the jar to the dependency folder;
  4. Write the Dockerfile file, the content is as follows:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.example.MyApplication"]
  1. It can be seen that the official recommended approach is to decompress the entire jar file and use the COPY command to copy it multiple times in the Dockerfile. The benefits of this are obvious: multiple layers, if only the application code is modified in the new version of the image, then when downloading the image Only download the /app layer, and the other parts directly use the local cache, which is a conventional optimization method for docker mirroring;
  2. The above scheme has a small problem: trouble! ! !
  3. So the 2.3.0.RELEASE version has made some optimizations to make things easier;

2.3.0. The difference between the RELEASE version scheme and the old version

The steps to build Docker for version 2.3.0.RELEASE are as follows:

  1. Add a configuration item to the spring-boot-maven-plugin plugin in pom.xml;
    2. Compile and build to generate jar;
  2. Write a Dockerfile, which uses multi-stage builds. After extracting the split from the jar with tools, execute the COPY command multiple times to put the split content into the mirror to achieve the purpose of multiple layers;

Therefore, compared with the old version, the 2.3.0.RELEASE version has the following changes:

  1. There is an additional parameter in pom.xml ;
  2. After the jar is built, there is no need to unzip the jar yourself;
  3. The content of Dockefile is different. In the old version, the jar is manually decompressed and then copied separately in the Dockerfile. 2.3.0.RELEASE is to extract each part of the content from the jar through the java command ;

After clarifying the difference between the new and the old version, let's continue to study the next question;

New parameters of the spring-boot-maven-plugin plugin in pom.xml

  1. The new parameters of the pring-boot-maven-plugin plug-in are shown in the following figure:
    Insert picture description here
  2. What is the use of the above parameters? I compiled and built the jar twice here. The first time I had the above parameters, the second time I didn’t. After decompressing the two generated jars, I found that after using the above parameters, the generated jar will appear in the red box below. The two files:
    Insert picture description here
  3. Look at the contents of the layers.idx file, as shown below:
    Insert picture description here
  4. What do the contents in the above picture mean? The official has given a detailed explanation, as shown in the red box below:
    Insert picture description here
  5. To sum up, the layers.idx file is a list, which records all the information to be copied to the mirror. Next, let’s see how to use the layers.idx file. This involves another file added to the jar package: spring-boot-jarmode-layertools-2.3.0.RELEASE.jar

spring-boot-jarmode-layertools工具

  1. As mentioned earlier, in addition to layers.idx, there is another file in the jar: spring-boot-jarmode-layertools-2.3.0.RELEASE.jar , let’s take a look at the usefulness of this file;
  2. Enter the target directory of the project , which is the compiled jar file (the file name here is dockerlayerdemo-0.0.1-SNAPSHOT.jar), note that the spring-boot-maven-plugin plug -in at this time is brought with the red picture below The parameters of the box:
    Insert picture description here
  3. Execute the following commands:
java -Djarmode=layertools -jar dockerlayerdemo-0.0.1-SNAPSHOT.jar list
  1. The result is shown in the figure below, which is the content of the layers.idx file:
    Insert picture description here
  2. Let 's take a look at the official explanation of this layertools . We have already experienced the function of the list parameter above. The focus is on the extract parameter in the red box . Its function is to extract the content needed to build the image from the jar:
    Insert picture description here
  3. Seeing this, do you think of the content of the Dockerfile in "Experiencing SpringBoot (2.3) Application Making Docker Image (Official Solution)" , please see the red box and red letter in the figure below, do you have a feeling of sudden realization: jar build list layers .idx, Dockerfile extracts the files from the jar according to the list and puts them into the mirror: So
    Insert picture description here
    far, the answers to the three questions have been found. To summarize:

What is the difference between the mirror construction scheme recommended by SpringBoot-2.3.0.RELEASE and the old version?

  1. Add a configuration item to the spring-boot-maven-plugin plugin in pom.xml;
  2. After the jar is built, the old version needs to decompress the jar by itself, the new version does not need;
  3. In the new version of the jar, there are more file lists layers.idx and image file processing tool spring-boot-jarmode-layertools-2.3.0.RELEASE.jar ;
  4. The content of the old version of the Dockefile: because the previous decompression is done, all the previously decompressed content is directly copied in the Dockerfile, there is a risk here: the file location of the previous decompression and the current copy must be the same;
  5. The new version of the Dockerfile content: use the tool spring-boot-jarmode-layertools-2.3.0.RELEASE.jar, extract the files from the jar according to the layers.idx content, and copy them to the mirror;
  6. In the new version of Dockerfile, due to the use of staged construction, the operation of extracting files from the jar will not be saved to the mirrored layer;

What are the new parameters of spring-boot-maven-plugin in pom.xml?

The new parameters of the spring-boot-maven-plugin plug-in make two more files in the jar obtained by compiling and building, as shown in the following figure:
Insert picture description here

In the Dockerfile, what does the operation java -Djarmode=layertools -jar application.jar extract mean?

  1. The function of java -Djarmode=layertools -jar application.jar extract is to extract files from the jar, which are part of the docker image;
  2. The parameter of the above operation is extract , and there are two other parameters. The official explanation of their role is as follows: So
    Insert picture description here
    far, the problem has been clarified, I believe you have enough understanding of the official SpringBoot-2.3.0.RELEASE image construction plan, and the last is I drew a flowchart based on my own knowledge to help you quickly understand the entire construction process:
    Insert picture description here

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