Recently the company’s application ready to containerize, because dozens of applications from testing to release is too much trouble, and also because of environmental factors lead to a variety of problems in the deployment. In order to maintain a consistent environment in development, testing, production, the introduction of container technology, first take the edge of the project to try, to gain experience. Today a brief summary of several common Docker packaging tools.
Spring Boot Docker
In a Spring Boot application, we can agree on different identifiers to define different environments. For example, dev
means development environment, test
means test environment, and the corresponding configuration files are application-dev.yaml
and application-test.yaml
. We activate the corresponding environment configuration by declaring spring.profiles.active
, e.g. spring.profiles.active=dev
when activating the dev
environment. The complete startup command is as follows.
|
|
Write a Dockerfile that can adapt to multiple environments based on the above command.
|
|
This way the packaged Docker image can be dynamically changed by adding an additional -env ACTIVE=test
to docker run
to change the environment. Writing Dockerfile alone is not convenient for our DevOps.
We need to be able to automate the process of building, pushing to repositories, pulling images, and running a series of pipeline operations. The good thing is that there are many tools on the market to help us with this process.
spring-boot-maven-plugin
This is an official Spring Boot plugin that provides Docker image building capabilities in some version of 2.x.
|
|
Once you have configured your Docker private storage, you can build the image via mvn clean spring-boot:build-image
.
The advantage of this approach is that there are no additional dependencies, but the disadvantage is that you need to download the build components from github, which can easily fail if you have a bad network.
Spotify Maven Plugin
The Spotify Maven plugin is a more common choice today. It requires application developers to write a Dockerfile and place the Dockerfile
in the project src/main/docker
directory. Import the following components.
This plugin provides mvn dockerfile:build
, mvn dockerfile:tag
and mvn dockerfile:push
commands to build, tag and publish to a remote private repository respectively, very simple.
This is a very easy plugin to get started with. The only requirement is that you need to be able to write a Dockerfile, so you can use this if you have high customization requirements.
Jib Maven Plugin
This once I have been introduced in an earlier article, you can learn more about it. It is Google’s open source OCI image packaging tool, which can be used to package Docker images, and in most cases already meet the needs. But if you want to customize it is still not easy, you need to read the official documentation given. The very first Dockerfile needs to be configured this way if you use JIb.
|
|
The advantage is that it does not require a local Docker environment and supports layered builds, image thinning, and easy to get started; the disadvantage is that customization is more difficult.
Reference
https://felord.cn/spring-docker-ex.html