How to Use Docker Pipeline Plugin in Jenkins
Use the
docker global variable provided by the Docker Pipeline Plugin in Jenkins to build images, run containers, and execute Docker commands inside your pipeline scripts. Wrap Docker commands inside docker.image('image_name').inside { ... } blocks to run steps inside containers.Syntax
The Docker Pipeline Plugin provides a docker global variable in Jenkins pipeline scripts. You can use it to build images, run containers, and execute commands inside containers.
Key parts:
docker.build('imageName'): Builds a Docker image from a Dockerfile.docker.image('imageName'): Refers to an existing Docker image..inside { ... }: Runs the enclosed steps inside a container of the specified image..run('command'): Runs a container with a command.
groovy
pipeline {
agent any
stages {
stage('Build Image') {
steps {
script {
def myImage = docker.build('my-image:latest')
}
}
}
stage('Run Container') {
steps {
script {
docker.image('my-image:latest').inside {
sh 'echo Hello from inside the container'
}
}
}
}
}
}Example
This example shows a Jenkins pipeline that builds a Docker image from a Dockerfile and then runs a shell command inside a container of that image.
groovy
pipeline {
agent any
stages {
stage('Build Docker Image') {
steps {
script {
def appImage = docker.build('my-app:1.0')
}
}
}
stage('Run Command in Container') {
steps {
script {
docker.image('my-app:1.0').inside {
sh 'echo Running inside Docker container'
}
}
}
}
}
}Output
[Pipeline] echo
Running inside Docker container
[Pipeline] End of Pipeline
Common Pitfalls
Common mistakes when using the Docker Pipeline Plugin include:
- Not having Docker installed or accessible on the Jenkins agent.
- Forgetting to wrap commands inside
.inside { ... }to run them inside the container. - Using the
dockervariable outsidescriptblocks in declarative pipelines. - Not tagging images properly, causing confusion or overwriting.
groovy
pipeline {
agent any
stages {
stage('Wrong Usage') {
steps {
script {
// This will fail because docker.image().inside must be inside script block
docker.image('my-image').inside {
sh 'echo This will cause an error'
}
}
}
}
stage('Correct Usage') {
steps {
script {
docker.image('my-image').inside {
sh 'echo This works fine'
}
}
}
}
}
}Quick Reference
| Command | Description |
|---|---|
| docker.build('name') | Builds a Docker image from Dockerfile in workspace |
| docker.image('name') | References an existing Docker image |
| .inside { ... } | Runs enclosed steps inside a container of the image |
| .run('command') | Runs a container with a specific command |
| sh 'command' | Runs shell commands inside the container |
Key Takeaways
Use the docker global variable inside script blocks to access Docker Pipeline Plugin features.
Wrap commands inside docker.image('image').inside { ... } to run them inside containers.
Always ensure Docker is installed and accessible on Jenkins agents running the pipeline.
Tag images clearly to avoid confusion and overwriting.
Avoid using docker commands outside script blocks in declarative pipelines.