In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to create Docker images for Spring Boot applications". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's learn how to create Docker images for Spring Boot applications!
1. Traditional Docker builds
The traditional way to build Docker images with Spring Boot is to use Dockerfiles. Here is a simple example:
FROM openjdk:8-jdk-alpine EXPOSE 8080 ARG JAR_FILE=target/demo-app-1.0.0.jar ADD ${JAR_FILE} app.jar ENTRYPOINT ["java","-jar","/app.jar"]
We can then create Docker images using the docker build command. This is great for most applications, but there are some downsides.
First, we use the fat jar created by Spring Boot. This affects start-up time, especially in container environments. We can save startup time by adding the split contents of the jar file.
Second, Docker images are built hierarchically. The Spring Boot fat jar features all application code and third-party libraries in one layer. This means that even if only one line of code changes, the entire layer must be rebuilt.
By decomposing jars before building, application code and third-party libraries each get their own layers. This way, we can take advantage of Docker's caching mechanism. Now, when a line of code is changed, only the corresponding layer needs to be rebuilt.
With that in mind, let's see how Spring Boot improves the process of creating Docker images.
2. Buildpacks
BuildPacks is a tool that provides frameworks and application dependencies.
For example, given a Spring Boot fat jar, a buildpack will provide us with the Java runtime. This allows us to skip Dockerfile and automatically get a reasonable docker image.
Spring Boot includes Maven and Gradle support for bulidpacks. For example, when building with Maven, we would run the following command:
./ mvnw spring-boot:build-image
Let's look at some of the related outputs and see what happens:
[INFO] Building jar: target/demo-0.0.1-SNAPSHOT.jar ... [INFO] Building image 'docker.io/library/demo:0.0.1-SNAPSHOT' ... [INFO] > Pulling builder image 'gcr.io/paketo-buildpacks/builder:base-platform-api-0.3' 100% ... [INFO] [creator] ===> DETECTING [INFO] [creator] 5 of 15 buildpacks participating [INFO] [creator] paketo-buildpacks/bellsoft-liberica 2.8.1 [INFO] [creator] paketo-buildpacks/executable-jar 1.2.8 [INFO] [creator] paketo-buildpacks/apache-tomcat 1.3.1 [INFO] [creator] paketo-buildpacks/dist-zip 1.3.6 [INFO] [creator] paketo-buildpacks/spring-boot 1.9.1 ... [INFO] Successfully built image 'docker.io/library/demo:0.0.1-SNAPSHOT' [INFO] Total time: 44.796 s
The first line shows that we built a standard fat jar, just like any other typical maven package.
The next line starts Docker image building. Then, see that this bulid pulls the packeto builder.
Packeto is an implementation based on cloud-native bulidpacks. It is responsible for analyzing our project and determining the required frameworks and libraries. In our example, it determines that we have a Spring Boot project and adds the required build packages.
Finally, we see Docker images generated and total build time. Note that on the first build, it took quite a bit of time to download the build package and create the different layers.
One of the characteristics of buildpacks is that Docker images are multi-layered. So if we just change the application code, subsequent builds will be faster:
... [INFO] [creator] Reusing layer 'paketo-buildpacks/executable-jar:class-path' [INFO] [creator] Reusing layer 'paketo-buildpacks/spring-boot:web-application-type' ... [INFO] Successfully built image 'docker.io/library/demo:0.0.1-SNAPSHOT' ... [INFO] Total time: 10.591 s
3. hierarchical jar package
In some cases, we may not like using bulidpacks --maybe our infrastructure is already tied to another tool, or we already have custom Dockerfiles that we want to reuse.
For these reasons, Spring Boot also supports building Docker images using layered jars. To understand how it works, let's look at a typical Spring Boot fat jar layout:
org/ springframework/ boot/ loader/ ... BOOT-INF/ classes/ ... lib/ ...
Fat jar consists of 3 main areas:
Bootstrap classes required to launch Spring applications
application code
third-party libraries
Using a layered jar, the structure looks similar, but we get a new layers.idx file that maps each directory in the fat jar to a layer:
- "dependencies": - "BOOT-INF/lib/" - "spring-boot-loader": - "org/" - "snapshot-dependencies": - "application": - "BOOT-INF/classes/" - "BOOT-INF/classpath.idx" - "BOOT-INF/layers.idx" - "META-INF/"
Out-of-the-box, Spring Boot provides four layers:
Out of the box, Spring Boot offers 4 layers:
dependencies: dependencies from third parties
snapshot-dependencies: snapshot dependencies from third parties
resources: static resources
application: application code and resources
Our goal is to place application code and third-party libraries into layers to reflect how often they change.
For example, application code may be the code that changes most frequently, so it has its own layers. In addition, each layer can evolve independently, and only when a layer changes will Docker images be rebuilt for it.
Now that we understand the layered jar structure, let's look at how to leverage it to make Docker images.
3.1. Create layered jar
First, we must build a project to create a layered jar. For Maven, you need to add a new configuration to the Spring Boot plugin section of POM:
org.springframework.boot spring-boot-maven-plugin true
With this configuration, the Maven package command (including its other dependencies) will generate a new layered jar using the four default tiers mentioned earlier.
3.2. View and extract layering
Next, we need to extract the layers from the jar so that the Docker image has the right layers. To examine any layer of a layered jar, run the following command:
java -Djarmode=layertools -jar demo-0.0.1.jar list
Then extract them and run the command:
java -Djarmode=layertools -jar demo-0.0.1.jar extract
3.3. Creating Docker Images
The easiest way to incorporate these layers into a Docker image is to use Dockerfile:
FROM adoptopenjdk:11-jre-hotspot as builder ARG JAR_FILE=target/*.jar COPY ${JAR_FILE} application.jar RUN java -Djarmode=layertools -jar application.jar extract FROM adoptopenjdk:11-jre-hotspot COPY --from=builder dependencies/ ./ COPY --from=builder snapshot-dependencies/ ./ COPY --from=builder spring-boot-loader/ ./ COPY --from=builder application/ ./ ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
This Dockerfile extracts layers from the fat jar and copies each layer into the Docker image.
Each COPY instruction eventually generates a new layer in the Docker image.
If we build this Dockerfile, we can see that each layer in the layered jar is added to the Docker image as its own layer:
... Step 6/10 : COPY --from=builder dependencies/ ./ ---> 2c631b8f9993 Step 7/10 : COPY --from=builder snapshot-dependencies/ ./ ---> 26e8ceb86b7d Step 8/10 : COPY --from=builder spring-boot-loader/ ./ ---> 6dd9eaddad7f Step 9/10 : COPY --from=builder application/ ./ ---> dc80cc00a655 ... At this point, I believe that everyone has a deeper understanding of "how to create Docker images for Spring Boot applications". Let's actually do it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.