Fabric8 Docker Maven Plugin
0 CommentsLast Updated on September 14, 2022 by Simanta
Modern Enterprise application development using the Spring framework makes use of containerization technology, such as Docker to ship and run applications. Building an application, packaging it, then building the image, and pushing images to an image registry is cumbersome if done manually. In addition, each time you make any changes to the codebase, you need to repeat those steps. You can automate those steps using the Fabric8 Docker Maven Plugin.
In my previous posts on this series, I discussed how to run Spring Boot in a Docker container and how to use Docker Hub to manage images and automate builds.
In this post, I’ll take you through the steps to configure and use the Fabric8 Docker Maven Plugin in Spring Boot applications.
The Maven Pom
The code to configure the docker-maven-plugin
plugin in the pom.xml file is this.
<plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.33.0</version> <configuration> <verbose>true</verbose> <images> <image> <name>${docker.image.prefix}/${docker.image.name}</name> <build> <dockerFileDir>${project.basedir}</dockerFileDir> <assembly> <descriptorRef>artifact</descriptorRef> </assembly> <tags> <tag>1.0.1</tag> </tags> </build> </image> </images> </configuration> <executions> <execution> <id>default</id> <phase>install</phase> <goals> <goal>build</goal> <goal>push</goal> </goals> </execution> </executions> </plugin>
The <name>
tag in Line 10 specifies the image prefix and name. The values are picked from the <properties>
section of the POM, which I will discuss next.
The <build>
section in <image>
specifies the image to build. Line 12 specifies the directory of Dockerfile
. In this example, it is set to the project base directory.
Line 12 specifies the tag for the image.
Finally, the <execution>
section binds the build and push goals of the plugin with the install phase of the default lifecycle ”
Next, specify the Docker image prefix and name in the <properties>
section.
<properties> <docker.image.prefix>prefix</docker.image.prefix> <docker.image.name>springboot-docker</docker.image.name> </properties>
The Maven Settings
In order to push images to Docker Hub, the plugin needs the credentials of Docker Hub.
You provide the credentials in the .m2/settings.xml
file.
The settings.xml file is this.
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <localRepository/> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups> <pluginGroup>io.fabric8</pluginGroup> <pluginGroup>org.springframework.boot</pluginGroup> </pluginGroups> <servers> <server> <id>docker-hub</id> <registry>docker.io</registry> <username>USERNAME</username> <password>PASSWORD</password> <configuration> <email>EMAIL</email> </configuration> </server> </servers> <mirrors/> <proxies/> <profiles/> <activeProfiles/> </settings>
Note: Replace the USERNAME
, PASSWORD
, and EMAIL
with your credentials.
Run the Fabric8 Docker Maven Plugin Goals
The configuration is ready. Now, you need to perform the following steps:
Clean the project
- Package the project into a Spring Boot JAR file.
- Use the plugin to build the Docker image.
- Use the plugin to push the image to Docker Hub.
Once you configure the plugin, all the preceding steps can be done with a single command, like this.
$ mvn install
Summary
The Fabric8 Docker Maven Plugin is also commonly used to perform integration tests on Docker containers. This topic deserves a separate blog, which I plan to take up in this series.
An alternative to the Fabric8 Docker Maven Plugin is the Spotify Dockerfile Maven. You can use this plugin to optimize Docker cache of the Maven dependencies in your image. Doing so subsequently vastly speeds up your builds.
To learn how to use Docker to supercharge your Enterprise Java Development, check the Udemy Docker for Java Developers course.
The source code for this post can be found here on GitHub.