current position:Home>Understand the layered construction of docker image image image by spring boot application

Understand the layered construction of docker image image image by spring boot application

2022-01-27 00:15:22 Java Xiaohai


I've been using it Dockerfile The way to Spring Boot application To build a Docker Image, Find out jar The bag is only ten trillion , But there are hundreds of megabytes of images in the construction , Transmission is very slow . It turned out spring-boot-maven-plugin Plug ins can be built hierarchically Docker Image, I tried , It works pretty well .

Common construction methods

take Spring Boot The application is packaged into Docker Image The most common way is to write Dockerfile, Use docker build Command manual packaging .

Dockerfile The contents are as follows :

FROM openjdk:17
COPY target/*.jar /app/app.jar
ENTRYPOINT ["java","-jar","/app/app.jar"]
 Copy code 
  1. The build process :


  1. The build results :


Use docker images Look at the mirror image , You can find two more mirrors : webflux_demo_file and openjdk.webflux_demo_file Is the image of the final build of the application ,openjdk It is the basic image that is relied on . From the image size, we can see webflux_demo_file Mirror Co 796m, And the basic image openjdk That's it 471m.

Layered construction

spring boot 2.3 After that spring-boot-maven-plugin Plug ins support building applications into Docker Image, And support layered construction 、 Push the image to the image warehouse and other functions .

To use spring-boot-maven-plugin Plug ins build applications hierarchically , Need to be in pom Add some configuration to the file .

  1. modify pom file

stay spring-boot-maven-plugin Add a way to enable layered construction and configure layered configuration .

    <!-- Declare how the timestamp is formatted -->

            <!-- Turn on layered construction -->
             <!-- Building configuration -->
            <!-- Image name ,  Use the build time as the version number of the image -->
 Copy code 
  1. establish layers.xml file

layers.xml The file configures the way of hierarchical construction , The contents are as follows :

<layers xmlns=""
        <into layer="spring-boot-loader">
        <into layer="application" />
        <into layer="application">
            <includeModuleDependencies />
        <into layer="snapshot-dependencies">
        <into layer="dependencies" />
 Copy code 
  1. Build a mirror image

Use mvn spring-boot:build-image Command or in maven Tools Middle double click spring-boot:build-image Start building images . Pictured


  1. Build information

Too much build information , Here's the cut . Here's the picture


  1. The build results

Use docker images Look at the mirror image , You can see that the image built in this way is only 282m. Half smaller than the image before use .


The fly in the ointment is that the construction needs to start from github Download a package , Resulting in a long build time

copyright notice
author[Java Xiaohai],Please bring the original link to reprint, thank you.

Random recommended