Still using the third-party docker plug-in? Spring boot official plug-in really fragrant!

Time:2021-4-13

To facilitate the construction of docker images for springboot applications, we often use the Maven plug-in to package images. We have been using third-party plug-ins before, and we havespotifyandfabric8Two kinds of productsdocker-maven-plugin. Recently, springboot 2.4.0 has been released, and the official plug-in has also added support for docker. After experiencing it, I find it easy to use. I recommend it to you!

Springboot real e-commerce project Mall (40K + star) address:https://github.com/macrozheng/mall

Use of third party plug-ins

Let’s first learn about the use of third-party plug-ins, which is convenient for comparison with official plug-ins,fabric8Plug in specific use can refer toManual deployment of springboot application? Try this automation plug-in! 》

  • It is worth noting that when we use plug-ins, we need to define the image building process ourselves, such as in thepom.xmlThe following configuration is used in,<images>The configuration under the tag is the configuration of the image construction process;
<build>
    <plugins>
        <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>docker-maven-plugin</artifactId>
            <version>0.33.0</version>
            <configuration>
                <! -- docker remote management address -- >
                <dockerHost>http://192.168.3.101:2375</dockerHost>
                <! -- docker push image warehouse address -- >
                <pushRegistry>http://192.168.3.101:5000</pushRegistry>
                <images>
                    <image>
                        <! -- because it is pushed to the private image warehouse, the warehouse address needs to be added to the image name -- >
                        <name>192.168.3.101:5000/mall-tiny/${project.name}:${project.version}</name>
                        <! -- defines the mirror build behavior -- >
                        <build>
                            <! -- define the underlying image -- >
                            <from>java:8</from>
                            <args>
                                <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
                            </args>
                            <! -- defines which files are copied to the container -- >
                            <assembly>
                                <! -- defines the directory to copy to the container -- >
                                <targetDir>/</targetDir>
                                <! -- only copy the generated jar package -- >
                                <descriptorRef>artifact</descriptorRef>
                            </assembly>
                            <! -- define container start command -- >
                            <entryPoint>["java", "-jar","/${project.build.finalName}.jar"]</entryPoint>
                            <! -- define maintainer -- >
                            <maintainer>macrozheng</maintainer>
                        </build>
                    </image>
                </images>
            </configuration>
        </plugin>
    </plugins>
</build>
  • Or define the image construction process in the dockerfile file first;
#The image needs to depend on the underlying image
FROM java:8
#Copy the files generated by the current Maven directory to the / directory of the docker container
COPY maven /
#The claim service runs on port 8080
EXPOSE 8080
#Specifies that the jar package runs when the docker container starts
ENTRYPOINT ["java", "-jar","/mall-tiny-fabric-0.0.1-SNAPSHOT.jar"]
#Specify the name of the maintainer
MAINTAINER macrozheng
  • Then, the dockerfile file is referenced in the plug-in to build the image;
<build>
     <dockerFileDir>${project.basedir}</dockerFileDir>
</build>
  • In fact, for springboot applications, how to build a docker image from an application jar package is basically the same. Why do you have to define the construction process of the image yourself?

Use of official plug-ins

The official springboot plug-in solves the above problem. It doesn’t need to write the docker image construction process by itself. Is it very convenient to build it directly and automatically! Now let’s experience the power of it!

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE                                                                COMMAND                  CREATED             STATUS              PORTS                                            NAMES
9ffec08467ac        joxit/docker-registry-ui:static                                      "/bin/sh -c entrypoi…"   2 hours ago         Up 2 hours          0.0.0.0:8280->80/tcp                             registry-ui
a809535ee2a2        registry:2                                                           "/entrypoint.sh /etc…"   3 months ago        Up 7 hours          0.0.0.0:5000->5000/tcp                           registry2
  • Then we need to upgrade the version of the application to springboot 2.4.0. The previous version of docker does not have this perfect support;
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
  • Then modify itpom.xmlFile to configure the official Maven plug-in, mainly to configure the docker related functions;
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <! -- configure image name -- >
            <name>192.168.3.101:5000/mall-tiny/${project.name}:${project.version}</name>
            <! -- after the image is packaged, it will be automatically pushed to the image warehouse -- >
            <publish>true</publish>
        </image>
        <docker>
            <! -- docker remote management address -- >
            <host>http://192.168.3.101:2375</host>
            <! -- access without TLS -- >
            <tlsVerify>false</tlsVerify>
            <! -- docker push image warehouse configuration -- >
            <publishRegistry>
                <! -- push image warehouse user name -- >
                <username>test</username>
                <! -- push image warehouse password -- >
                <password>test</password>
                <! -- push image warehouse address -- >
                <url>http://192.168.3.101:5000</url>
            </publishRegistry>
        </docker>
    </configuration>
