In order to create a Docker image, the "Docker Pipeline plugin" also provides a build() method for creating a new image, from a Dockerfile in the repository, during a Pipeline run.
One major benefit of using the syntax docker.build("my-image-name") is that a Scripted Pipeline can use the return value for subsequent Docker Pipeline calls.
For example:
node {
checkout scm
def customImage = docker.build("my-image:${env.BUILD_ID}")
customImage.inside {
sh 'make test'
}
}
The return value can also be used to publish the Docker image to Docker Hub, or a custom registry, via the push() method, as shown:
node {
checkout scm
def customImage = docker.build("my-image:${env.BUILD_ID}")
customImage.push()
}
One common usage of image "tags" is to specify a latest tag for the most recently, validated, version of a Docker image.
The push()method accepts an optional tag parameter, allowing the Pipeline to push the customImage with different tags,
For example:
node {
checkout scm
def customImage = docker.build("my-image:${env.BUILD_ID}")
customImage.push()
customImage.push('latest')
}
The build() method builds the Dockerfile in the current directory by default.
This can be overridden by providing a directory path containing a Dockerfile as the second argument of the build() method, For example:
node {
checkout scm
def testImage = docker.build("test-image", "./dockerfiles/test")
testImage.inside {
sh 'make test'
}
}
This Builds test-image from the Dockerfile found at ./dockerfiles/test/Dockerfile.