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-slimAdd an environment variable
MY_VAR with value helloInside the
stages, create a stage named Run Python that runs python --version and echo $MY_VARPrint 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