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:
1 2 |
docker --version Docker version 19.03.5, build 633a0ea |
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:
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
.jar
name – 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.10</version> <executions> <execution> <id>default</id> <phase>package</phase> <goals> <goal>build</goal> </goals> </execution> </executions> <configuration> <repository>javabycode/${project.artifactId}</repository> <tag>${project.version}</tag> <buildArgs> <JAR_FILE>${project.artifactId}-${project.version}.war</JAR_FILE> </buildArgs> </configuration> </plugin> |
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.
1 2 3 4 5 6 7 8 |
FROM openjdk:8-jdk-alpine MAINTAINER javabycode.com # JAR_FILE argument is defined already in pom.xml ARG JAR_FILE ADD target/${JAR_FILE} app.war ENTRYPOINT ["java","-jar","/app.war"] |
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
Then list that image in local using the command docker image ls
1 2 3 |
D:\Projects\SpringBoot-Freemarker-Example>docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE javabycode/springboot-freemarker-example 0.0.1-SNAPSHOT 3eafdb4138ca 1 minutes ago 120MB |
That’s all about the tutorial Spring Boot Docker using Maven Plugin.
References
Download complete source code, click link below
SpringBoot-Freemarker-Example-Docker-Maven.zip (128 downloads)