Maven-Based Docker Projects

An explanation of how we wrap all of our Docker-based projects in maven project structures for consistency and simplicity in our build pipelines.

6 min read
Maven-Based Docker Projects

At Single, we wrap almost all of our docker builds in maven projects. Maven is a Java-based build and dependency management tool. While its main use is for JVM-based projects, it can also be used alongside docker to build images and run containers, which is the approach we will be discussing here.


The main reason we decided to wrap all of our Docker-based projects in maven projects is for consistency. The same command will create a docker container no matter what kind of component is being built — whether that’s a microservice, a database, or an Angular-based UI project. Similarly, when developers want to run the containers locally, one command will spin up any project. Aside from the ease of use on the development side, this consistency in project configuration makes our CI/CD configuration and scripts much simpler. The CI process only needs to have java available and know how to execute maven commands to build Docker images.

An example

We’ve created an example project to demonstrate what our project setup looks like for building docker containers. Let’s examine the pieces of the project to show how we have it all setup.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">


    <description>Example demonstrating maven project structure for docker builds</description>



        <>single music</>

            <!-- tag::docker[] -->

            <!-- end::docker[] -->

The POM (Project Object Model) file is the main configuration file for a maven project. For those who aren’t familiar, it’s where you define the project dependencies and build configuration. The important part for our purposes here is the build plugin configuration. You can see we’re making use of the docker-maven-plugin from fabric8. We give our image a name and an alias for the network which we'll discuss later. In the build configuration we have arguments, the location of our Dockerfile, and our image tags. In the run configuration we have our restart policy, our container naming strategy, the network configuration and the any port mappings. In the execution block we bind the docker plugin's build goal to maven's install goal (More on that later).


FROM library/nginx


LABEL org.label-schema.vcs-url="${VCS_URL}"
LABEL org.label-schema.vcs-ref="${VCS_REF}"
LABEL org.label-schema.vendor="${VENDOR}"
LABEL org.label-schema.version="${VERSION}"
LABEL org.label-schema.schema-version="${SCHEMA_VERSION}"

COPY container/ /

For this example, our Dockerfile is rather simple. We start with a base nginx image. Then, we declare arguments and use them to define labels. These are the same arguments present in the build configuration in the pom file. The last line copies the contents of our container folder to the root directory of the image.


        <h1>HELLO WORLD</h1>

This wouldn’t be a good example project without a hello world file. So, here it is — a simple html file that will end up in the container at /usr/share/nginx/html , the default location for files to be served up by nginx.

For our simple example, all we need is these three files.

Building the docker image

Since we have bound the build goal of the docker-maven-plugin to maven's install goal, we can build the image by executing the maven wrapper in the project’s root directory like:

./mvnw clean install

This will result in an image being created named nginx-docker-example which matches the name we have defined in our docker plugin configuration in the pom.xml. It will have the tag latest which we also have defined in the pom configuration.

To set values for the labels we defined in the Dockerfile, you have to use a more verbose version of the same command. For example, this is what our maven install script looks like in our Gitlab CI build containers:

mvn clean install $@ -B \\ \\"${DATE_RFC_3339}" \\"${VCS_URL}" \\"${VCS_REF}" \\"${VENDOR}" \\"${PROJECT_VERSION}" \\"${SCHEMA_VERSION}"

Gitlab defines some of those environment variables, and others we define ourselves earlier in the script. Notice the name of the flags match names defined in the properties section of our pom.xml file. They are later referenced in the args section of the maven-docker-plugin with the ${...} syntax. You could follow a similar pattern to add additional labels.

Running the docker image

The maven-docker-plugin provides a few other goals, one of which is the start command, which we can invoke by executing:

./mvnw docker:start

This will start up a container of our built image with the configuration we defined in the run configuration of our POM file. It will be running with host port 8080 bound to the container port 80. If you navigate to https://localhost:8080 you should see our “Hello World” page.

If you get an error about the local-network not existing, you may have to create it with:

docker network create --attachable local-network


That’s pretty much it! We apply this maven-based project pattern to most of our projects. You may think, “Goodness, that’s a lot of XML”, or “Just make a bash script that executes docker build,” and these are all very valid considerations — however, what’s really important is that there is consistency across your projects. Keeping all of our projects maven-based is how we avoid having many different configurations that require their own, special build processes.

Related Articles

Logging Strategies
5 min read
The Single CLI
3 min read
Using Embedded Mysql in JUnit Tests
7 min read

Back To Top

🎉 You've successfully subscribed to Single Technology!