"IT is like a shark, if you stop moving you will die!"

Vladimir Dejanović

How to Docker-ise your Java application with Maven plugin

01 Apr 2018     2 min read

There are two maven plugins that you can use, in order to add your Java Applicatoin into Docker in an easy way.

First plugin that you can use is docker-maven-plugin from Fabric8

Second is docker-maven-plugin from Spotify

Let me show you how to do it by using Fabric8 plugin.

Update pom.xml

Just add configuration like this into your pom.xml, in build part, plugins.

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.20.1</version>
    <configuration>
        <images>
            <image>
                <name>${project.name}:${project.version}</name>
                <build>
                    <from>openjdk:9</from>
                    <assembly>
                        <descriptorRef>artifact</descriptorRef>
                    </assembly>
                    <ports>
                        <port>8080</port>
                    </ports>
                    <cmd>java -jar maven/${project.name}-${project.version}.jar</cmd>
                </build>
                <run>
                    <ports>
                        <port>8080:8080</port>
                    </ports>
                </run>
            </image>
        </images>
    </configuration>
</plugin>

In this case, I tell to plugin to use base docker image openjdk tag 9, and to create docker image by the name ${project.name} with tag ${project.version}. You can of course hard-code name here, if you don't want for it to be changed.

Once container is started it will execute command

  • java -jar maven/${project.name}-${project.version}.jar

in my case, it will start stand alone web application. Because, this is web application, I will also expose port 8080.

Build image

In order to create docker image, all that is needed now, is to run this command

 mvn docker:build

Connect docker image build to Maven phase

Sometimes, it is better to connect building of image with some phase in maven process. In order to do so, you just need to add this in plugin

    <executions>
        <execution>
            <id>docker:build</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>

Now maven is instructed to always create docker image as part of package phase.

End result

Full plugin added to pom.xml should look like this

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.20.1</version>
    <configuration>
        <images>
            <image>
                <name>${project.name}:${project.version}</name>
                <build>
                    <from>openjdk:9</from>
                    <assembly>
                        <descriptorRef>artifact</descriptorRef>
                    </assembly>
                    <ports>
                        <port>8080</port>
                    </ports>
                    <cmd>java -jar maven/${project.name}-${project.version}.jar</cmd>
                </build>
                <run>
                    <ports>
                        <port>8080:8080</port>
                    </ports>
                </run>
            </image>
        </images>
    </configuration>
    <executions>
        <execution>
            <id>docker:build</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Working Example

You can check all of this in action here