DevOps

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.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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

1
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

1
2
3
4
5
6
7
8
9
<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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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.

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button