Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The process of making Docker Image with SpringBoot (2.3)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about the process of making Docker images for SpringBoot applications. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Key knowledge points: the official solution of the old version of SpringBoot for mirroring layer

Take the 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)) shows the following:

Compile and build the SpringBoot project, and get jar in the target directory

Create a new dependency folder in the target directory

Extract jar to the dependency folder

Write a Dockerfile file as follows:

FROM openjdk:8-jdk-alpineVOLUME / tmpARG DEPENDENCY=target/dependencyCOPY ${DEPENDENCY} / BOOT-INF/lib / app/libCOPY ${DEPENDENCY} / META-INF / app/META-INFCOPY ${DEPENDENCY} / BOOT-INF/classes / appENTRYPOINT ["java", "- cp", "app:app/lib/*", "com.example.MyApplication"]

It can be seen that the officially recommended practice is to extract the entire jar file and copy it separately with the COPY command many times in Dockerfile. The benefits are obvious: for multiple layer, if only the application code is modified in the new version of the image, then the layer / app will only be downloaded when downloading the image, and the other parts will directly use the local cache, which is the conventional optimization method of docker image.

There is a small problem with the above scheme: trouble!

So the 2.3.0.RELEASE version made some optimizations to make things easier.

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

The steps for building Docker with 2.3.0.RELEASE version are as follows:

The spring-boot-maven-plugin plug-in in pom.xml adds a configuration item; 2. Compile and build to generate jar

Write Dockerfile, which uses multi-stage build (multi-stage builds). After extracting the split from the jar with the tool, execute the COPY command many times to put the split content into the mirror image to achieve the purpose of multiple layer

Therefore, the 2.3.0.RELEASE version has the following changes compared to the previous version:

There is an extra parameter in pom.xml.

After building the jar, you do not need to decompress the jar yourself.

The content of Dockefile is different. In the old version, the jar was extracted manually and then copied separately in Dockerfile. 2.3.0.RELEASE extracted each part of the content from jar through the java command.

Having figured out the difference between the old and new versions, let's move on to the next question.

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

The new parameters for the pring-boot-maven-plugin plug-in are shown below:

two。 What is the use of the above parameters? I have compiled and built jar twice. The first time has the above parameters and the second time does not. After decompressing and comparing the two generated jar, it is found that after using the above parameters, the generated jar will be more than the two files in the red box below:

Take a look at the contents of the layers.idx file, as shown below:

What does the content in the picture above mean? Officials have given a detailed explanation, as shown in the red box below:

To sum up, the layers.idx file is a list of all the information to be copied into the image. Let's take a look at how to use the layers.idx file, which involves another new file in the jar package: spring-boot-jarmode-layertools-2.3.0.RELEASE.jar.

Spring-boot-jarmode-layertools tool

I have already introduced that in addition to layers.idx, there is another file in jar: spring-boot-jarmode-layertools-2.3.0.RELEASE.jar, let's see the use of this file.

Enter the target directory of the project, where is the compiled jar file (my file name is dockerlayerdemo-0.0.1-SNAPSHOT.jar). Note that the spring-boot-maven-plugin plug-in comes with the parameters in the red box below:

Execute the following command:

Java-Djarmode=layertools-jar dockerlayerdemo-0.0.1-SNAPSHOT.jar list

The result is shown in the figure below, which is the content of the layers.idx file:

Let's take a look at the official explanation of this layertools. We have experienced the role of the list parameter above. The focus is on the extract parameter in the red box, which is used to extract the content needed to build an image from the jar:

When you see here, do you think of the content of Dockerfile in "experience SpringBoot (2.3) Application making Docker Image (official solution)"? please take a look at the red box and scarlet letter in the following figure. Do you have a sudden realization: in the jar build generation list layers.idx,Dockerfile, the image is extracted from jar according to the list:

So far, the answers to all three questions have been found, so let's make a summary:

How is the image construction scheme recommended by SpringBoot-2.3.0.RELEASE different from the old version?

The spring-boot-maven-plugin plug-in in pom.xml adds a configuration item

After building the jar, the old version needs to extract the jar by itself, but the new version does not need it.

In the new version of jar, there is a file listing layers.idx and an image processing tool spring-boot-jarmode-layertools-2.3.0.RELEASE.jar.

Old Dockefile content: because the previous unzipped content is ready, all the previously unzipped content is copied directly in the Dockerfile. There is a risk here: the previous step of unzipping should be consistent with the current copied file location.

New version of Dockerfile content: use the tool spring-boot-jarmode-layertools-2.3.0.RELEASE.jar to extract files from the jar according to the layers.idx content and copy them to the image

In the new version of Dockerfile, because phased builds are used, the operation of extracting files from jar is not saved to the mirrored layer

What exactly did the new parameters of the spring-boot-maven-plugin plug-in in pom.xml do?

The parameter added by the spring-boot-maven-plugin plug-in results in two more files in the build jar, as shown in the following figure:

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

The function of java-Djarmode=layertools-jar application.jar extract is to extract files from jar, which are part of the docker image

The parameter for the above operation is extract, and there are two other parameters, which are officially explained as follows:

So far, all the questions have been clarified. I believe you know enough about the official image construction scheme of SpringBoot-2.3.0.RELEASE. Finally, I draw a flow chart based on my own understanding to help you quickly understand the whole construction process:

The above is the process of making a Docker image for the SpringBoot application shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report