0
0
Jenkinsdevops~30 mins

Docker socket mounting in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Docker Socket Mounting in Jenkins
📖 Scenario: You are setting up a Jenkins pipeline that needs to build Docker images. To do this, Jenkins must access the Docker daemon on the host machine. This is done by mounting the Docker socket inside the Jenkins container.This project will guide you step-by-step to configure the Docker socket mounting in a Jenkins pipeline script.
🎯 Goal: Build a Jenkins pipeline script that mounts the Docker socket /var/run/docker.sock from the host into the Jenkins container, allowing Docker commands to run inside the pipeline.
📋 What You'll Learn
Create a Jenkins pipeline script with a Docker agent
Mount the Docker socket /var/run/docker.sock inside the container
Use the mounted socket to run a Docker command inside the pipeline
Print the output of the Docker command
💡 Why This Matters
🌍 Real World
Mounting the Docker socket allows Jenkins pipelines to build, run, and manage Docker containers on the host machine. This is common in CI/CD pipelines for containerized applications.
💼 Career
Understanding Docker socket mounting is essential for DevOps engineers and Jenkins administrators who automate Docker workflows and container deployments.
Progress0 / 4 steps
1
Create Jenkins pipeline with Docker agent
Write a Jenkins pipeline script that uses a docker agent with the image docker:latest. Create a pipeline block with an agent section specifying docker { image 'docker:latest' }.
Jenkins
Need a hint?

Use the docker agent block inside agent to specify the Docker image.

2
Mount Docker socket inside the Docker agent
Add a args option inside the docker agent block to mount the Docker socket. Use args '-v /var/run/docker.sock:/var/run/docker.sock' to mount the socket from the host to the container.
Jenkins
Need a hint?

The args option lets you pass extra Docker run arguments like volume mounts.

3
Add a stage to run a Docker command
Inside the stages block, add a stage named 'Check Docker'. In the steps section of this stage, run the shell command docker ps to list running Docker containers.
Jenkins
Need a hint?

Use stage and steps blocks to organize pipeline tasks. Use sh to run shell commands.

4
Print the output of the Docker command
Modify the sh step to capture the output of docker ps into a variable called dockerOutput. Then add a echo step to print dockerOutput.
Jenkins
Need a hint?

Use sh with returnStdout: true to capture command output. Use echo to print it.