In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
It is believed that many inexperienced people have no idea about how to use SpringBoot to make Docker image. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
SpringBoot application source code
This actual combat uses an ordinary SpringBoot project. If you do not want to write code, the source code can be downloaded from GitHub. The address and link information are shown in the following table:
Name Link Note Project Home Page https://github.com/zq2599/blog_demos the home page of the project 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) git@github.com:zq2599/blog_demos.git the warehouse address of the project source code, ssh protocol
There are multiple folders in this git project. The application of this chapter is under the dockerlayerdemo folder, as shown in the red box below:
Version information
SpringBoot:2.3.0.RELEASE
JDK:1.8.0_121
Maven:3.3.9
Docker:19.03.8
Operating system: MacBook pro 13 inch, macOS Catalina 10.15.4
Build a mirror image for actual combat
Modify pom.xml to add child nodes to the configuration of the spring-boot-maven-plugin plug-in. The value of enabled is true, as shown in the red box below:
Add the Dockerfile file to the directory where the pom.xml file is located, as follows:
# specify basic image This is the early stage of phased construction. FROM openjdk:8u212-jdk-stretch as builder# executes the working directory WORKDIR application# configuration parameter ARG JAR_FILE=target/*.jar# copies the jar file from the compiled build to the image space COPY ${JAR_FILE} application.jar# uses the tool spring-boot-jarmode-layertools to extract the split construction result from application.jar RUN java-Djarmode=layertools-jar application.jar extract# formal construction In the previous stage of building image FROM openjdk:8u212-jdk-stretchWORKDIR application#, multiple files were extracted from jar. Here, execute the COPY command to copy to the mirror space, and each COPY is a layerCOPY-- 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"]
Execute the following command to compile the build project:
Mvn clean package-U-DskipTests
Make sure that the jar file is generated in the target directory after the compilation and build is complete
Execute the following command in the directory where Dockerfile resides to build the image (adjust the image name according to your actual situation):
Docker build-t dockerlayerdemo:0.0.1.
The prompts for successful image construction are as follows:
Verification
Execute the following command to create and start the container:
Docker run-- rm-p 8080 dockerlayerdemo:0.0.1
Console message for successful startup:
3. Browser access: http://localhost:8080/hello, as shown in the following figure, everything is normal:
4. Look at the hierarchical information of the image and execute the command:
Docker history dockerlayerdemo:0.0.1
As shown in the figure below, the contents of the entire jar, such as class, dependent libraries, dependent resources, etc., are COPY to the image space several times, so if you only change the class in the future, when you update the image, you only need to download the layer of class (other layer can be cached locally directly):
At this point, the image construction method officially recommended by SpringBoot-2.3.0.RELEASE is completed, but it also leaves some questions:
How is the image construction solution recommended in version 2.3 different from the previous version?
What exactly do the new parameters of the spring-boot-maven-plugin plug-in in pom.xml do?
What does java-Djarmode=layertools-jar application.jar extract mean in Dockerfile?
After reading the above, have you mastered the method of making Docker image using SpringBoot (2.3)? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.