0
0
Jenkinsdevops~30 mins

Docker agent in Jenkinsfile - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Docker Agent in Jenkinsfile
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Look at the Jenkins console output after running the pipeline. It should show the Python version and the word hello.