Skip to main content
In this lesson you’ll learn how to use Docker containers as Jenkins pipeline agents. A Docker image is used to create a container and run pipeline stages inside it. This approach provides isolated, reproducible build environments for individual stages in a Declarative Pipeline.
Prerequisites:
  • Install the Jenkins Docker Pipeline plugin.
  • Ensure the agent node(s) intended to run Docker containers have the Docker Engine and the docker CLI installed.
  • Make sure the Jenkins agent process can execute Docker commands (for example, the agent user is in the docker group or the Docker socket is accessible).
A Jenkins web UI screenshot of the Installed Plugins page with a search for "docker" showing two Docker-related plugins (Docker Commons and Docker Pipeline) listed and enabled. The left sidebar shows navigation items like Updates, Available plugins, and Advanced settings.

How the Docker-based agent is configured

When using Declarative Pipelines, the agent block supports a docker declaration. Key options include: A minimal agent block to run inside a Docker image:

Example Jenkinsfile — Docker image as a stage agent

This Declarative Pipeline uses a global agent any for the pipeline, runs two stages on default agents, and a third stage inside a Docker container (node:18-alpine) on a labeled node.

What happens when the pipeline runs

  • When the pipeline reaches stage S3-Docker Image Agent, Jenkins schedules the build on a node that matches the label (ubuntu-docker-jdk17-node20).
  • Jenkins will pull the node:18-alpine image on that node (if alwaysPull is enabled, it pulls even if present).
  • Jenkins launches a container with the workspace mounted and environment variables forwarded from the agent.
  • Build steps in that stage execute inside the container.
  • After the stage completes, Jenkins stops and removes the container (default behavior). The pulled image remains cached on the node unless removed.
Pipeline execution overview (UI)
A dark‑theme Jenkins dashboard showing the "pipeline-external-agent" pipeline with a horizontal stage flow (Checkout SCM, S1-Any Agent, S2-Ubuntu Agent, S3-Docker Image) and green checkmarks for completed stages. The left sidebar shows pipeline actions (Status, Changes, Build Now, Configure) and a build history panel.

Representative console output

Jenkins logs docker pull and docker run operations in the build console. Example (trimmed) output for the Docker-based stage:

Notes on container lifecycle

  • The container is launched with docker run and mounts the Jenkins workspace so build files are available inside the container.
  • By default, the container is removed after the stage finishes. If you need to persist artifacts, use workspace steps (e.g., archiveArtifacts) or configure workspace behavior appropriately.
  • Images are cached on the node until explicitly removed with docker rmi.

Inspecting Docker on the agent node

After a run you can verify running and stopped containers as well as local images on the agent node:
If you need custom dependencies or a reproducible build environment, consider building a custom Docker image with a Dockerfile and using that image in your pipeline stage instead of relying on official base images.

Additional resources

Watch Video