First, you need to apply the plugin to your build.gradle
.
plugins {
id "eu.xenit.docker" version "5.3.2" // See https://plugins.gradle.org/plugin/eu.xenit.docker for the latest version
}
Then you can configure the dockerBuild
configuration to set the Dockerfile, repositories and tags for the image that will be built.
If a Dockerfile
exists in the root of your project, it will be used as the Dockerfile for the image build.
Else, the Dockerfile generated by createDockerFile
will be used for the image build.
Repositories and tags are combined to produce the full image name that will be built. Repositories and tags are optional. If none (or an empty list) are provided, the image will not have a name and can't be pushed to a repository.
dockerBuild {
dockerFile = file('Dockerfile') // or `createDockerFile.destFile`
// Repositories to publish to.
// Full image names will be constructed by cartesian product of repositories and tags
repositories = ['xenit/example-docker-plugin', 'hub.xenit.eu/private/example-docker-plugin']
tags = ['1','1.0','test']
}
The createDockerFile
task is of type DockerfileWithCopyTask
.
It contains all methods of the Dockerfile
task type, and adds a smartCopy
method to easily add files.
You can build your whole Dockerfile programmatically with this task. This comes in handy when you want to add build artifacts to the Dockerfile, or want to generate it dynamically based on some custom logic.
createDockerFile {
from("debian")
runCommand("apt-get install default-jre")
volume("/data")
smartCopy(jar, "/opt/app/app.jar")
workingDir("/opt/app/")
defaultCommand("java", "-jar", "app.jar")
}
A smartCopy
method is available to make it easier to copy any file in the project to the Docker image.
smartCopy
works similar to copyFile
, except that paths are relative to the project directory instead of relative to the Dockerfile.
There is no limitation to the location of files that are copied with smartCopy
, files can originate from outside the project directory.
smartCopy
invocations
- single file or directory inputs:
smartCopy(String source, String destination)
: Evaluated assmartCopy(project.file(source), destination)
,source
is resolved usingProject#file()
.smartCopy(File source, String destination)
: Copies file or directorysource
todestination
in the Docker image.- If
destination
does not exist, thesource
will be copied to that path - If
destination
is an already existing directory,source
will be copied inside that directory. This is the same behavior as the Docker COPY instruction and the Unix cp command have.
- If
smartCopy(Provider<File> source, String destination)
smartCopy(Provider<File> source, Provider<String> destination)
- file collections:
smartCopy(FileCollection source, String destination)
: Copies the files insource
to the directorydestination
in the Docker image. Contrary to the single-file copy,destination
is forced to be a directory.smartCopy(FileCollection source, Provider<String> destination)
createDockerFile {
// Copy the contents of "directory123" in your project to /opt/directory123 in the Docker image
smartCopy "directory123", "/opt/directory123"
// Copy the file "file123" in your project to /opt/file123 in the Docker image
// Note: when copying a file and /opt/file123 is an already existing directory, the file will be copied to /opt/file123/file123
// This is standard behavior of both Docker COPY and the Unix cp command
smartCopy "file123", "/opt/file123"
// Copy all files of a FileCollection or Configuration into a folder
smartCopy files("lib"), "/tmp/classes"
smartCopy runtimeClasspath, "/tmp/classes"
}
The buildDockerImage
task is of type DockerBuildImage
.
It is automatically configured to use the Dockerfile that is configured in dockerBuild.dockerFile
and to tag the images with names generated from dockerBuild.repositories
and dockerBuild.tags
.
The pushDockerImage
task is of type DockerPushImage
.
It is automatically configured to push all the tagged images that are built by buildDockerImage
to their respective repositories.
An example for this plugin can be found in the docker plugin example
cd src/integrationTest/examples/example-docker-plugin
../../../../gradlew buildDockerImage