About the Springboot-2.3 Containerization Techniques series

“SpringBoot-2.3 Container technology” series, aims to learn and practice the latest container technology brought by version 2.3, so that our Java applications more adapt to the container environment, in the cloud computing era still follow the mainstream, to maintain competitiveness;

The whole series of articles are divided into two parts: theme and auxiliary. The theme part is as follows:

  1. Experience SpringBoot(2.3) application to make Docker image (official scheme);
  2. Detailed Explanation of SpringBoot(2.3) application making Docker image (official scheme);
  3. “Mastering Springboot-2.3 Container Probes: Basics”;
  4. Mastering Springboot-2.3 container Probes: In Depth.
  5. “Mastering Springboot-2.3 Container Probe: Combat”;

The supporting parts are some references and summary of the notes, as follows:

  1. “Springboot-2.3 Mirroring solution why do multiple layers”;
  2. Docker command (sudo);
  3. “Rapid deployment of SpringBoot applications to K8S in development phase”;

This introduction

  1. The theme of this paper is practical combat, and strive to experience the official mirror production scheme at the fastest speed;
  2. This chapter will not cover the theoretical knowledge, which will be left to the next chapter;
  3. There may be a lot of questions after the operation. At the end of the article, there are some typical questions that you can think about before entering the next chapter.

SpringBoot application source code

This practice is a common SpringBoot project, if you do not want to write code, source code can be downloaded on GitHub, address and link information as shown in the following table:

The name of the link note
Project home page Github.com/zq2599/blog… The project’s home page on GitHub
Git repository address (HTTPS) Github.com/zq2599/blog… The project source warehouse address, HTTPS protocol
Git repository address (SSH) [email protected]:zq2599/blog_demos.git The project source warehouse address, SSH protocol

This git project has multiple folders. The application of this chapter is in the DockerLayerDemo folder, as shown in the red box below:

Version information

  1. SpringBoot: 2.3.0. RELEASE
  2. The JDK: 1.8.0 comes with _121
  3. Maven: 3.3.9
  4. Docker: 19.03.8
  5. Operating system: MacBook Pro 13 inch, macOS Catalina 10.15.4

Building mirror combat

  1. Modify pom. XML and add a child node to the spring-boot-Maven-plugin configuration. The value of Enabled is true, as shown in the red box below:

  1. Add the Dockerfile file to the pom. XML directory with the following contents:
# select base image, ARG JAR_FILE=target/*.jar # COPY ${JAR_FILE} application.jar # Use spring-boot-jarmode-layerTools to extract the split result from application.jar RUN java-djarmode =layertools -jar application.jar extract FROM openJDK :8u212-jdk-stretch application # In the previous phase, multiple files were extracted from the JAR and copied to the mirror space by executing the COPY command respectively. Each COPY is a layer COPY - from = builder application/dependencies /. / COPY - from = builder application/spring - the boot loader /. / COPY --from=builder application/snapshot-dependencies/ ./ COPY --from=builder application/application/ ./ ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]Copy the code
  1. Execute the following command to compile the build project:

    mvn clean package -U -DskipTestsCopy the code
  2. Ensure that the target directory has jar files generated after the build is completed.

  3. In the directory where Dockerfile resides, run the following command to build the image (please adjust the image name according to your actual situation) :

    Docker build-t DockerLayerDemo :0.0.1Copy the code
  4. The following message is displayed if the image is successfully constructed:

validation

  1. Create and start the container by executing the following command:

    Docker run --rm -p 8080:8080 DockerLayerDemo :0.0.1Copy the code
  2. Information about the console successfully started:

3. Browser access:http://localhost:8080/hello, as shown below, everything is normal:

4. View the hierarchical information about the mirror and run the following command:

Docker history dockerlayerdemo: 0.0.1Copy the code
  1. As shown in the following figure, the contents of the entire JAR, such as class, dependency library, dependency resource, etc., are copied to the image space several times. Therefore, if only the class is changed in the future, when updating the image, you only need to download the layer of the class (other layers can be directly cached locally) :

Questions left

At this point, springboot-2.3.0.release is officially recommended to build an image, but some questions remain:

  1. What is the difference between version 2.3 and previous versions of the recommended image construction scheme?
  2. What does the spring-boot-Maven-plugin do with the parameters added to pum.xml?
  3. Java-djarmode = layerTools -jar application.jar extract

These questions will be explored in depth in the next article.

The reference information

IO /spring-boot… 4.31 section,

Welcome to my GitHub

  • Address: github.com/zq2599/blog…
  • Content: original article classification summary, and supporting source code, involving Java, Docker, K8S, DevOPS and so on

Welcome to pay attention to my public number: programmer Xin Chen

This article is published by OpenWrite!