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

Analysis and treatment of scroll bars in both HTML and embedded Flash

We often encounter this situation when doing devel...

Detailed tutorial on replacing mysql8.0.17 in windows10

This article shares the specific steps of replaci...

Why MySQL does not recommend using subqueries and joins

To do a paginated query: 1. For MySQL, it is not ...

Comparison of the efficiency of different methods of deleting files in Linux

Test the efficiency of deleting a large number of...

Complete steps to use vue-router in vue3

Preface Managing routing is an essential feature ...

Implementation of a simple login page for WeChat applet (with source code)

Table of contents 1. Picture above 2. User does n...

In-depth explanation of binlog in MySQL 8.0

1 Introduction Binary log records SQL statements ...

Detailed explanation of common operations of Docker images and containers

Image Accelerator Sometimes it is difficult to pu...

The difference between mysql outer join and inner join query

The syntax for an outer join is as follows: SELEC...

How to define data examples in Vue

Preface In the development process, defining vari...

Detailed explanation of the pitfalls of Apache domain name configuration

I have never used apache. After I started working...

How to use multi-core CPU to speed up your Linux commands (GNU Parallel)

Have you ever had the need to compute a very larg...

Detailed explanation of Angular parent-child component communication

Table of contents Overview 1. Overview of input a...

MySQL database index order by sorting detailed explanation

Table of contents The cause of the incident Anato...