Skip to content

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

mvn clean install

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:

mvn clean install -DskipDocker

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

<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

mvn clean install -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:

<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:

<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:

<profile>
	<id>enableDocker-linux</id>
	<properties>
		<docker.skip>false</docker.skip>
	</properties>
	<activation>
		<!-- Activate on Linux systems. Assume presence of docker.pid file indicates Docker is available -->
		<file>
			<exists>/var/run/docker.pid</exists>
		</file>
	</activation>
</profile>

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.

<profile>
	<id>enableDocker-win10</id>
	<properties>
		<docker.skip>false</docker.skip>
	</properties>
	<activation>
		<!-- Activate on Windows 10 systems with native Docker. Check for docker.exe in default install directory -->
		<file>
			<exists>C:\Program Files\Docker\Docker\resources\bin\docker.exe</exists>
		</file>
	</activation>
</profile>

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:

<profile>
	<id>enableDocker-toolbox</id>
	<properties>
		<docker.skip>false</docker.skip>
	</properties>
	<activation>
			<!-- Activate on Windows (Docker Toolbox) systems. Assume presence of DOCKER_HOST indicates Docker is available -->
		<property>
			<name>env.DOCKER_HOST</name>
		</property>
	</activation>
</profile>

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

Published inDockerHow To

Be First to Comment

Leave a Reply

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