📖 Scenario: You are setting up a Jenkins pipeline to run build steps inside isolated Docker containers. This helps keep your builds clean and consistent, like having a fresh workspace every time.
🎯 Goal: Create a Jenkins pipeline script that uses a Docker agent to run a simple shell command inside a container. This will show how Docker agents isolate the build environment.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block
Use a agent directive with docker to specify the container image
Add a stage named Run inside Docker
Inside the stage, run a shell command echo "Hello from Docker"
Print the output of the command
💡 Why This Matters
🌍 Real World
Many teams use Jenkins with Docker agents to ensure builds run in clean, repeatable environments without leftover files or dependencies.
💼 Career
Understanding Docker agents in Jenkins is essential for DevOps roles to create reliable CI/CD pipelines that work the same everywhere.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with pipeline {} and inside it add an empty agent directive and a stages block.
Jenkins
Hint
Start with pipeline {}, add agent any to run on any available node, and add stages {} for build steps.
2
Configure the Docker agent
Change the agent directive to use a Docker container with image alpine:latest. Use agent { docker { image 'alpine:latest' } }.
Jenkins
Hint
Use the agent block with docker and specify the image as 'alpine:latest'.
3
Add a stage to run a shell command inside the Docker container
Inside stages, add a stage named Run inside Docker. Inside it, add a steps block with a shell command echo "Hello from Docker" using sh.
Jenkins
Hint
Use stage('Run inside Docker') with steps { sh 'echo "Hello from Docker"' } to run the command inside the container.
4
Print the output of the Docker container command
Add a script block inside steps to capture the output of echo "Hello from Docker" using sh(script: ..., returnStdout: true). Then print the output with echo.
Jenkins
Hint
Use def output = sh(script: ..., returnStdout: true).trim() to get the command output, then print it with echo output.
Practice
(1/5)
1. What is the main benefit of using Docker agents in Jenkins for running jobs?
easy
A. They provide isolated environments to avoid conflicts between builds.
B. They speed up the Jenkins UI loading time.
C. They allow Jenkins to run without any plugins installed.
D. They automatically backup Jenkins configuration files.
Solution
Step 1: Understand Docker agent purpose
Docker agents run Jenkins jobs inside containers, isolating each build environment.
Step 2: Identify the benefit of isolation
This isolation prevents conflicts and keeps builds clean and reproducible.
Final Answer:
They provide isolated environments to avoid conflicts between builds. -> Option A
Quick Check:
Docker agents = isolated environments [OK]
Hint: Docker agents isolate builds to avoid conflicts [OK]
Common Mistakes:
Thinking Docker agents speed up Jenkins UI
Believing Docker agents remove need for plugins
Assuming Docker agents backup Jenkins configs
2. Which of the following is the correct syntax to specify a Docker agent with image python:3.12 in a Jenkins pipeline?
But the build fails with docker: command not found. What is the likely cause?
medium
A. The Jenkins agent cannot mount volumes.
B. The ubuntu:latest image does not have Docker installed.
C. The args option is incorrect syntax.
D. The docker ps command is invalid inside containers.
Solution
Step 1: Check the Docker image capabilities
The ubuntu:latest image is a minimal OS image and does not include Docker by default.
Step 2: Understand why command fails
Since Docker is not installed inside the container, docker ps command is not found, causing the error.
Final Answer:
The ubuntu:latest image does not have Docker installed. -> Option B
Quick Check:
Missing Docker in image = command not found error [OK]
Hint: Check if Docker is installed inside the container image [OK]
Common Mistakes:
Blaming volume mount syntax
Assuming docker commands always work in containers
Thinking docker ps is invalid inside containers
5. You want to run a Jenkins job inside a Docker container with a custom environment variable MY_VAR=hello and mount the workspace directory to /app inside the container. Which Jenkins pipeline snippet correctly sets this up?