0
0
Jenkinsdevops~10 mins

Docker socket mounting in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Docker socket mounting
Start Jenkins Container
Mount /var/run/docker.sock
Jenkins uses Docker CLI
Docker commands sent to host Docker daemon
Host Docker daemon executes commands
Containers created/managed on host
Jenkins monitors container status
Jenkins container mounts the Docker socket file from the host, allowing Jenkins to control Docker on the host directly.
Execution Sample
Jenkins
docker run -d \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts
This command runs Jenkins container mounting the Docker socket so Jenkins can run Docker commands on the host.
Process Table
StepActionMount StatusDocker Command SourceDocker Daemon ResponseResult
1Start Jenkins containerSocket not mounted yetN/AN/AJenkins container initializing
2Mount /var/run/docker.sockSocket mounted inside containerJenkins containerHost Docker daemon readyJenkins can send Docker commands
3Jenkins runs 'docker ps'Socket mountedJenkins containerHost Docker daemon lists containersOutput: list of running containers
4Jenkins runs 'docker run alpine'Socket mountedJenkins containerHost Docker daemon creates alpine containerNew container started on host
5Jenkins monitors container statusSocket mountedJenkins containerHost Docker daemon reports statusJenkins sees container running
6Stop Jenkins containerSocket unmountedN/AN/AJenkins container stops, no Docker control
💡 Jenkins container stops or socket unmounted, Docker commands no longer forwarded
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Socket Mountedfalsetruetruetruetruefalse
Docker Command SourceN/AJenkins containerJenkins containerJenkins containerJenkins containerN/A
Containers on Hostexisting containersexisting containersexisting containersexisting + alpineexisting + alpineexisting + alpine
Key Moments - 3 Insights
Why does Jenkins need the Docker socket mounted?
Mounting the Docker socket allows Jenkins inside the container to send commands directly to the host's Docker daemon, as shown in execution_table step 2 and 3.
What happens if the socket is not mounted?
Without the socket mounted, Jenkins cannot communicate with the host Docker daemon, so Docker commands fail or do nothing, as seen in step 1 where socket is not mounted.
Does mounting the socket give Jenkins full control over host Docker?
Yes, mounting the socket gives Jenkins the same Docker control as the host user, so it can create, stop, and manage containers, as shown in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Jenkins first successfully run a Docker command?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Check the 'Docker Command Source' and 'Docker Daemon Response' columns in execution_table rows.
According to variable_tracker, what is the state of 'Socket Mounted' after step 5?
AN/A
Bfalse
Ctrue
Dunknown
💡 Hint
Look at the 'Socket Mounted' row and the column 'After Step 5' in variable_tracker.
If the socket was not mounted, how would the 'Docker Command Source' column in execution_table change?
AIt would show 'Host Docker daemon' instead of 'Jenkins container'
BIt would remain 'Jenkins container' but commands would fail
CIt would be 'N/A' for all steps
DIt would show 'Jenkins container' and commands succeed
💡 Hint
Consider what happens when Jenkins tries to run Docker commands without socket mounting, referencing step 1 and 2.
Concept Snapshot
Docker socket mounting lets a container (like Jenkins) access the host's Docker daemon.
Mount /var/run/docker.sock from host to container.
This enables running Docker commands inside the container that affect host containers.
Without mounting, Docker commands inside container fail.
Use: docker run -v /var/run/docker.sock:/var/run/docker.sock ...
Full Transcript
Docker socket mounting means sharing the Docker daemon's communication file from the host into a container. When Jenkins runs inside a container, it cannot control Docker on the host unless it has access to the Docker socket file. By mounting /var/run/docker.sock from the host into the Jenkins container, Jenkins can send Docker commands directly to the host's Docker daemon. This allows Jenkins to create, list, and manage containers on the host as if it was running Docker commands locally. The execution steps show starting Jenkins without the socket (no Docker control), then mounting the socket, running Docker commands successfully, and finally stopping Jenkins which removes Docker control. Variables track the socket mount status and containers on the host. Key moments clarify why mounting is needed and the security implications. The quiz tests understanding of when Docker commands succeed and the effect of mounting the socket.