Activate the Docker Maven plugin when Docker is present

The wonderful docker-maven-plugin from Spotify is a great way to build Docker images from Maven. If you bind it to Maven phases, it can be used to make a one-step build of a project artifact and its Docker image. For example, if you bind the Docker Maven plugin’s build goal to the Maven package phase, it will create your Docker image when you run a standard

MS DOS

1

mvncleaninstall

command. That’s neat, but the drawback is that the build will fail entirely if Docker is not available on the build machine. This somewhat goes against the Maven ideal of portable builds – we don’t want a build that works on my machine but not yours.

We can workaround this problem by making the Docker build optional and enabling only if Docker is available.

Simple Solution: Enable Docker Maven plugin with a system property

The
docker-maven-plugin has a handy configuration option:
skipDocker. If this is set to true, the plugin is skipped entirely. We can therefore disable skip Docker by running Maven commands with this option set:

MS DOS

1

mvncleaninstall -DskipDocker

Better yet, we can skip Docker by default and run it only if its explicitly enabled.

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<properties>

<docker.skip>true</docker.skip>

</properties>

...

<plugin>

<groupId>com.spotify</groupId>

<artifactId>docker-maven-plugin</artifactId>

<version>0.4.13</version>

<configuration>

<skipDocker>${docker.skip}</skipDocker>

...

</configuration>

</plugin>

It can then be enabled at command line with

MS DOS

1

mvncleaninstall -Ddocker.skip=false

If we always want to enable the Docker build on this machine, we can add a profile to our
settings.xml like this:

XHTML

1

2

3

4

5

6

7

8

9

<profile>

<id>enableDocker</id>

<properties>

<docker.skip>false</docker.skip>

</properties>

<activation>

<activeByDefault>true</activeByDefault>

</activation>

</profile>

Advanced solution: detect Docker

An ideal solution would be to automatically activate Docker if Docker is present on the build machine. Unfortunately, that’s not an option in the plugin nor is there a straightforward way to get Maven to detect Docker. We can however check for files or environment variables that would normally only be set by Docker. This isn’t foolproof and isn’t guaranteed to correctly detect Docker but it’s a good start.

Again, our default is to skip Docker:

XHTML

1

2

3

<properties>

<docker.skip>true</docker.skip>

</properties>

Then we use Maven profile activation to enable docker if we can detect its presence. The detection is a little awkward – it depends on the host OS. Here are three profiles to activate Docker on various environments.

Linux

On a standard Linux host, Docker uses files in
/var/run/. Checking for the presence of
docker.pid is a reasonable signal that Docker is present:

This will obviously fail if Docker is installed to a different drive or not in the default location.

Legacy Windows (Docker Toolbox)

Docker can run in the Docker Toolbox on Windows (or Mac) environments that cannot run Docker natively. The Docker Toolbox sets up various environment variables so checking for the presence of one of these is a good marker:

If all else fails

If these profiles don’t correctly detect Docker, it’s still possible to activate it by falling back to the simple solution: set
-Ddocker.skip=false at command line or add your own profile to your
settings.xml.