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
Newsletter
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

  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • Why Spark Executors Leave Cores Idle on YARN: A Packing Model for Executor Sizing
  • PWA on iOS, Part 2: HTTPS, Manifest and Service Worker
  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Build Docker Images With Maven and Gradle

Build Docker Images With Maven and Gradle

If you are using Docker and Java, you might want to build the image from a Dockerfile in your build tool. In this post, we discuss how to do this in both cases.

By 
Alex Soto
·
Oct. 14, 16 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
31.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

One of the things that you might want to do if you are using Docker and Java is to build the image from a Dockerfile in your build tool, such as Maven or Gradle. In this post, I am going to show you how to do it in both cases. I am going to assume that you have the de-facto project layout, having the Dockerfile file at the root of the project.

Maven

There are several Maven plugins that can be used for building a Docker image in Maven, but one of the most used is the fabric8-maven-plugin.

To start, you need to register and configure the plugin in pom.xml:

<build>
  <plugins>
    <plugin>
      <groupId>io.fabric8</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>0.16.7</version>
      <configuration>
        <images>
          <image>
            <name>arquillian/age-checker:${project.version}</name>
            <build>
              <dockerFileDir>${project.basedir}</dockerFileDir>
            </build>
          </image>
        </images>
      </configuration>
    </plugin>
  </plugins>
</build>

In the configuration section, you set the image name and the directory where the Dockerfile is located.

Any additional files located in the dockerFileDir directory will also be added to the build context. Since Dockerfile is the root of the project, the target directory is added too. The problem arises because this plugin uses the target and Docker to generate the build, and if you try to build it, you'll get next exception: tar file cannot include itself. To avoid this problem, you need to create a .maven-dockerignore file specifying which directory must be ignored at the same level as the Dockerfile:

target/docker/

And that's all! After that, you can do:

mvn package docker:build

Notice that this plugin honors Docker environment variables such as DOCKER_HOST, DOCKER_CERT_PATH, etc., so if your environment is correctly configured, then you don't need to do anything else.

Gradle

There are several Gradle plugins that can be used for building a Docker image in Gradle, but one of the most used is gradle-docker-plugin.

To start you need to register and configure the plugin in build.gradle:

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.bmuschko:gradle-docker-plugin:3.0.3'
    }
}

apply plugin: 'com.bmuschko.docker-remote-api'
import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage

docker {
    if (System.env.containsKey('DOCKER_HOST') && System.env.containsKey('DOCKER_CERT_PATH')) {
        url = System.env.DOCKER_HOST.replace("tcp", "https")
        certPath = new File(System.env.DOCKER_CERT_PATH)
    }
}

task buildImage(type: DockerBuildImage) {
    dependsOn assemble
    inputDir = project.rootDir
    tag = "arquillian/game-service:${project.version}"
}

In the case of Gradle, you need to configure Docker host properties since the plugin does not honor Docker environment variables. You need to configure them in docker {} block.

Finally, you create a task of type DockerBuildImage in which you set the Dockerfile root directory using inputDir attribute and image name using tag attribute.

Conclusion

In this post, you've seen different ways of doing the same in two different build tools, which is building a Docker image from a Dockerfile. Notice that these plugins also allow you to define the Dockerfile content as a configuration field, so you are not creating a Dockerfile file, but specifying its content inside the build tool. You can read more about this feature here in the case of Maven plugins and here in the case of Gradle.

Docker (software) Build (game engine) Gradle Apache Maven

Published at DZone with permission of Alex Soto. See the original article here.

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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