In this guide, you’ll learn how to configure Jenkins Pipeline stages to run inside Docker containers using the Docker Pipeline Plugin. By the end, you’ll be able to pull official images (e.g., Node.js 18 Alpine) or build custom images from a Dockerfile, schedule them on specific nodes, and inspect the full console output.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
Ensure you have the Docker Pipeline Plugin installed and enabled:
Configuring a Docker-Based Agent
Navigate to Pipeline Syntax → Agent to generate the DSL snippet. You can choose:| Directive | Description |
|---|---|
| dockerFile | Build a custom image from a local Dockerfile. |
| docker | Pull and run a prebuilt Docker image. |
docker, you can configure:
| Parameter | Purpose |
|---|---|
image | Name of the Docker image (e.g., node:18-alpine). |
args | Additional flags for docker run. |
label | (Optional) Restrict execution to nodes with this label. |
customWorkspace | (Optional) Override the workspace directory inside the container. |
registryUrl | (Optional) Private registry URL. |
credentialsId | (Optional) Credentials for private registry. |
alwaysPull | Force image pull on each build. |
Use
alwaysPull true to ensure the latest image version is used, preventing stale caches.
Example Pipeline
Below is a sampleJenkinsfile demonstrating three stages:
Adding a Docker Image Agent to Stage S3
To run S3-Docker Image Agent inside the official Node.js 18 Alpine container, update that stage:Scheduling on a Specific Node
If you need the Docker container to run on a labeled node (for example, one with JDK 17 and Node 20), add thelabel directive:
Jenkinsfile to your repository, then trigger the build in Jenkins:

Build Console Output
When the pipeline runs, Jenkins will:- Pull the Docker image if it’s not already available.
- Create and start a container on the designated agent.
- Execute the shell steps inside that container.
- Stop and remove the container once finished.
Verifying on the Jenkins Agent
After completion, confirm no containers are running:Conclusion
You’ve configured a Jenkins Pipeline stage to execute inside a Docker container using a prebuilt image. For more control over dependencies and environment setup, consider building custom images with aDockerfile.