Packaging a Java Application in a Docker Image with Maven – Take 2, Fabric8

By | March 12, 2017

This article is an addition to my previous article on how to package a Java application in a Docker image with Maven. This time I will use the Fabric8 Docker Maven plug-in instead of the Spotify Dockerfile Maven plug-in, since I found out that it does support creation of Docker images from a Dockerfile. After having tried the Fabric8 plug-in out I also found that it is better than the Spotify plug-in in that it deletes any old images without having been explicitly told to. As in the previous article, the example will be a Spring Boot web application which will be packaged into a Docker image.


The preparations consists completing the example project I used in the previous article.

Maven Profile

To create the Docker image containing the Spring Boot application, I’ll use the following Maven plug-ins:

  • maven-resources-plug-in
    Copies files to a directory in which the Docker image will be built.
    The files needed to build the Docker image are copied to a directory in the target directory of the project.
  • maven-dependency-plug-in
    Copies the JAR file containing the application and any additional Maven artifacts to the Docker image build directory.
  • Fabric8 Docker Maven plug-in
    Builds the Docker image. The GitHub project is located here and the documentation can be found here.

As in the previous article, I use a Maven profile to contain what is needed to build the Docker image.
The updated Docker image building Maven profile looks like this:

Noteworthy about this new version of the profile:

  • The value of the property has changed.
    The reason for this is that the Fabric8 Docker plug-in uses the directory previously used, so I had to change it.
  • A property has been introduced.
    With the Fabric8 Docker plug-in you do not have to rely on an environment variable, but can configure the URL to the Docker host API in the Maven plug-in.
  • A property has been introduced.
    Although I do not expect this to change frequently, I created this property that specifies the name of the Dockerfile from which the Docker image is to be created.
  • The configuration of the Maven resources plug-in has not changed.
  • The configuration of the Maven dependencies plug-in has not changed.
  • The Spotify Docker-Maven plug-in has been removed.
    Since the Fabric8 plug-in is capable of deleting existing Docker images with the same name, the Spotify Docker-Maven plug-in is no longer needed.
  • Finally there is the Fabric8 Docker Maven plug-in.
    This is the plug-in responsible for building the Docker image. In fact, as configured in the example above, there will be two Docker images built; one with the tag being the same as the version of the project and one with the latest tag. If you do not want to create an image with the latest tag, remove the line containing <tag>latest</tag> in the configuration.
  • The latest version of the Fabric8 Docker Maven plug-in was, at the time of writing, 0.19.0.
    You may want to update to a more recent version if one is available.

Create the Docker Images

Creating the Docker images with the Fabric8 plug-in is a little different, at least with the configuration above.
Open a terminal window and go to the directory that contains the pom.xml file of the example Spring Boot web application and build the Docker image using this Maven command:

After some time, the message BUILD SUCCESS should appear in the terminal and if you list the Docker images, you should see two new images listed:

Use the Docker Images

The Docker images are launched in the same manner as described in the previous article. Depending on which of the two images you want to use, you change the tag accordingly.
To start a Docker container using one of the newly produced Docker images, I use the command:

I can also use the following command:

Happy coding!


Leave a Reply

Your email address will not be published. Required fields are marked *