How to Run Docker Inside Docker?

Lalita Rajpoot
3 min readJan 5, 2022

Hello Everyone, In this blog I’m going to explain you “How to run docker inside docker “ in very easy and efficient method…

What is a container?

A container collects code and all its dependencies to ensure a smooth and quick transition of applications from one computing environment to another. A container image contains everything that is required to run any application (i.e., the code, system tools, system libraries, and settings).

There are many methods achieve docker inside docker. Here, I’m run docker inside run by mounting docker.sock method.

About /var/run/docker.sock

Docker.sock is a Unix socket that enables the Docker server-side daemon to communicate with its command-line interface. Sockets are meant for communication between processes on the same host.If you are on the same host where Docker daemon is running, you can use the /var/run/docker.sock to manage containers.

The socket appears as the /var/run/docker.sock file. Because it is a file, admins can share and run docker.sock within a container and then use it to communicate with that container. A container that runs docker.sock can start or stop other containers, create images on the host or write to the host file system.

With this approach, a container, with Docker installed, does not run its own Docker daemon but connects to the Docker daemon of the host system. That means, you will have a Docker CLI in the container, as well as on the host system, but they both connect to one and the same Docker daemon. At any time, there is only one Docker daemon running in your machine, the one running on the host system.
To achieve this, you can start a Docker container, that has Docker installed.

So, Let’s move on to our main point

First we’ll start docker and check its status in our VM using given below command:

systemctl start docker
system status docker

And, now we will run docker with default Unix socket docker.sock as volume.

Use following command :-

docker run -it -v /var/run/docker.sock:/var/run/docker.sock docker

Now , Here we ran docker with the default Unix socket docker.sock and then we are in Docker container that we just started.

Let’s try to run some Docker commands(e.g- docker pull centos:latest)

docker pull centos:latest
successfully image pulled

Successfully this command also run ,Now move forward..

We’ll launch another container inside already launched container using following command:-

docker run -it --name d1 ubuntu:latest
Successfully launched docker inside docker

So, we have done our task … It was so easy. I hope this is very clear to you all

Thanks for your time …. Stay Happy & Healthy :)

--

--