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

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:

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

It can then be enabled at command line with

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

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:

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:

Windows 10 (Native Docker)

On Windows 10 running Docker natively through the Windows OS, detection is a little tricky. The best we can do is check for the presence of Docker in the default installation directory.

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.

Source code used in this post taken from Spanners 4.5

Leave a Reply

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