Spring Boot Docker using Maven Plugin, easy in 15 minutes

This tutorial introduces how to configure Spring Boot Docker using Maven Plugin. This way is an improvement for the one you just use Dockerfile only to create Docker image.  Let’s dig deeper into this tutorial.

Spring Boot Docker using Maven Plugin

Docker installation

This post doesn’t focus on how to install Docker on a local machine. If your OS is Windows, please refer to the installation guide Docker for Windows. Otherwise, you can choose the correct installer for your OS and follow the instructions here.

Since you have done the installation. You can very your Docker version like this:

Now, you have Docker installed on local.

Spring Boot application

This post just focuses on Docker so we will use the existed project from the post Spring Boot FreeMarker Hello World Example. This is a standalone application, like this:

Spring Boot FreeMarker Hello World Example


Why Maven Plugin?

In the previous post, we introduced how to build spring boot application using the Dockerfile. However, there are some issues that you may encounter in your projects using this method:

  • The.jarname – The jar name is mentioned with a version in the command of Dockerfile. But that version is usually changed when the application grows then the jar name is updated again and again manually.
  • Using the terminal – We must open a console or other tools support it and do command manually. If your project is in the CI/CD (Continous Integration/Continous Delivery) pipelines you really don’t want to do something manually like that.   You can do it automatically if you use Maven plugins.

There are some Maven plugins that help you fix two issues above. Let’s discover how to use it.

Maven plugin configuration

Here, we introduce to you a plugin that can help you is the dockerfile-maven-plugin. It  allow you to build Docker images.

This configuration makes Maven build an image and use the artifact ID as the name and tag it with the version.

Note: artifactId in pom.xml should be lowercase to avoid some Docker issues.

Dockerfile defines a image

The Dockerfile is simple like this. You maybe see the JAR_FILE argument that point to the directory of jar file and is declared already in pom.xml file. So now you haven’t concern about the jar filename changed.

Build Docker image using Maven

It’s time to test what you just do configuration above. Hit the command mvn package on your console and it prints output like this

Spring Boot Docker using Maven Plugin

Then list that image in local using the command docker image ls

That’s all about the tutorial Spring Boot Docker using Maven Plugin.


Dockerfile Maven

Download complete source code, click link below

SpringBoot-Freemarker-Example-Docker-Maven.zip (128 downloads)

Please share it if you found this useful
Hide Buttons