Home » DevOps » How to Docker-ise your Java application with Maven plugin

About Vladimir Dejanovic

Vladimir Dejanovic
IT Consultant, Software Architect, Team Lead and Chapter Lead working in industry since 2006 developing high performance software in multiple programming languages and technologies from desktop to mobile and web with high load traffic. Enjoining developing software mostly in Java and JavaScript, however also wrote fair share of code in Scala, C++, C, PHP, Go, Objective-C, Python, R, Lisp and many others.

How to Docker-ise your Java application with Maven plugin

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

Published on Java Code Geeks with permission by Vladimir Dejanović, partner at our JCG program. See the original article here: How to Docker-ise your Java application with Maven plugin

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of