
Run Docker inside Containerized Jenkins in 3 easy steps
Recently I got started with Docker and within my first week of learning, I came across this very debatable topic of running docker within docker.
Now docker in docker has always been a topic of debate among the devOps community. So I decided to learn the dind concept.
Why I needed Docker in Docker?
Apart from learning Docker, I’m also playing with Jenkins and a web app project. Having some experience in Jenkins, I turned towards Jenkins to have a CI/CD deployment pipeline set for my this little project.
I’m using a dockerised Jenkins instance which checks out my project from git and runs the npm install command on it.
After git checkout and build step, I wanted my jenkins to make a docker image of this project and upload it on docker repository. At first it seemed easy to follow as every tutorial on internet showed it was easy to run docker within jenkins. What they didn’t show was, how to run docker commands within a Jenkins instance that’s already running on jenkins.
This is where the Docker in Docker concept comes into play. I needed my Dockerized jenkins to run Docker commands on runtime. This is what I did.
Running docker in docker:
1. Make custom docker image
- Make a new file in your favourite text editor and name it Dockerfile. I’m using vs code as it has whole lot of plugins to support different platforms and languages.
- Paste the following code into the Dockerfile.
FROM jenkins/jenkins:2.277.1-lts-jdk11USER rootRUN apt-get update\&& apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-commonRUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -RUN add-apt-repository \"deb [arch=amd64] https://download.docker.com/linux/debian \$(lsb_release -cs) \stable"RUN apt-get update\&& apt-get install docker-ce=17.12.1~ce-0~debian -yRUN usermod -aG docker jenkins
- The above script will download the Jenkins image from dockerhub and install Docker as well as run the updates on both the OS and Docker.
- Launch a terminal session and navigate to this directory where dockerfile is saved.
- Run the following command to make a docker image out of this dockerfile.
docker build -t jenkins .
- Here the command will make an image out of dockerfile named jenkins.
2. Run the image
- Now run the following command to run this newly built image into a docker container
docker container run -p 8080:8080 -p 50000:50000 -v (YOUR_LOCAL_JENKINS_DIR):/var/jenkins_home -v “$HOME”:/home -v /var/run/docker.sock:/var/run/docker.sock jenkins
3. Run docker command within jenkins
- The above command will run the jenkins on localhost:8080 and from there you can setup the jenkins, install necessary plugins.
- After jenkins has started, make a new freestyle project.

- Select that new project.
- Go into Build steps in the project configuration page.

- Select execute shell and type this in the white box.
docker run hello-world
- Now come back in the project main menu and select Build now from the left side navigation.

- If the build is successful, you will see the console output similar to this snapshot.

This should get you all the configuration needed to run docker commands within jenkins instance running in a container.
If there’s any error in your setup, feel free to let me know in the comments section and I can help out. Thanks everyone for reading!