{{announcement.body}}
{{announcement.title}}

Build Docker Image From Maven

DZone 's Guide to

Build Docker Image From Maven

See how to automate creating a docker image from your Java application and push it to a docker repository using Maven

· Java Zone ·
Free Resource

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.
  • 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. 

Step 2

In your pom.xml, under  <builds><plugins> ..., add the following plugin:

XML
 







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!

Topics:
docker ,java ,maven 3.5.0 ,pom.xml ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}