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

Practice and Analysis of Spring Boot layered Packaging Docker Image

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about how to carry out Spring Boot layered packaging Docker image practice and analysis, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

1. Prepare the spring boot project

Simple, skip

Spring boot version > 2.3.x is fine.

Simply wrote a hello world.

two。 Make relevant configuration

Many blog here emphasize the need to manually configure and enable layered packaging in pom.xml:

Org.springframework.boot spring-boot-maven-plugin true

Version 2.3.x needs to be manually configured and enabled because it is a new feature. However, the current spring boot version (using version 2.6.1) does not need to be manually configured, and this feature is supported by default. (according to some blog, it is enabled by default after version 2.4.x.)

The current official documentation (version 2.6.0) also supports it by default, but it needs to be manually configured when the feature is not needed:

The repackaged archive includes the layers.idx file by default. To disable this feature, you can do so in the following manner:

Org.springframework.boot spring-boot-maven-plugin false 3. Package

Execute mvn package to package and get the jar package file.

In fact, the essence is to use the repackage of the spring-boot-maven plug-in, resulting in a complete jar package containing dependencies and the original jar package (automatically adding the suffix .original)

Open the jar package and you will find that there are classpath.idx and layers.idx files in the BOOT-INF folder. Open it and have a look at it:

Classpath.idx

-"BOOT-INF/lib/logback-classic-1.2.7.jar"

-"BOOT-INF/lib/logback-core-1.2.7.jar"

-"BOOT-INF/lib/log4j-to-slf4j-2.14.1.jar"

-"BOOT-INF/lib/log4j-api-2.14.1.jar"

-"BOOT-INF/lib/jul-to-slf4j-1.7.32.jar"

-"BOOT-INF/lib/jakarta.annotation-api-1.3.5.jar"

-"BOOT-INF/lib/snakeyaml-1.29.jar"

-"BOOT-INF/lib/jackson-databind-2.13.0.jar"

-"BOOT-INF/lib/jackson-annotations-2.13.0.jar"

-"BOOT-INF/lib/jackson-core-2.13.0.jar"

"BOOT-INF/lib/jackson-datatype-jdk8-2.13.0.jar"

"BOOT-INF/lib/jackson-datatype-jsr310-2.13.0.jar"

-"BOOT-INF/lib/jackson-module-parameter-names-2.13.0.jar"

-"BOOT-INF/lib/tomcat-embed-core-9.0.55.jar"

-"BOOT-INF/lib/tomcat-embed-el-9.0.55.jar"

-"BOOT-INF/lib/tomcat-embed-websocket-9.0.55.jar"

-"BOOT-INF/lib/spring-web-5.3.13.jar"

-"BOOT-INF/lib/spring-beans-5.3.13.jar"

-"BOOT-INF/lib/spring-webmvc-5.3.13.jar"

-"BOOT-INF/lib/spring-aop-5.3.13.jar"

-"BOOT-INF/lib/spring-context-5.3.13.jar"

-"BOOT-INF/lib/spring-expression-5.3.13.jar"

-"BOOT-INF/lib/spring-boot-2.6.1.jar"

-"BOOT-INF/lib/spring-boot-autoconfigure-2.6.1.jar"

-"BOOT-INF/lib/slf4j-api-1.7.32.jar"

-"BOOT-INF/lib/spring-core-5.3.13.jar"

-"BOOT-INF/lib/spring-jcl-5.3.13.jar"

-"BOOT-INF/lib/spring-boot-jarmode-layertools-2.6.1.jar"

Layers.idx

"dependencies":

-"BOOT-INF/lib/"

"spring-boot-loader":

-"org/"

"snapshot-dependencies":

"application":

-"BOOT-INF/classes/"

-"BOOT-INF/classpath.idx"

-"BOOT-INF/layers.idx"

-"META-INF/"

Verify it again:

Hierarchical information ok

4. Dockerfile

The Dockerfile file is also relatively regular, except that before packaging the image, use the jarmode command to decompress the jar package, and then COPY the different layers in the process of packaging the image, and put the application layer with the greatest possibility of change at the end, so that you can directly use Docker's cache to improve packaging efficiency without any changes.

FROM eclipse-temurin:8-jre-focal as builder

WORKDIR application

ARG JAR_FILE=target/*.jar

COPY ${JAR_FILE} application.jar

RUN java-Djarmode=layertools-jar application.jar extract

FROM eclipse-temurin:8-jre-focal

WORKDIR application

COPY-from=builder application/dependencies/. /

COPY-from=builder application/spring-boot-loader/. /

COPY-from=builder application/snapshot-dependencies/. /

COPY-from=builder application/application/. /

ENV TZ= "Asia/Shanghai"

RUN ln-snf / usr/share/zoneinfo/$TZ / etc/localtime & & echo $TZ > / etc/timezone

ENV JVM_OPTS= ""

ENV JAVA_OPTS= ""

ENTRYPOINT ["sh", "- c", "java $JVM_OPTS $JAVA_OPTS org.springframework.boot.loader.JarLauncher"]

5. Application Modification & dive Analysis

In fact, if you package the image only once, it does not show the advantage of layering, so you need to modify some of the content and then package it again (or many times, but the essence is the same, so it is only modified once here)

Modify part of the application code:

Re-pack the jar package & re-mirror the image to get two images:

Use dive to analyze two mirrors:

It can be found that until the changed application layer (application/application/), the layered Id and Digest on both sides are exactly the same, so these layers can be reused, and the same layer will only store a single one in the docker directory, which can greatly reduce the time for container compilation and push.

The above is how to carry out the practice and analysis of Spring Boot layered packaging Docker image. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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

Development

Wechat

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

12
Report