📖 Scenario: You are setting up a Jenkins pipeline to build and test your application inside a Docker container. This helps keep your build environment clean and consistent, just like using a special kitchen for baking cakes so the recipe always turns out the same.
🎯 Goal: Create a Jenkinsfile that uses a Docker agent with the agent block. You will specify the Docker image, add a simple environment variable, run a shell command inside the container, and print the output.
📋 What You'll Learn
Create a pipeline block with a agent using Docker image python:3.12-slim
Add an environment variable MY_VAR with value hello
Inside the stages, create a stage named Run Python that runs python --version and echo $MY_VAR
Print the output of the commands in the console
💡 Why This Matters
🌍 Real World
Using Docker agents in Jenkins pipelines helps developers run builds and tests in clean, consistent environments without installing tools on the Jenkins server itself.
💼 Career
Many DevOps and CI/CD engineer roles require writing Jenkins pipelines that use Docker containers to ensure reliable and repeatable builds.
Progress0 / 4 steps
1
Create the Jenkins pipeline with Docker agent
Write a Jenkinsfile starting with pipeline block that uses agent with Docker image python:3.12-slim.
Jenkins
Hint
Use agent { docker { image 'python:3.12-slim' } } inside the pipeline block.
2
Add environment variable inside the pipeline
Add an environment block inside the pipeline with variable MY_VAR set to hello.
Jenkins
Hint
Use environment { MY_VAR = 'hello' } inside the pipeline block.
3
Add a stage to run commands inside Docker container
Add a stages block with a stage named Run Python. Inside it, add a steps block that runs shell commands python --version and echo $MY_VAR using sh.
Jenkins
Hint
Use stages { stage('Run Python') { steps { sh 'python --version'; sh 'echo $MY_VAR' } } }.
4
Print the output of the commands
Run the Jenkins pipeline. The console output should show the Python version and the text hello printed from the environment variable.
Jenkins
Hint
Look at the Jenkins console output after running the pipeline. It should show the Python version and the word hello.
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.