0
0
Jenkinsdevops~10 mins

Docker agent in Jenkinsfile - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Docker agent in Jenkinsfile
Start Jenkins Pipeline
Define Docker Agent
Jenkins pulls Docker image
Run build steps inside Docker container
Build steps execute commands
Container stops after steps
Pipeline ends
The Jenkins pipeline starts, defines a Docker agent, pulls the Docker image, runs build steps inside the container, then stops the container and ends.
Execution Sample
Jenkins
pipeline {
  agent {
    docker {
      image 'python:3.12'
    }
  }
  stages {
    stage('Test') {
      steps {
        sh 'python --version'
      }
    }
  }
}
This Jenkinsfile runs the 'Test' stage inside a Python 3.12 Docker container and prints the Python version.
Process Table
StepActionEvaluationResult
1Start pipelineN/APipeline begins execution
2Define agent as Docker with image 'python:3.12'N/AJenkins prepares to use Docker agent
3Pull Docker image 'python:3.12'Image not present locallyImage downloaded from Docker Hub
4Start container from imageContainer createdContainer running
5Execute step: sh 'python --version'Run command inside containerOutputs 'Python 3.12.x'
6Finish stage 'Test'All steps doneStage completed
7Stop Docker containerContainer stoppedContainer removed
8Pipeline endsAll stages completePipeline finished successfully
💡 Pipeline ends after all stages complete and Docker container stops
Status Tracker
VariableStartAfter Step 3After Step 4After Step 7Final
Docker Imageundefined'python:3.12' pulledImage readyContainer stoppedN/A
Container Statenonenonerunningstoppedremoved
Pipeline Statenot startedstartedrunningrunningfinished
Key Moments - 3 Insights
Why does Jenkins pull the Docker image even if it might exist locally?
Jenkins checks if the image exists locally; if not, it pulls it. This is shown in step 3 where the image is downloaded only if missing.
What happens to the Docker container after the build steps finish?
As shown in step 7, the container stops and is removed to clean up resources after the pipeline stage completes.
How does Jenkins run commands inside the Docker container?
In step 5, Jenkins executes shell commands inside the running container, isolating the build environment.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Jenkins start the Docker container?
AStep 4
BStep 3
CStep 5
DStep 7
💡 Hint
Check the 'Action' column for 'Start container from image' in the execution table.
According to the variable tracker, what is the container state after step 7?
Aremoved
Brunning
Cstopped
Dnone
💡 Hint
Look at the 'Container State' row under 'After Step 7' in the variable tracker.
If the Docker image is already present locally, which step in the execution table would change?
AStep 4 would be skipped
BStep 3 would be skipped or faster
CStep 5 would not run
DStep 7 would be earlier
💡 Hint
Step 3 is about pulling the image; if present locally, pulling is skipped or very fast.
Concept Snapshot
Jenkins Docker agent syntax:
agent { docker { image 'image_name' } }

Jenkins pulls the image if missing,
runs build steps inside the container,
then stops and removes the container.
Use for isolated, consistent build environments.
Full Transcript
This visual execution shows how Jenkins uses a Docker agent in a pipeline. The pipeline starts, defines a Docker agent with a specific image, and Jenkins pulls the image if it is not already present locally. Then Jenkins starts a container from that image and runs the build steps inside it. After the steps complete, the container stops and is removed. The pipeline then finishes successfully. Variables like Docker image, container state, and pipeline state change step-by-step. Key moments clarify why Jenkins pulls images, how commands run inside containers, and container cleanup. Quiz questions test understanding of container start, container state after stopping, and image pulling behavior.