</plugin>
  • If you are using idea, double-click the springboot plug-inbuild-imageThe command can be packaged with one click and pushed to the image warehouse;

Still using the third-party docker plug-in? Spring boot official plug-in really fragrant!

  • You can also use the following Maven command on the command line to package and build the image;
mvn spring-boot:build-image
  • During the image construction process, the following information will be output. Because many dependencies will be downloaded from GitHub, the download will fail when the network is bad. Just try several times
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:base' 100%
[INFO]  > Pulled builder image 'paketobuildpacks/[email protected]:9d377230ba8ee74d8619178fd318b1b87a7da1a88bdb198afd14dd7de9e8ea6a'
[INFO]  > Pulling run image 'docker.io/paketobuildpacks/run:base-cnb' 100%
[INFO]  > Pulled run image 'paketobuildpacks/[email protected]:33d37fc9ba16e220f071805eaeed881a508ceee5c8909db5710aaed7e97e4fc2'
[INFO]  > Executing lifecycle version v0.9.3
[INFO]  > Using build cache volume 'pack-cache-5641f846df6.build'
[INFO] 
[INFO]  > Running creator
[INFO]     [creator]     ===> DETECTING
[INFO]     [creator]     5 of 18 buildpacks participating
[INFO]     [creator]     paketo-buildpacks/ca-certificates   1.0.1
[INFO]     [creator]     paketo-buildpacks/bellsoft-liberica 5.2.1
[INFO]     [creator]     paketo-buildpacks/executable-jar    3.1.3
[INFO]     [creator]     paketo-buildpacks/dist-zip          2.2.2
[INFO]     [creator]     paketo-buildpacks/spring-boot       3.5.0
[INFO]     [creator]     ===> ANALYZING
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/ca-certificates:helper" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/bellsoft-liberica:helper" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/bellsoft-liberica:java-security-properties" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/bellsoft-liberica:jre" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/bellsoft-liberica:jvmkill" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/executable-jar:class-path" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/spring-boot:helper" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/spring-boot:spring-cloud-bindings" from app image
[INFO]     [creator]     Restoring metadata for "paketo-buildpacks/spring-boot:web-application-type" from app image
[INFO]     [creator]     ===> RESTORING
[INFO]     [creator]     ===> BUILDING
[INFO]     [creator]     
[INFO]     [creator]     Paketo CA Certificates Buildpack 1.0.1
[INFO]     [creator]       https://github.com/paketo-buildpacks/ca-certificates
[INFO]     [creator]       Launch Helper: Reusing cached layer
[INFO]     [creator]     
[INFO]     [creator]     Paketo BellSoft Liberica Buildpack 5.2.1
[INFO]     [creator]       https://github.com/paketo-buildpacks/bellsoft-liberica
[INFO]     [creator]       Build Configuration:
[INFO]     [creator]         $BP_JVM_VERSION              8.*             the Java version
[INFO]     [creator]       Launch Configuration:
[INFO]     [creator]         $BPL_JVM_HEAD_ROOM           0               the headroom in memory calculation
[INFO]     [creator]         $BPL_JVM_LOADED_CLASS_COUNT  35% of classes  the number of loaded classes in memory calculation
[INFO]     [creator]         $BPL_JVM_THREAD_COUNT        250             the number of threads in memory calculation
[INFO]     [creator]         $JAVA_TOOL_OPTIONS                           the JVM launch flags
[INFO]     [creator]       BellSoft Liberica JRE 8.0.275: Reusing cached layer
[INFO]     [creator]       Launch Helper: Reusing cached layer
[INFO]     [creator]       JVMKill Agent 1.16.0: Reusing cached layer
[INFO]     [creator]       Java Security Properties: Reusing cached layer
[INFO]     [creator]     
[INFO]     [creator]     Paketo Executable JAR Buildpack 3.1.3
[INFO]     [creator]       https://github.com/paketo-buildpacks/executable-jar
[INFO]     [creator]       Process types:
[INFO]     [creator]         executable-jar: java org.springframework.boot.loader.JarLauncher
[INFO]     [creator]         task:           java org.springframework.boot.loader.JarLauncher
[INFO]     [creator]         web:            java org.springframework.boot.loader.JarLauncher
[INFO]     [creator]     
[INFO]     [creator]     Paketo Spring Boot Buildpack 3.5.0
[INFO]     [creator]       https://github.com/paketo-buildpacks/spring-boot
[INFO]     [creator]       Creating slices from layers index
[INFO]     [creator]         dependencies
[INFO]     [creator]         spring-boot-loader
[INFO]     [creator]         snapshot-dependencies
[INFO]     [creator]         application
[INFO]     [creator]       Launch Helper: Reusing cached layer
[INFO]     [creator]       Web Application Type: Contributing to layer
[INFO]     [creator]         Servlet web application detected
[INFO]     [creator]         Writing env.launch/BPL_JVM_THREAD_COUNT.default
[INFO]     [creator]       Spring Cloud Bindings 1.7.0: Reusing cached layer
[INFO]     [creator]       4 application slices
[INFO]     [creator]       Image labels:
[INFO]     [creator]         org.opencontainers.image.title
[INFO]     [creator]         org.opencontainers.image.version
[INFO]     [creator]         org.springframework.boot.spring-configuration-metadata.json
[INFO]     [creator]         org.springframework.boot.version
[INFO]     [creator]     ===> EXPORTING
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/ca-certificates:helper'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/bellsoft-liberica:helper'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/bellsoft-liberica:java-security-properties'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/bellsoft-liberica:jre'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/bellsoft-liberica:jvmkill'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/executable-jar:class-path'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/spring-boot:helper'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/spring-boot:spring-cloud-bindings'
[INFO]     [creator]     Reusing layer 'paketo-buildpacks/spring-boot:web-application-type'
[INFO]     [creator]     Reusing 4/5 app layer(s)
[INFO]     [creator]     Adding 1/5 app layer(s)
[INFO]     [creator]     Reusing layer 'launcher'
[INFO]     [creator]     Reusing layer 'config'
[INFO]     [creator]     Reusing layer 'process-types'
[INFO]     [creator]     Adding label 'io.buildpacks.lifecycle.metadata'
[INFO]     [creator]     Adding label 'io.buildpacks.build.metadata'
[INFO]     [creator]     Adding label 'io.buildpacks.project.metadata'
[INFO]     [creator]     Adding label 'org.opencontainers.image.title'
[INFO]     [creator]     Adding label 'org.opencontainers.image.version'
[INFO]     [creator]     Adding label 'org.springframework.boot.spring-configuration-metadata.json'
[INFO]     [creator]     Adding label 'org.springframework.boot.version'
[INFO]     [creator]     Setting default process type 'web'
[INFO]     [creator]     *** Images (d5e1771dac7b):
[INFO]     [creator]           192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT
[INFO] 
[INFO] Successfully built image '192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT'
[INFO] 
[INFO]  > Pushed image '192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT'
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:06 min
[INFO] Finished at: 2020-11-27T15:07:46+08:00
[INFO] Final Memory: 37M/359M
[INFO] ------------------------------------------------------------------------
  • After the image is successfully built, you can view our image from the image warehouse
