Docker-in-Docker considerations in Jenkins - Commands & Configuration
pipeline {
agent {
docker {
image 'docker:24.0.5'
args '--privileged -v /var/run/docker.sock:/var/run/docker.sock'
}
}
stages {
stage('Build Docker Image') {
steps {
sh 'docker build -t my-app-image .'
}
}
}
}
This Jenkinsfile defines a pipeline that runs inside a Docker container using the official Docker image version 24.0.5.
The --privileged flag allows the container to run Docker daemon commands.
The volume mount -v /var/run/docker.sock:/var/run/docker.sock shares the host's Docker socket, enabling Docker commands inside the container to communicate with the host Docker daemon.
This setup avoids running a full Docker daemon inside the container, which is complex and less efficient.
docker run --privileged -d -v /var/run/docker.sock:/var/run/docker.sock --name jenkins-agent docker:24.0.5 sleep infinity
--privileged - Allows the container to run Docker commands that require extended privileges.-v /var/run/docker.sock:/var/run/docker.sock - Shares the host Docker socket with the container to use the host Docker daemon.-d - Runs the container in detached mode.docker exec jenkins-agent docker ps
docker exec jenkins-agent docker build -t my-app-image .
docker exec jenkins-agent docker run --rm my-app-image
--rm - Automatically removes the container after it exits.If you remember nothing else from Docker-in-Docker, remember: mounting the host Docker socket inside a privileged container lets you run Docker commands inside containers without running a full Docker daemon inside them.
Practice
Solution
Step 1: Understand Docker-in-Docker concept
Docker-in-Docker allows running Docker commands inside a container, enabling containerized builds.Step 2: Relate to Jenkins usage
In Jenkins pipelines, DinD is used to run Docker commands inside the Jenkins container for building images or running containers.Final Answer:
To run Docker commands inside a Jenkins container -> Option DQuick Check:
Docker-in-Docker = run Docker inside container [OK]
- Confusing DinD with running Jenkins inside Docker
- Thinking DinD replaces Jenkins
- Assuming DinD avoids Docker on host
Solution
Step 1: Identify Docker-in-Docker requirements
DinD requires elevated privileges to run Docker daemon inside a container.Step 2: Match with Docker run options
The--privilegedflag grants the container extended privileges needed for DinD.Final Answer:
--privileged -> Option CQuick Check:
DinD needs --privileged flag [OK]
- Using only volume mount without privileges
- Confusing --network host with privileges
- Assuming --rm affects DinD
pipeline {
agent {
docker {
image 'docker:dind'
args '--privileged'
}
}
stages {
stage('Build') {
steps {
sh 'docker info'
}
}
}
}
What will the docker info command output indicate?Solution
Step 1: Analyze pipeline agent configuration
The agent usesdocker:dindimage with--privileged, enabling Docker daemon inside the container.Step 2: Understand
This command queries the Docker daemon status; with DinD properly set, it shows daemon info.docker infobehaviorFinal Answer:
Docker daemon is running inside the container -> Option AQuick Check:
docker:dind + --privileged = Docker daemon running [OK]
- Expecting 'command not found' without checking image
- Assuming daemon is not running without privileges
- Confusing permission errors with missing daemon
Cannot connect to the Docker daemon. What is the most likely cause?Solution
Step 1: Identify error cause
The error means Jenkins container cannot access Docker daemon inside itself.Step 2: Check Docker-in-Docker setup
Without--privileged, Docker daemon inside container won't start or be accessible.Final Answer:
Missing --privileged flag in Docker run command -> Option AQuick Check:
Cannot connect daemon = missing --privileged [OK]
- Blaming Jenkinsfile syntax for daemon connection
- Assuming host Docker installation affects DinD
- Ignoring container privilege requirements
--privileged. Which alternative approach is best?Solution
Step 1: Understand security concerns with --privileged
The--privilegedflag grants broad access, which can be risky in shared environments.Step 2: Identify safer alternative
Mounting the host Docker socket allows Jenkins to use host Docker daemon without needing privileged container.Final Answer:
Mount the host Docker socket/var/run/docker.sockinto the Jenkins container -> Option BQuick Check:
Socket mount avoids --privileged security risk [OK]
- Using docker:dind without privileges expecting it to work
- Thinking disabling Docker is a solution
- Assuming running Jenkins on host avoids all risks
Ready to prove it?
Pass the test and this pattern moves unknown
