Hello Learner, I am back with another blog related to the docker container. In this blog, we will learn How to run the docker in a docker container. So we will use two different ways to do this task.
Docker
It is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production. You can follow this link to know more about the docker. You can install docker with this link.
Run Docker inside the Docker container:
To run the docker inside the docker container, We have two options:
- Mounting docker. sock
- Dind method
1. Run docker in docker using the mounting method:
This method totally depends on the /var/run/docker.sock. This is the default socket. The socket is meant for communication between processes. Always docker daemon listens to the docker.sock. If you are on the same machine where you run the docker daemon is running. Then you can manage the container using /var/run/docker.sock
Now you have a basic understanding of docker.sock and it is very easy to run docker inside docker. You just need to run the below command. In this command, we have defined the default docker.sock as the volume. You can run the below command as an example.
For example,
docker run -it -v /var/run/docker.sock:/var/run/docker.sock docker
Now it’s time to run the docker in docker. I have divided this process into steps. You can just follow the below steps.
Step1: We will use the official docker image with mounting the docker.sock as volume
docker run -v /var/run/docker.sock:/var/run/docker.sock -ti docker
Step2: Once you run the above command and exec into the container then run the below command.
docker pull ubuntu
Step3: list all images using the below command.
docker images

Step4: Now create a folder and inside the folder create a docker file.
mkdir demo && cd demo
vim Dockerfile
Now paste the below container inside the docker file
FROM ubuntu:latest
LABEL maintainer=” MuZakkir Saifi”
RUN apt-get update && \
apt-get -qy full-upgrade && \
apt-get install -qy curl && \
apt-get install -qy curl && \
curl -sSL https://get.docker.com/ | sh
Step5: Now its time to build the docker file with the below command:
docker build -t demo:0.1 .



2. Dind Method
This method is quite different from the above one. In this method, It creates a child container inside the docker container. We can use this method When only if you really want the containers and images inside the container. Otherwise, We should use the above approach.
We will use the docker official image with the dind tag. This tag dind image comes with the all required utilities for docker to run the docker inside.
Note: We just need to keep one thing in mind it requires our container to be run in privileged mode.
Step 1: Run the below command to run the container with the dind tag image.
docker run --privileged -d --name <demo> docker:dind
Step 2: Now login to the container.
docker exec -it <demo> /bin/sh
Step3: Once you run the above command and exec into the container then run the below command.
docker pull ubuntu
Step4: list all images using the below command.
docker images



Step5: Now create a folder and inside the folder create a docker file.
mkdir demo && cd demo
vim Dockerfile
Now paste the below container inside the docker file
FROM ubuntu:latest
LABEL maintainer=”MuZakkir Saifi”
RUN apt-get update && \
apt-get -qy full-upgrade && \
apt-get install -qy curl && \
apt-get install -qy curl && \
curl -sSL https://get.docker.com/ | sh
Step6: Now its time to build the docker file with the below command:
docker build -t demo:0.1 .






Conclusion:
In this blog, We learned how we can run the docker in docker using the child method and volume sharing. You can subscribe to the knoldus to learn more. We have launched our docker course. You can enroll in and learn the docker. Thank you.
Happy Learning !!