Build Docker Image From Maven
Join the DZone community and get the full member experience.
Join For FreeOverview
In this post, I share how you can automate creating a docker image from your Java application and push it to a docker repository using Maven. You can also tie the various steps to maven goals to control when an image should be created. There are other ways too, but I found this one to be very easy for developers.
Prerequisites
You should have the following installed:
- Docker. Read DZone's Refcard on getting started with Docker
- JDK.
- Maven.
- Dockerfile.
- Access to a Docker repo (optional, only if you want the image to be pushed to a remote repo).
Step 1
Define a Dockerfile and place it under your Maven project root folder where pom.xml resides.
DZone's previously covered how to publish Maven artifacts using Jenkins.
Step 2
In your pom.xml, under <builds><plugins> ...
, add the following plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<!-- Remove existing image from local repo -->
<execution>
<id>docker-clean</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>rmi</argument>
<argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
</arguments>
</configuration>
</execution>
<!--
Create new docker image using Dockerfile which must be present in current working directory.
Tag the image using maven project version information.
-->
<execution>
<id>docker-build</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>build</argument>
<argument>-t</argument>
<argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
<argument>.</argument>
</arguments>
</configuration>
</execution>
<!-- Login and Push the image to a docker repo. -->
<execution>
<id>docker-login</id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>login</argument>
<argument>-u</argument>
<argument>${docker.user}</argument>
<argument>-p</argument>
<argument>${docker.password}</argument>
<argument>${docker.url}</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>docker-push</id>
<phase>deploy</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>push</argument>
<argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Related Tutorial: How to deploy a mule application
Step 3
To compile and build the Docker image locally:
mvn clean install
To compile, build, and push the image to a remote repo:
mvn clean deploy -Ddocker.user=<username> -Ddocker.password=<passwd>
-Ddocker.url=<docker-registry-url>
And that's it! If you have any questions or critique, please leave a comment below!
Opinions expressed by DZone contributors are their own.
Comments