Summary of Spring Boot Docker packaging tools

Summary of Spring Boot Docker packaging tools

Recently, the company's applications are preparing to be containerized because it is too troublesome to test and release dozens of applications, and various problems will arise during deployment due to environmental factors. In order to maintain a consistent environment in development, testing, and production, container technology was introduced. We first tried it with edge projects and accumulated experience. Today, we briefly summarized several common Spring Boot Docker packaging tools.

Spring Boot Docker

In Spring Boot applications, we can agree on different identifiers to define different environments. For example, dev represents the development environment and test represents the test environment. The corresponding configuration files are apppcation-dev.yaml and apppcation-test.yaml. We activate the corresponding environment configuration by declaring spring.profiles.active, for example, spring.profiles.active=dev when activating the dev environment. The complete startup command is:

java -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=dev -jar spring-boot-app.jar

According to the above command, write a Dockerfile that can adapt to multiple environments:

# Import openjdk image FROM adoptopenjdk/openjdk8 
#Declare the author LABEL AUTHOR=felord OG=felord.cn 
# Mount several useful folders such as logs VOLUME ["/tmp","/logs"] 
#Declare an environment parameter to dynamically enable the default dev configuration file 
ENV ACTIVE=dev 
# Expose port EXPOSE 8080 
# Copy and modify the name of the jar file after application packaging ADD /target/flyway-spring-boot-1.0.0.jar app.jar 
# The first command run when the container starts is used to start the application ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=${ACTIVE}","-jar","app.jar"]

The packaged Docker image can then dynamically change the environment by adding an additional --env ACTIVE=test to docker run. Simply writing Dockerfile is not convenient for our DevOps.

Docker image life cycle

We need to be able to automatically build, push to the repository, pull images, and run a series of pipeline operations. Fortunately, there are many tools on the market to help us achieve this process.

spring-boot-maven-plugin

This is an official Spring Boot plug-in that provides Docker image building capabilities in a certain version of 2.x.

<project> 
 <build> 
  <plugins> 
   <plugin> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-maven-plugin</artifactId> 
    <configuration> 
     <image> 
     <name>docker.repo.com/library/${project.artifactId}:${project.version}</name> 
      <publish>true</publish> 
     </image> 
     <docker> 
      <publishRegistry> 
       <username>user</username> 
       <password>secret</password> 
       <url>https://docker.repo.com/v1/</url> 
       <email>[email protected]</email> 
      </publishRegistry> 
     </docker> 
    </configuration> 
   </plugin> 
  </plugins> 
 </build> 
</project>

After configuring the Docker private warehouse, you can build the image through mvn clean spring-boot:build-image.

The advantage of this method is that there is no additional dependency. The disadvantage is that you need to download the build components from GitHub, and it is easy to fail if the network is not good.

Spotify Maven Plugin

The Spotify Maven plugin is a popular choice at present. It requires application developers to write a Dockerfile and place it in the project's src/main/docker directory. Then you can import it by:

<plugin> 
          <groupId>com.spotify</groupId> 
          <artifactId>dockerfile-maven-plugin</artifactId> 
          <version>1.4.8</version> 
          <configuration> 
              <repository>repo.com/${project.artifactId}</repository> 
          </configuration> 
      </plugin>

This plug-in provides three commands: mvn dockerfile:build, mvn dockerfile:tag, and mvn dockerfile:push, which are used to build, tag, and publish to a remote private repository, respectively. It is very simple.

This is a plug-in that is very easy to use. The only requirement is that you need to be able to write Dockerfile. You can use this if you have high requirements for customization.

Jib Maven Plugin

I have already introduced this in an earlier article, you can learn more about it. It is Google's open source OCI image packaging tool that can be used to package Docker images, which meets the needs in most cases. But if you want to customize it, it is still not easy, you need to read the official documents. The initial Dockerfile needs to be configured like this if using JIb:

<plugin> 
    <groupId>com.google.cloud.tools</groupId> 
    <artifactId>jib-maven-plugin</artifactId> 
    <version>3.0.0</version> 
    <configuration> 
        <from> 
            <image>adoptopenjdk/openjdk8</image> 
        </from> 
        <to> 
            <image>docker.repo.com/library/${project.artifactId}</image> 
            <auth> 
                <username>felord</username> 
                <password>xxxxxx</password> 
            </auth> 
            <tags> 
                <tag>${project.version}</tag> 
            </tags> 
        </to> 
        <extraDirectories> 
            <paths> 
                <path> 
                    <from>target/${project.artifactId}-${project.version}.jar</from> 
                    <includes>*.jar</includes> 
                    <into>/app.jar</into> 
                </path> 
            </paths> 
        </extraDirectories> 
        <containerizingMode>packaged</containerizingMode> 
        <container> 
            <volumes>/tmp,/logs</volumes> 
            <ports> 
                <port>8080</port> 
            </ports> 
            <environment> 
                <active>dev</active> 
            </environment> 
            <entrypoint> 
                java,-Djava.security.egd=file:/dev/./urandom,-Dspring.profiles.active=${active},-jar,/app.jar 
            </entrypoint> 
            <creationTime>USE_CURRENT_TIMESTAMP</creationTime> 
        </container> 
    </configuration> 
</plugin>

The advantage is that it does not require a local Docker environment, and supports layered construction and image slimming, making it easy to get started; the disadvantage is that customization is more difficult.

This is the end of this article about which Docker packaging plug-in for Spring Boot is better. For more relevant Spring Boot Docker packaging plug-in content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief analysis of SpringBoot packaging and uploading to docker and implementing multi-instance deployment (IDEA version)
  • Detailed steps for springboot docker jenkins to automatically deploy and upload images
  • Implementation of Springboot packaging as Docker image and deployment
  • The simplest implementation of spring boot packaging docker image
  • Analysis of Springboot microservice packaging Docker image process
  • Detailed steps for Spring Boot packaging and uploading to Docker repository

<<:  Use CSS to prevent Lightbox to realize the display of large image code without refreshing when clicking on small image

>>:  Causes and solutions for MySQL data loss

Recommend

Detailed explanation of MySQL 8's new feature ROLE

What problems does MySQL ROLE solve? If you are a...

Detailed explanation of Linux kernel macro Container_Of

Table of contents 1. How are structures stored in...

Dynamic starry sky background implemented with CSS3

Result:Implementation Code html <link href=...

Detailed explanation of the interaction between React Native and IOS

Table of contents Prerequisites RN passes value t...

Detailed explanation of Docker common commands Study03

Table of contents 1. Help Command 2. Mirror comma...

Example of MySQL slow query

Introduction By enabling the slow query log, MySQ...

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...

Detailed explanation of how to use the Vue license plate input component

A simple license plate input component (vue) for ...

Should nullable fields in MySQL be set to NULL or NOT NULL?

People who often use MySQL may encounter the foll...

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...

What is ZFS? Reasons to use ZFS and its features

History of ZFS The Z File System (ZFS) was develo...

Implementing user registration function with js

This article example shares the specific code of ...

Docker Swarm from deployment to basic operations

About Docker Swarm Docker Swarm consists of two p...