Com.palantir.docker

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

Latest Version

https://plugins.gradle.org/m2/com/palantir/docker/com.palantir.docker.gradle.plugin/

build.gradle

Using plugin DSL:

plugins {
  id "com.palantir.docker" version "0.25.0"
}

Alternatively, use legacy format common section:

buildscript {

    ext {

        ...
        dockerGradleVersion = '0.21.0'
    }

    repositories {

        mavenCentral()

        maven {
            
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        ...
        classpath("gradle.plugin.com.palantir.gradle.docker:gradle-docker:${dockerGradleVersion}")
    }
}

ext {
    
    dockerNamespace = "com.example/playground"
}

...

apply plugin: 'com.palantir.docker'

...

Spring Boot Application Container

The docker plugin configuration that creates a SpringBoot application container. This configuration requires the common section, above.

docker {
    
  dependsOn build as Task

  // bootJar.baseName resolves to the artifact name, without version and extension information
  name "${dockerNamespace}/${bootJar.baseName}"

  dockerfile file('Dockerfile')

  // bootJar.archivePath resolves to the absolute path of the artifact file
  files bootJar.archivePath

  // bootJar.archiveName resolves to the artifact name, including version and extension
  buildArgs(['JAR_FILE': "${bootJar.archiveName}"])
}

The corresponding Dockerfile:

FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} /app.jar
RUN apk --no-cache add curl bash bind-tools
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Application Plugin Distribution Container

This is the docker plugin configuration that builds a container based on an application distribution built by the Application plugin. This configuration requires the common section, above.

docker {

    dependsOn distZip as Task

    //
    // define the distribution file
    //
    String distributionBaseName = ((DistributionContainer)project.extensions.getByName("distributions")).getByName("main").baseName;
    File distributionZip = new File(new File(project.buildDir, "distributions"), distributionBaseName + ".zip")

    name "${dockerNamespace}/${distributionBaseName}:latest"

    dockerfile file('Dockerfile')

    files distributionZip

    buildArgs(['DISTRIBUTION_ZIP': distributionZip.name])
}

The corresponding Dockerfile:

FROM openjdk:8-jdk-alpine
ARG DISTRIBUTION_ZIP
COPY ${DISTRIBUTION_ZIP} .
RUN apk --no-cache add curl bash bind-tools unzip; unzip ${DISTRIBUTION_ZIP}; rm ${DISTRIBUTION_ZIP}
ENTRYPOINT ["/oa2aws/bin/oa2aws"]

Idiosyncrasies

com.palantir.docker and Distribution Plugin

The output of dockerfileZip task is called project-name.zip by default. When the plugin is used together with the distribution plugin, they break each other. The distribution plugin will output its zip file with the same name, which gets overwritten by the dockerfileZip. Workaround is the following build.gradle snippet:

dockerfileZip {
  baseName 'dockerfile'
}

RUN Executables Failure is Not Reported

Under some non-elucidated circumstances, the Dockerfile RUN commands silently fail, but the build process is not aborted, leading to incomplete or invalid images. For example, using $(basename ...) failed silently.

Operations

Build the Image and Place it in the Local Registry

gradle clean docker

The command will build the container image and place into the local registry as "com.example/playground/myartifact" with the "latest" tag.

"Normalized" build Task

If the docker tasks belongs to a dedicated docker sub-project, that does not have a "build" task, the project can be "normalized" with a build task as follows:

task build {
    dependsOn getTasksByName("docker", false).asList().get(0)
}

Declare docker Tasks' Dependencies on other Tasks

In most cases it is necessary to first build the artifacts that will be package in the container image. The task dependency can be expressed as follows:

docker {
  ...
  dependsOn build
  ...
}

For more details, see:

Task Execution Order

Push to External Repository

gradle docker dockerPush

Avoid Unzipping as a Container Build Operation

When packaging a ZIP distribution, one of the steps is to unzip the distribution file. This is unnecessary, as Gradle build area already contains the unzipped contents. Come up with the recipe to use that and avoid RUN unzip ... in the Dockerfile. Some ideas are available here: https://github.com/palantir/gradle-docker.

Execution Internals

The build context is a local directory similar to /var/lib/docker/tmp/docker-builder036476946.