A Docker agent in a Jenkinsfile is a way to run Jenkins pipeline steps inside a Docker container. It helps create a consistent environment for builds.
Click to reveal answer
beginner
How do you specify a Docker agent in a Jenkins declarative pipeline?
Use the agent { docker { image 'image-name' } } syntax inside the Jenkinsfile to run steps inside the specified Docker image.
Click to reveal answer
beginner
Why use a Docker agent in Jenkins pipelines?
Using a Docker agent ensures the build runs in a clean, isolated environment with all required tools, avoiding conflicts and making builds reproducible.
Click to reveal answer
intermediate
What happens if the Docker image specified in the Jenkinsfile is not available on the Jenkins host?
Jenkins will try to pull the Docker image from the configured Docker registry before running the build inside the container.
Click to reveal answer
beginner
Show a simple Jenkinsfile snippet using a Docker agent with the image 'python:3.12'.
What does the 'agent { docker { image "node:18" } }' block do in a Jenkinsfile?
APushes the 'node:18' image to Docker Hub.
BBuilds a Docker image named 'node:18'.
CRuns pipeline steps inside a Docker container using the 'node:18' image.
DStarts a Docker daemon on the Jenkins server.
✗ Incorrect
The agent block with docker image runs the pipeline steps inside a container created from the specified image.
If the Docker image is not found locally, what does Jenkins do?
APulls the image from the Docker registry.
BFails the build immediately.
CBuilds the image from a Dockerfile automatically.
DSkips the Docker agent and runs on the Jenkins host.
✗ Incorrect
Jenkins pulls the image from the Docker registry if it is not available locally.
Which Jenkinsfile syntax is correct to use a Docker agent?
Aagent { docker { image 'ubuntu:20.04' } }
Bagent { shell 'docker run ubuntu' }
Cagent { label 'docker' }
Dagent { dockerfile 'Dockerfile' }
✗ Incorrect
The correct syntax to specify a Docker image as an agent is 'agent { docker { image 'image-name' } }'.
What is a key benefit of using Docker agents in Jenkins pipelines?
AUnlimited build concurrency.
BFaster network speed.
CAutomatic code deployment.
DConsistent and isolated build environments.
✗ Incorrect
Docker agents provide consistent and isolated environments for builds, reducing errors caused by environment differences.
Can you run multiple stages inside the same Docker agent container in a Jenkins pipeline?
AOnly if you use scripted pipeline syntax.
BYes, all stages share the same Docker container if agent is defined at pipeline level.
CNo, each stage creates a new container.
DOnly if you disable Docker caching.
✗ Incorrect
When the Docker agent is defined at the pipeline level, all stages run inside the same container.
Explain how to use a Docker agent in a Jenkinsfile and why it is useful.
Think about how Docker containers help keep builds clean and repeatable.
You got /3 concepts.
Describe what happens when Jenkins runs a pipeline with a Docker agent but the image is not present locally.
Consider how Docker handles missing images on a host.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using a docker agent in a Jenkinsfile?
easy
A. To speed up the Jenkins UI loading time
B. To run the build inside a container for a consistent environment
C. To deploy the application automatically after build
D. To backup Jenkins configuration files
Solution
Step 1: Understand what a Docker agent does in Jenkins
A Docker agent runs the build steps inside a Docker container, ensuring the environment is consistent and isolated.
Step 2: Compare options with this purpose
Only To run the build inside a container for a consistent environment describes running builds inside containers for consistency. Other options describe unrelated tasks.
Final Answer:
To run the build inside a container for a consistent environment -> Option B
But the build fails with error: docker: Error response from daemon: the input device is not a TTY. What is the best fix?
medium
A. Change image to ubuntu:latest
B. Add tty true to the docker agent block
C. Remove the -it argument from args
D. Replace sh with bat
Solution
Step 1: Understand the error message
The error says the input device is not a TTY, caused by using -it (interactive + tty) in a non-interactive Jenkins build.
Step 2: Fix the Docker args
Removing -it from args avoids requesting a TTY, fixing the error.
Final Answer:
Remove the -it argument from args -> Option C
Quick Check:
Remove interactive TTY args in Jenkins Docker agent [OK]
Hint: Avoid -it args in Jenkins Docker agents [OK]
Common Mistakes:
Adding tty true without removing -it
Changing image without fixing args
Using Windows bat step on Linux agent
5. You want to run a Jenkins pipeline using a Docker agent with the image maven:3.8.6-jdk-17 and mount the host directory /var/maven-repo to /root/.m2/repository inside the container. Which Jenkinsfile snippet correctly sets this up?
The syntax to mount a host directory to a container path is -v host_path:container_path.
Step 2: Match the mount paths correctly
The host directory /var/maven-repo should be mounted to /root/.m2/repository inside the container, so the args must be -v /var/maven-repo:/root/.m2/repository.