While working with Jenkins running inside a Docker container, I ran into a common challenge:
👉 How do you run Docker containers from inside another Docker container?
This scenario often appears when Jenkins pipelines need to build or run Docker images, or when you want to run tools like Containerlab inside a Jenkins container for network lab automation.
After some experimentation, I found that the simplest and most practical approach is to map the Docker socket and Docker executable from the host into the Jenkins container.
This allows the Jenkins container to communicate directly with the host Docker daemon.
Here is the command I used to start Jenkins:
docker run -d -p 8080:8080 -p 50000:50000 \
-v jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(which docker):/usr/bin/docker
Flags Explained
-p
Maps a host port to a port inside the container.
Jenkins uses 8080 (web UI) and 50000 (agent communication).
-v
Maps a host location to a location inside the container.
In this case we map:
/var/run/docker.sock → allows Jenkins to talk to the host Docker daemon
/usr/bin/docker → provides the Docker CLI inside the container
Important: Docker Socket Permissions
In some environments, the Jenkins user inside the container may not have permission to access the Docker socket.
To fix this:
1️⃣ Login to the container as root
docker exec -u 0 jenkins_container_name bash
2️⃣ Update the Docker socket permissions
chmod 666 /var/run/docker.sock
3️⃣ Exit the root shell and continue using the normal Jenkins user if needed.
Result
With this setup, Jenkins can build, run, and manage Docker containers on the host without running Docker-in-Docker.
⚡ Simple, efficient, and widely used in CI/CD pipelines.
Common Errors When Running Docker from Jenkins Containers
Many engineers run into permission errors when Jenkins tries to access the Docker daemon.
A common error looks like this:
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
This usually happens because the Jenkins user inside the container does not have permission to access the Docker socket.
One quick workaround is updating the socket permissions:
chmod 666 /var/run/docker.sock
Alternatively, you can add the Jenkins user to the Docker group on the host.