Skip to content

jazzschmidt/gradle-docker-plugin

Repository files navigation

Gradle Docker Plugin

CI

Simple to use Gradle Plugin to build, tag and push Docker Images. Supports Multi-Module-Projects, build caching, Java 8 and runs on Gradle 6+ and 7+.

Usage

Apply the plugin:

plugins {
    id 'com.github.jazzschmidt.gradle-docker-plugin' version '<version>'
}

All projects that have a docker directory will be configured with the docker tasks.

Suppose the following project structure:

  • :root: project
    • build.gradle
    • :example: project
      • docker/
        • Dockerfile
        • config.toml
      • src/main/java
        • ...
      • build.gradle

With the following build.gradle:

// Project :root:
plugins {
    id 'com.github.jazzschmidt.gradle-docker-plugin' version '1.0.1'
}

group 'com.example'
version '0.0.1'

// Define custom docker registries
dockerRegistry {
    uploadTo("registry", "my-registry.com")
}
// Project :example:
plugins {
    id 'java'
}

group 'com.example'
version '0.0.1'

dockerImage {
    from(jar)
    
    tag "nexus", "nexus.local/${project.name}" // Omit the version to use project version
    tag "nexusLatest", "nexus.local/${project.name}:latest"
}

This will generate the following tasks in :example: project in the docker group:

  • docker - builds the Docker image
  • dockerAssemble - assembles resources for the image in build/docker
  • dockerClean - cleans the resources and all tags
  • dockerTag - creates all tags
  • dockerTagRegistry - tags my-registry.com/example:0.0.1
  • dockerTagNexus - tags nexus.local/example:0.0.1
  • dockerTagNexusLatest - tags nexus.local/example:latest
  • dockerPush - pushes all tags

and tasks for pushing specific tags:

  • dockerPushRegistry
  • dockerPushNexus
  • dockerPushNexusLatest

Any file in the docker directory will be used to assemble the Docker Image. The plugin adds a dockerImage extension, that can be used to add further dependencies to the Docker build and define custom tags. As for the example above, dockerAssemble will create something like this:

  • :example: project
    • build/docker
      • Dockerfile
      • config.toml
      • example-0.0.1.jar