Build a Docker image using Maven and Spring Boot
This tutorial will introduce three techniques for building a Spring Boot java application into a Docker image using Maven.
The three approaches walked through in this article are;
- Using the integrated Spring Boot build-image goal.
- Using the jib-maven-plugin from Google.
- Using the dockerfile-maven-plugin from Spotify.
Let’s get started.
Spring Boot application
For the purpose of this demonstration we are going to use the Hello World example detailed on the Spring Boot Quickstart. You can bring your own application or get started by using the source code that has been prepared for this article and can be cloned from GitHub.
I’m using Java 11 and Maven 3.6.3. Build using mvn package
and verify the application runs as expected by using java -jar target/demo-application-0.0.1-SNAPSHOT.jar
(or just use mvn spring-boot:run
if you prefer).
Assuming all went well, you should be able to hit localhost:8080/hello in a browser to see your running Spring Boot application.
Now let’s work through options to turn this into a Docker image.