This article introduces three techniques for building SpringBoot applications into Docker images using Maven.

(1) Use the build-image built-in spring-boot-Maven-plugin.

(2) Use Google jiB-Maven-Plugin.

(3) Use the Dockerflle-Maven-plugin.

Spring applications Boot

For practical purposes, a Spring Boot project is required.

Demo project Address:

Github.com/davelms/med…

Project Environment:

  • Java 11
  • Maven 3.6.3

Jar packages can be built normally using the MVN Package.

Can also work normally:

Java - jar target/demo application - 0.0.1 - the SNAPSHOT. The jarCopy the code

Test page:

http://localhost:8080/hello

Now that the Demo project is ready, let’s start practicing 3 ways to build an image.

#1 Spring Boot Maven plugin build-image

Spring Boot comes preloaded with its own plug-in for building Docker images, and we don’t need to change anything because it’s spring-boot-starter-parent in Pom.xml.

You don’t need to write a Dockerfile or worry about anything else, plugin does it all for you, such as security, memory, performance, etc.

Simply execute:

mvn spring-boot:build-image
Copy the code

After the execution is complete, a success message is displayed:

Run container tests:

Docker run -p 9090:8080 -t demo-application:0.0.1-SNAPSHOTCopy the code

Note: The native port mapped here is 9090.

#2 jib-maven-plugin

Jib is a Maven and Gradle plugin for creating Docker images.

This plugin has one very obvious feature: there is no need to install Docker locally, which is very convenient for continuous integration, and the Jib can be pushed directly to the designated Docker repository.

The Jib also does not need to write a Dockerfile.

It’s also very easy to use, with no code changes and no changes to pom.xml.

Just execute:

MVN compile com. Google. Cloud. Tools: jib - maven - plugin: 2.3.0: dockerBuildCopy the code
  • mvn compile

Maven is the familiar maven build directive.

  • Com. Google. Cloud. Tools: jib – maven – plugin: 2.3.0

Specifies the use of the Jib plug-in

  • dockerBuild

Is the execution target of the Jib plugin. DockerBuild specifies the Jib to use our locally installed Docker.

After the execution is complete, a success message is displayed:

Start container tests:

Docker run -p 9091:8080 -t demo-application:0.0.1-SNAPSHOTCopy the code

Note: The native port mapped here is 9091.

#3 dockerfile-maven-plugin

This plugin requires us to write a Dockerfile, which is already ready in the Domo project.

Dockerfile needs to be placed in the root directory of the project, along with pom.xml.

Then, you need to add some configuration in pom.xml to specify the repository, the tag tag, and the JAR_FILE defined in the Dockerfile above.

The image will be automatically built when the MVN package is executed, and you will see the message:

Run container:

Docker run -p 9092:8080 -t demo-application:0.0.1-SNAPSHOTCopy the code

Note: The native port mapped is 9092.

summary

The most convenient of the three is SpringBoot’s native way of doing nothing yourself.

The most characteristic of Jib is that it does not require you to install Docker locally and can be directly pushed to the designated warehouse, and it is also very simple to use.

Dockerfile-maven-plugin is the most troublesome plugin to create a dockerfile, but it is actually the most useful, because the first two are related to the network environment (you know), even if you configure the Docker image, will also report an error. You can try it.

References:

  • jib-maven-plugin

Github.com/GoogleConta…

  • dockerfile-maven plugin

Github.com/spotify/doc…

Translation of this article

Medium.com/swlh/build-…

Recommended reading

  • Architecture technology trends that developers must understand: Service Mesh
  • The illustration Kubernetes
  • ZooKeeper is not a good registry
  • Does Redis Cluster lose data?
  • Distributed transaction scheme – TCC