Launching a container inside a container

Harshal Kondhalkar
4 min readDec 7, 2021

--

Have you ever tried to run a docker container inside docker?

Here in this blog, I am going to explain how to run a docker inside docker. Following are the steps to do so-

Initially pull any docker image from the docker hub. Example- centos

Use the command:

docker pull centos:latest

Create a workspace/directory on your base OS

Then launch a container with the centos image on docker

Use the following command command:

docker run -it - privileged -v /root/new_storage/:/var/lib/docker - name os101 centos
  1. We used privileged mode or else to run docker engine inside docker container it will fail due to some security reason. A privileged container means it has all the root capabilities of the host
  2. Docker stores all data like containers, images, networks, plugins and many mores inside in one place that is /var/lib/docker. Also we know if we terminate any container than all the content inside the container will be removed. So, for that reason we have to mount a directory from the base OS to the container created. So I have created a folder with name new_storage inside the host machine and linked this folder with that folder /var/lib/docker. Thus created a persistent storage of the /var/lib/docker folder

Now we install docker on centos environment for that first we create a repo file inside in /etc/yum.repos.d

Write the following in the vi editor-

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

Now install docker using yum command-

We can install any software on docker with yum command as it by default configured in docker software-

To check if yum is configured use command-

yum repolist
yum install docker-ce --nobest -y

Run docker engine as follows-

dockerd &

dockerd” is a long running program that never terminated so we used & to run in background

Now docker is running inside your containerized docker, so the docker installed on top docker will act the same. Now we run a container on this docker environment. Lets Now docker is running inside your docker container and we can do everything whatever we do in docker. Now we run a container on this docker environment. Lets download a small size image e.g. alpine image to launch a new container on top this docker environment.

docker pull alpine

Command used to see if image is download-

docker images

Launch a new container and then terminate it.

docker run -it --name os1 alpine

Then again start it with “docker start” command to check if the container is actually launched.

docker start os1

Then use the following command to check if container is launched-

docker ps

Thus alpine linux is running on top of centos with the help of docker and centos is running on the top of base system OS also with the help of docker.

Thank you!

--

--