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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • Using Environment Variable With Angular
  • Kubernetes Remote Development in Java Using Kubernetes Maven Plugin

Trending

  • How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
  • Simpler Data Transfer Objects With Java Records
  • Assessing Bias in AI Chatbot Responses
  • Mastering Deployment Strategies: Navigating the Path to Seamless Software Releases
  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
DZone Core CORE ·
May. 11, 20 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
84.1K 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

  • Fast Deployments of Microservices Using Ansible and Kubernetes
  • Ensuring Reliable Microservice Deployment With Spring Boot Build Info Maven Plugin
  • Using Environment Variable With Angular
  • Kubernetes Remote Development in Java Using Kubernetes Maven Plugin

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!