mvn: Automagically create a Docker image
Having a Docker image of your software projects may make things easier for you, but will for sure lower the barrier for users to use your tools — at least in many cases ;-)
I am developing many tools in Java using Maven to manage dependencies. Thus, I’ve been looking for means to generate corresponding Docker files using the very same build management. There are already a few approaches to build Docker images through Maven, e.g. alexec’s docker-maven-plugin, and fabric8io’s docker-maven-plugin … and so on — just to name a few. However, all theses solutions seem super-heavy and they require learning new syntax and stuff, while it is so easy and doesn’t require any third party plugins.
Build Docker images using maven-antrun
Maven’s antrun plugin allows for execution of external commands. That means, you just need to maintain a proper Dockerfile along with your sources and after building the tool with maven you can call the docker-build command to create a Docker image of the current version of your tool.
I did that for a Java web application. The Dockerfile is stored as a resource in src/main/docker/Dockerfile
is actually very simple:
Using Maven we can make sure (i) that the Dockerfile is copied next to the compiled and packaged tool in the target
directory, (ii) that the placeholder ${project.version}
in the Dockerfile is replaced with the current version of your tool, and (iii) that the docker-build command is invoked.
Copy the Dockerfile to the right place
Maven’s resources-plugin is ideally suited to deal with resources. To copy all Docker related resources to the target
directory you can use the following snippet:
In addition, the <filtering>true</filtering>
part also makes sure to replace all Maven-related placeholders, just like the ${project.version}
that we’ve been using. Thus, this solves (i) and (ii) and after the validate phase we’ll have a proper target/Dockerfile
.
Build a Docker image
Using Maven’s antrun-plugin we can call the docker tool:
This executes a command like
after the deploy phase.
Thus, it builds a docker image tagged with the current version of your tool. The build’s context is target
, so it will use the target/Dockerfile
which COPY
s the new version of your tool into the image.
Automatically build images using a Maven profile
I created a docker
profile in Maven’s configuration file that is active per default if there is a src/main/docker/Dockerfile
in your repository:
Bonus: Also push the new image to the Docker Hub
To also push the image you need to execute the push command:
And due to the latest
-confusion of Docker you also should create the latest-alias and also push that:
However, both is easy. Just append a few more exec
calls in the antrun-plugin!
The final pom.xml
snippet can be found on GitHub.
Supplement
The image for this article was derived from Wikipedia’s Apache Logo and Wikipedia’s Docker logo, licensed under the Apache License, Version 2.0.
Leave a comment
There are multiple options to leave a comment:
- send me an email
- submit a comment through the feedback page (anonymously via TOR)
- Fork this repo at GitHub, add your comment to the _data/comments directory and send me a pull request
- Fill the following form and Staticman will automagically create a pull request for you:
1 comment
Just a heads-up to everyone using this for spring-boot project: Instead of using ${project.version}, you have to use @project.version@. The original way will not work as a special case in spring boot.