Is it Possible to run Docker in Docker and Launch Container of Jenkins?

Sumayya Khatoon
6 min readJun 24, 2021

--

Hello Folks! In this Blog I’ll guide you to launch Jenkins Container inside Docker in Docker Concept. So, Guys let’s get going…

Today the entire IT world is utilizing the power of Containers. Using containers developers can create predictable environments that are isolated from other apps. Regardless of where the app is deployed, everything remains consistent and this leads to massive productivity: less time debugging, and more time launching fresh features and functionality for users.

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. In simple words, It’s a tool which help us to launch Containers just in One Second.😉Docker is a containerization platform.

Isn’t it Amazing when you launch container just in One second?🤔Umm, Yes, It is…

How Docker works?

Docker actually uses a client-server architecture, kinda like this.

Architecture

The client refers to the docker binary itself, and is the interface that developers who use Docker are familiar with.

The server refers to the docker daemon, or dockerd, which is a background process that is responsible for executing the actions requested by the clients.

The client communicates with the server via a socket. A socket allows for 2 processes to communicate with each other, in this case the client and server. There are 3 types of sockets used in docker:

  • Unix Socket: Allows 2 processes on the same operating system to communicate with each other
  • TCP Socket: Allows 2 processes on same or different hosts to communicate with each other using the TCP protocol
  • FD Socket: Used on systems that support systemd for systemd socket activation.

In most cases, only Unix and TCP sockets are used.

Now the Question arises here , Can we run Docker inside Docker?🤔 The answer to this is “Yes, it is possible to launch Docker in Docker”.

So , let’s implement the Practical →

Is there any pre-requisites?

  • Yes, Docker should be installed in your host and the Services of docker should be Up and Running!

If you’re using RedHat Linux 8 then you can use this command to install Docker.

yum install docker-ce --nobest -ysystemctl enable docker --now

In this Blog Post I’ll show you two ways to achieve docker in docker

  • Running docker by mounting docker.sock (DooD Method)
  • Using Docker in Docker (dind) method.

Method 1: Running docker by mounting docker.sock

  • Step 1: Going inside the /var/run/docker.sock and checking the version of Docker Engine.

Use this command to check the version of Docker Engine.

curl --unix-socket /var/run/docker.sock http://localhost/version

What is /var/run/docker.sock?

/var/run/docker.sock is the default Unix socket. Sockets are meant for communication between processes on the same host. Docker daemon by default listens to docker.sock.

  • Step 2: Pulling the Official Docker image from DockerHub

It is the Docker Image which has Docker installed.

  docker pull docker 
  • Step 3: Launching Docker container and mounting the docker.sock as volume.

If you are on the same host where you docker daemon is running, you can use the /var/run/docker.sock to manage containers.

docker run -it -v /var/run/docker.sock:/var/run/docker.sock docker
Docker In Docker launched!!

Here , Docker installation inside the container was completely encapsulated from the host system.

  • Step 4: Launching Container inside Container.

Here I’m Going to launch Container of Jenkins. I’ve already created the Docker Image for launching the Jenkins Container.

Since, You’re already inside your docker in docker container run the below given command inside your container itself.

docker pull sumayyadocker/jenkins:v1

The Docker image has been Pulled Successfully. Using the Image we can launch Container inside Container.

Here is the command

docker run -it --name <con_name> --privileged -p 9999:8080 sumayyadocker/jenkins:v1
Here you’ll get the Password to login to your Jenkins Portal

Successfully Launched Container inside Container…

  • Step 5: Testing Time.

Grab the IP of your Host and exposed port number and browse it in your Browser.

Copy Paste the Admin Password

And here the Dashboard of Jenkins🙈

Hip Hip Hurray! We’ve launched Container inside Container…

Method 2: Launching Docker in Docker using dind method.

  • Step 1: Pulling the Official Docker in Docker image from Docker Hub
  docker pull docker:dind
  • Step 2: Launching the Container using Privileged Mode.

In “privileged” mode of containers allows you to run some containers with (almost) all the capabilities of their host machine, regarding kernel features and device access.


docker run -dit --privileged --name <name> docker:dind
  • Step 3: Log in to the container using exec command.
 docker exec -it <name_of_con> /bin/sh
Docker in Docker Launched!
  • Step 4: Launching Container in Container.

Like previously we can now launch Container inside the Docker in Docker setup.

For doing so you can pull the image of ubuntu from Docker Hub.

And after this , launching the container using the ubuntu image.

So simple, right?😅

Container inside Container launched !

Voila! Our Milestone is achieved!!

Hope you find my Blog Easy as well as Interesting!

Do like comment and give a clap!👏

That’s all! Signing off!

Thank you!

--

--

Sumayya Khatoon
Sumayya Khatoon

Written by Sumayya Khatoon

Machine Learning || Deep Learning || Kubernetes|| Docker || AWS || Jenkins || Ansible(RH294) || Python || Linux(RHEL8 )

No responses yet