In building the required libraries for a docker container, using a maven project, the libraries have to be copied to a separate location and we have to manually run a docker build. This process is cumbersome since you have to repeat the same process over even if there is a slight modification.
fabric8 docker-maven-plugin is the perfect solution for this requirement. spotify also supports a docker plugin. However fabric8 provides much more functionalities. For example, with fabric8 it’s possible to inject values to the docker file with ease.
fabric8 provides a great source of documentation (https://dmp.fabric8.io/). However for a beginner, it could be challenging. My requirement was to configure the plugin in
such a way that would enable even a user without docker on his/her machine to do a maven build.
Configuring fabric8 docker-maven-plugin
The first step was to move the variable names to the POM so that configurations can be separated in order to improve maintainability.
<fabric8io.docker.version>0.21.0</fabric8io.docker.version>
<docker.registry>myregistry.malith.com:5000</docker.registry>
<docker.image.tag>latest</docker.image.tag>
<docker.repository>projectname:${docker.image.tag</docker.repository>
<docker.fileName>Dockerfile</docker.fileName>
<docker.skip>false</docker.skip>
Next under the <build><plugins> tag add the maven docker plugin.
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${fabric8io.docker.version}</version>
<configuration>
<registry>${docker.registry}</registry>
<useColor>true</useColor>
<verbose>true</verbose>
<skip>${docker.skip}</skip>
<images>
<image>
<name>${docker.registry}/${docker.repository}</name>
<build>
<dockerFileDir>${project.build.directory}/projectname-${project.version}-docker/projectname</dockerFileDir>
<filter>@</filter>
<dockerFile>${docker.fileName}</dockerFile>
<tags>
<tag>${docker.image.tag}</tag>
</tags>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<!-- "build" should be used to create the images with the artifact -->
<goal>build</goal>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
Next, I needed to skip the execution of the plugin if the user does not want to build the docker image. Therefore I created a profile, that would set the variable ${docker.skip} to true on the provision of the variable skipDocker.
I added the following section to the pom.
<profiles>
<profile>
<id>docker</id>
<properties>
<docker.skip>true</docker.skip>
</properties>
<activation>
<property>
<name>skipDocker</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
Based on the above code snippet, if skipDocker has been given as a maven directive, it would set docker.skip to true, effectively skipping the execution of the plugin.
Therefore a user can skip building the docker image simply by,
maven clean install -DskipDocker
Fabric8 has a lot of support for docker builds. I have a lot to explore. Hope you’ve found my findings useful. Don’t forget to leave a comment. 🙂