How to run Docker inside Docker Container and why?

How to run Docker inside Docker Container and why?

In this blog, I will walk you through how to install docker inside docker and its use case.

Docker is a software platform that simplifies the process of building, running, and managing applications.
It does this by virtualizing the operating system of the computer on which it is installed and running.

before going to how we should know why to install docker inside docker.

Use Case of Docker inside Docker

  • use case for docker in docker is for the CI pipeline, where you need to build and push docker images to a container registry after a successful code build.
  • Sandbox Environment
  • For experimental purposes on your local development workstation.

Let’s have a look at how to install it. Make sure you have docker installed in your host to try this setup.

Docker in Docker

  1. Creating storage

mkdir storage ``cd storage

Creating namespace for docker

2. Creating docker container in privileged mode

docker run -it --privileged -v /root/storage:var/lib/lib/docker --name myos centos

Running docker container in privileged mode

  • A privileged container means it has all the root capabilities of the host machine. We used privileged mode because due to security reasons docker-engine inside docker will fail.
  • Docker stores all data like containers, images, networks, plugins, and many mores inside in /var/lib/docker, so this folder is the most important folder. we create a folder that is storage inside in host machine and link this folder with that folder /var/lib/docker means we create persistent storage of that folder /var/lib/docker. We are using persistent storage so if we terminate any container then data inside the container will not get removed.

3. Creating the repo file inside in /etc/yum.repos.d

Vi/etc/yum.repos.d/docker.repo

this Will open the vi text editor

and write this command to the repo file

[docker] baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/ gpgcheck=0

4. Install Docker inside Docker

yum install -y docker-ce --nobest

5. Starting Docker engine

dockerd &

This command will start the docker in the background.

6. Launch a container in containerized docker

docker run -it --name container2 centos:latest

Running docker container inside container

This is how we can install Docker inside Docker and run the container in it.

one question might occur in your mind “Can we again install docker in it?”

yes, you can always nest one more level.

Did you find this article valuable?

Support Ritik Gupta Blog's by becoming a sponsor. Any amount is appreciated!