DOCKER INSIDE DOCKER
Tags: docker, linux, devops, beginners
Published: 2021-07-04 15:50:53

Sometimes we want Docker inside Docker. For Example :the CI pipeline,   Jenkins Docker-based dynamic agents for your CI/CD pipelines, Sandboxed environments or experimental workstations.

Here are three methods. (1) and (2) are need to be secured as they have more privileges over your docker daemon. You can use firewall in docker or make it rootless.

Following articles can help you :

{%link https://dev.to/manishfoodtechs/adding-firewall-to-docker-4hjd %}

{%link https://dev.to/manishfoodtechs/why-hackers-first-love-a-docker-container-hacking-docker-l97 %}

and 

{%link https://dev.to/manishfoodtechs/rootless-docker-kick-docker-hackers-and-make-docker-more-secure-new-concept-70g %}

In 3 method, we will use sysbox.

##Method 1: Docker in Docker Using [/var/run/docker.sock]
If you are on the same host where Docker daemon is running, you can use the /var/run/docker.sock to manage containers.

To run docker inside docker, all you have to do it just run docker with the default Unix socket docker.sock as a volume.

```
docker run -v /var/run/docker.sock:/var/run/docker.sock \
           -ti docker
```
Now, from within the container, you should be able to execute docker commands for building and pushing images to the registry.

Start Docker container in interactive mode mounting the docker.sock as volume. We will use the official docker image.

```
docker run -v /var/run/docker.sock:/var/run/docker.sock -ti docker

#Inside Container

docker pull ubuntu

#When you list the docker images, you should see the ubuntu image along with other docker images in your host VM

docker images

# Now create a Dockerfile inside test directory
and RUN DOCKER FILE
docker build -t test-image .
```
##Method 2: Docker in Docker Using dind
Note: This requires your container to be run in privileged mode.

```
docker run --privileged -d --name dind-test docker:dind
docker exec -it dind-test /bin/sh
docker pull ubuntu
docker images
mkdir test && cd test
vi Dockerfile
#Create Docker file and then run
docker build -t test-image
```
#Method 3: Docker in Docker Using Sysbox Runtime
Method 1 & 2 has some disadvantages in terms of security because of running the base containers in privileged mode. Nestybox tries to solve that problem by having a sysbox Docker runtime. However , with firewall implementation you can get a security layer. Check my above article.

(1) Get Sysbox CE
Community edition, based on the open-source Sysbox.
{% github nestybox/sysbox %}

At the time of writing article [supported distros](https://github.com/nestybox/sysbox/blob/master/docs/distro-compat.md) are:
Ubuntu Bionic (18.04),Ubuntu Focal (20.04),Debian Buster (10),Debian Bullseye (11),Fedora 31,Fedora 32,CentOS 8

Installing :
```
git clone --recursive git@github.com:nestybox/sysbox.git
make sysbox
sudo make install
#Once Sysbox is installed, you start it with:
sudo ./scr/sysbox
#Configuring Docker
sudo ./scr/docker-cfg --sysbox-runtime=enable

```
(2)USE SYSBOX

```
docker run --runtime=sysbox-runc --name sysbox-dind -d docker:dind
docker exec -it sysbox-dind /bin/sh
```

###CONGRATULATIONS :)

Here is a [nice article](https://devopscube.com/run-docker-in-docker/)

Further :
I was one of the attendee in this meeting . You can find co founder speaking about sysbox https://bluejeans.com/s/Qq_IsjrnOaG 20.05 Minutes

##[Click here for joining my team](https://forms.gle/pSCALuEWHus3pg9F6)