DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Containers: Docker-First Mobile Build Pipelines (Android and iOS) — End-to-End from Code to Artifact
  • Slimming Down Docker Images: Base Image Choices and The Power of Multi-Stage Builds
  • Expert Techniques to Trim Your Docker Images and Speed Up Build Times
  • Docker Multi-Stage Builds: Optimizing Development and Production Workflows

Trending

  • 5 Failure Patterns That Break AI Chatbots in Production
  • Spring AI Advisors: Chat Memory, Token Tracking, and Message Logging
  • AI Assessments Are Everywhere
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Build Docker Image From Maven

Build Docker Image From Maven

By 
Preetdeep Kumar user avatar
Preetdeep Kumar
·
May. 11, 20 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
85.8K Views

Join the DZone community and get the full member experience.

Join For Free

Overview

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:

XML
 




x
82


 
1
<plugin>
2
  <groupId>org.codehaus.mojo</groupId>
3
  <artifactId>exec-maven-plugin</artifactId>
4
  <version>1.6.0</version>
5
  <executions>
6
      <!-- Remove existing image from local repo -->
7
    <execution>
8
      <id>docker-clean</id>
9
      <phase>install</phase>
10
      <goals>
11
        <goal>exec</goal>
12
      </goals>
13
      <configuration>
14
        <executable>docker</executable>
15
        <workingDirectory>${project.basedir}</workingDirectory>
16
        <arguments>
17
          <argument>rmi</argument>
18
          <argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
19
        </arguments>
20
      </configuration>
21
    </execution>
22
        
23
    <!-- 
24
      Create new docker image using Dockerfile which must be present in current working directory.
25
      Tag the image using maven project version information.
26
    -->
27
    <execution>
28
      <id>docker-build</id>
29
      <phase>install</phase>
30
      <goals>
31
        <goal>exec</goal>
32
      </goals>
33
      <configuration>
34
        <executable>docker</executable>
35
        <workingDirectory>${project.basedir}</workingDirectory>
36
        <arguments>
37
          <argument>build</argument>
38
          <argument>-t</argument>
39
          <argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
40
          <argument>.</argument>
41
        </arguments>
42
      </configuration>
43
    </execution>
44
        
45
        <!-- Login and Push the image to a docker repo. -->
46
    <execution>
47
      <id>docker-login</id>
48
      <phase>deploy</phase>
49
      <goals>
50
        <goal>exec</goal>
51
      </goals>
52
      <configuration>
53
        <executable>docker</executable>
54
        <workingDirectory>${project.basedir}</workingDirectory>
55
        <arguments>
56
          <argument>login</argument>
57
          <argument>-u</argument>
58
          <argument>${docker.user}</argument>
59
          <argument>-p</argument>
60
          <argument>${docker.password}</argument>
61
          <argument>${docker.url}</argument>
62
        </arguments>
63
      </configuration>
64
    </execution>
65
    <execution>
66
      <id>docker-push</id>
67
      <phase>deploy</phase>
68
      <goals>
69
        <goal>exec</goal>
70
      </goals>
71
      <configuration>
72
        <executable>docker</executable>
73
        <workingDirectory>${project.basedir}</workingDirectory>
74
        <arguments>
75
          <argument>push</argument>
76
          <argument>${project.groupId}/${project.artifactId}:${project.version}</argument>
77
        </arguments>
78
      </configuration>
79
    </execution>
80
  </executions>
81
</plugin>
82

          


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!

Docker (software) Apache Maven Build (game engine)

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Containers: Docker-First Mobile Build Pipelines (Android and iOS) — End-to-End from Code to Artifact
  • Slimming Down Docker Images: Base Image Choices and The Power of Multi-Stage Builds
  • Expert Techniques to Trim Your Docker Images and Speed Up Build Times
  • Docker Multi-Stage Builds: Optimizing Development and Production Workflows

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook