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

Summary of Kubernetes's application areas

Kubernetes is the leader in the container orchest...

Simple operation of installing vi command in docker container

When using a docker container, sometimes vim is n...

How to correctly create MySQL indexes

Indexing is similar to building bibliographic ind...

How to use squid to build a proxy server for http and https

When we introduced nginx, we also used nginx to s...

HTTP return code list (Chinese and English explanation)

http return code list (below is an overview) for ...

Introduction to the use of em in elastic layout in CSS3: How many pixels is 1em?

I have been using CSS for a long time, but I have...

onfocus="this.blur()" is hated by blind webmasters

When talking about the screen reading software op...

Detailed explanation of MySQL/Java server support for emoji and problem solving

This article describes the support and problem so...

A brief discussion on Flink's fault-tolerant mechanism: job execution and daemon

Table of contents 1. Job Execution Fault Toleranc...

JavaScript implements circular progress bar effect

This article example shares the specific code of ...

14 Ways to Create Website Content That Engages Your Visitors

When I surf the Net, I often see web sites filled...

Analysis and Solution of ERROR:2002 Reported When MySQL Starts

Preface This article mainly introduces the analys...

5 ways to migrate Docker containers to other servers

Migration is unavoidable in many cases. Hardware ...

js to achieve star flash effects

This article example shares the specific code of ...

The difference between z-index: 0 and z-index: auto in CSS

I've been learning about stacking contexts re...