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.
Create 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.
Click build now
  • 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!

--

--

--

DevOps and Cloud Advocate

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Life is a journey of twists and turns, peaks and valleys, mountains to climb and oceans to explore.

Sudesh Recommends SpringOne 2021 “Session Picks”

To Be, or Not To Be: A Developer Advocate

Lessons from first Retro Scrum

Configure Consul for performance at scale

[Guest Post] Behind the Extension — Control the Show

The Consent Amendment Process of Australian CDR Specification.

Why I decided to pursue Software Engineering

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shikhar Shah

Shikhar Shah

DevOps and Cloud Advocate

More from Medium

Pets vs Cattle part 2

Migrate containers to GitHub Container Registry (GHCR) — GitHub Packages — Part 1: Bash script

Part 3: Using Object Storage S3 Compatible API – Oracle Cloud

SonarQube For Beginners — Part 1/2