[[email protected] ~]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
paketobuildpacks/run                                  base-cnb            a717358311fc        9 days ago          87.2MB
java                                                  8                   d23bdf5b1b1b        3 years ago         643MB
192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin   0.0.1-SNAPSHOT      d5e1771dac7b        40 years ago        244MB
pack.local/builder/fewqajyqsc                         latest              f15fad05a5ba        40 years ago        558MB
pack.local/builder/kirivtcqtu                         latest              f15fad05a5ba        40 years ago        558MB
paketobuildpacks/builder                              base                511452064e06        40 years ago        558MB
  • We can start fromDocker Registry UIView the images in the image warehouse in thehttp://192.168.3.101:8280/

Still using the third-party docker plug-in? Spring boot official plug-in really fragrant!

  • Then start our springboot application with the following command:
docker run -p 8080:8080 --name mall-tiny-docker-plugin \
--link mysql:db \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/mall-tiny-docker-plugin/logs:/var/logs \
-d 192.168.3.101:5000/mall-tiny/mall-tiny-docker-plugin:0.0.1-SNAPSHOT
  • After successful startup, you can successfully access the swagger page of the springboot applicationhttp://192.168.3.101:8080/swagger-ui.html

Still using the third-party docker plug-in? Spring boot official plug-in really fragrant!

summary

The official Maven plug-in of springboot avoids the process of writing docker image construction, and makes full use of jar layering technology after springboot 2.3. However, it causes some trouble for scenes that need custom image construction.

reference material

Official document:https://docs.spring.io/spring…

Project source address

https://github.com/macrozheng…

This article is about GitHubhttps://github.com/macrozheng/mall-learningHas been included, welcome to star!