Docker inside another Docker (dind)
Docker内のDocker — Let’s have some fun with nested docker
Unless you are not being on Earth for last couples of years, You most probably have come across buzz words like Docker and Container and Kubernetes (k8s).
If you need help — Read Docker Basics and SQL 2017 in Docker or Docker Official site
Today we are planning to have fun with Docker by running Docker inside Docker.
Word of caution
This is for fun only, there are many security-related confusion which can occur in “Docker-in-Docker” mode. The primary purpose of “Docker-in-Docker” was to help with the development of Docker itself.
The second issue is linked to storage drivers. When you run Docker in Docker, the outer Docker runs on top of a normal filesystem (EXT4, BTRFS, what have you) but the inner Docker runs on top of a copy-on-write system (AUFS, BTRFS, Device Mapper, etc., depending on what the outer Docker is set up to use).
what about the build cache? That one can get pretty tricky too. questions like below have hunted me “Running Docker-in-Docker; how can I use the images located on my host, rather than pulling everything again in my inner Docker?”
If you are using “Docker-in-Docker” in production then let me know in comments !!
Let’s start our Docker-in-Docker journey
- Make sure you have installed latest docker version and docker daemon is running. Confirm using following commands.
2. Get “Nginx:latest” image from Docker Hub
3. Now run a container from downloaded nginx:latest image.
Do not forget to add the “privileged” flag. As we are planning to run Docker inside docker, Our “docker1” container must have “privileged” key. This is magic.
4. Now, let’s bash into our docker1.
5. Now, we need to install docker inside “docker1” container. As nginx:latest image has “Debian 9 (stretch)” as base image follow the below command.
6. Now, let's pull image called “nginx:alpine” and run our second docker container from the pulled image.
7. Let’s sh into our docker2 and edit “index.html”
8. Now, let’s go to localhost on our host machine browser. Whoo !! we are accessing docker2 from our host machine. All works like magic.