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

Preface

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
WORKDIR /app
COPY target/*.jar /app/app.jar
ENTRYPOINT ["java","-jar","/app/app.jar"]
 Copy code 
  1. The build process :

image.png

  1. The build results :

image.png

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 .


<properties>
    <!-- Declare how the timestamp is formatted -->
    <maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>


<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <layers>
            <!-- Turn on layered construction -->
            <enabled>true</enabled>
             <!-- Building configuration -->
            <configuration>${project.basedir}/layers.xml</configuration>
        </layers>
        <image>
            <!-- Image name ,  Use the build time as the version number of the image -->
            <name>example.com/webflux_demo:${maven.build.timestamp}</name>
        </image>
    </configuration>
</plugin>
 Copy code 
  1. establish layers.xml file

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

<layers xmlns="http://www.springframework.org/schema/boot/layers"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/boot/layers
                          https://www.springframework.org/schema/boot/layers/layers-2.6.xsd">
    <application>
        <into layer="spring-boot-loader">
            <include>org/springframework/boot/loader/**</include>
        </into>
        <into layer="application" />
    </application>
    <dependencies>
        <into layer="application">
            <includeModuleDependencies />
        </into>
        <into layer="snapshot-dependencies">
            <include>*:*:*SNAPSHOT</include>
        </into>
        <into layer="dependencies" />
    </dependencies>
    <layerOrder>
        <layer>dependencies</layer>
        <layer>spring-boot-loader</layer>
        <layer>snapshot-dependencies</layer>
        <layer>application</layer>
    </layerOrder>
</layers>
 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

image.png

  1. Build information

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

image.png

  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 .

image.png

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.
https://en.cdmana.com/2022/01/202201270015176736.html

Random recommended