Com.palantir.docker: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 51: Line 51:
docker {
docker {
      
      
   dependsOn build
   dependsOn build as Task


   // bootJar.baseName resolves to the artifact name, without version and extension information
   // bootJar.baseName resolves to the artifact name, without version and extension information

Revision as of 07:12, 28 February 2019

External

Internal

Overview

Latest Version

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

Example

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'

...

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"]

Configuration

The plugin configuration is applied in a docker {} script block. The following parameters are available:

name

The container name. Includes the namespace and may include a tag. Example:

docker {
  ...
  name com.example/my-container:latest
  ...
}

name the name to use for this container, may include a tag

dockerfile

The dockerfile to use for building the image. It defaults to project.file('Dockerfile') and must be a file object.

docker {
  ...
  dockerfile file('Dockerfile')
  ...
}

files

An argument list of files to be included in the Docker build context, evaluated per Project#files

docker {
  ...
  bootJar.archivePath, './build/themyscira-openapi-with-aws-extensions.json'
  ...
}

buildArgs

An argument map of string-to-string which will set --build-arg arguments to the docker build command and pass Docker build-time variables. It defaults to empty, which results in no --build-arg parameters.

Single argument:

docker {
  ...
  buildArgs(['DISTRIBUTION_FILE': distribution.name])
}

Multiple arguments:

docker {
  ...
  buildArgs(['DISTRIBUTION_FILE': distribution.name, 'DISTRIBUTION_IMAGE_DIR': distributionImageDir])
}

The arguments passed as such can then be used in the Dockerfile as follows:

...
ARG DISTRIBUTION_FILE
ARG DISTRIBUTION_IMAGE_DIR
COPY ${DISTRIBUTION_FILE} ${DISTRIBUTION_IMAGE_DIR}/${DISTRIBUTION_FILE}
...

More details about Docker build-time variables:

Docker Build-Time Variables

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

Execution Internals

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