Container's within Container : DIND vs DOOD

Docker is a versatile tool that simplifies the process of building, running, and managing containers. In this comprehensive guide, we'll explore two intriguing methods for running Docker within a container:

One, involves mounting the host's Docker socket (DOOD) and Other, explores the Docker-in-Docker (DIND) approach.

Let's dive into each method, understand their applications, benefits, and considerations, and ultimately explore the capabilities of Docker in Container.

Prerequisite:

  • Docker Installed

  • Internet Connection to pull Image

Method 1: Docker with Docker (DOOD)

Direct interaction with the host's Docker daemon: This method connects the container to the host's Docker daemon using the docker.sock socket, allowing you to manage containers and images from within the container.

Steps:

  1. Run the following command to launch one container by mounting /var/run/docker.sock to the same volume (-v) /var/run/docker.sock. This mounts the docker.sock of the host system into the container.

     docker run -it -v /var/run/docker.sock:/var/run/docker.sock docker
    
  2. You can now run docker commands within the container as if you were running them on the host system. For example, you can pull an image and run a container.

     docker pull ubuntu
     docker run -it --name ubuntu
    
  3. Any containers you create within the docker container will be visible on the host system and vice versa.

Advantages:

  • Simpler setup

  • No need for a separate Docker daemon

Disadvantages:

  • Less secure, as the container has access to the host's Docker daemon with heightened capabilities, which could be a security concern in certain environments

  • Potential for conflicts with other containers using the same Docker daemon

Method 2: Docker in Docker (DIND)

Runs an isolated Docker daemon within the container: This method creates a separate, isolated Docker environment within the container using a special image called docker:dind. This method creates a child container inside a container.

Steps:

  1. Launch a new container using the docker image with the dind tag. For example:

     docker run --privileged -d --name <container-name> docker:dind
    
  2. You can now execute commands within the dind container using the docker exec command. For example, to launch an ubuntu container within the dind container:

     docker exec -it <container-name> /bin/bash
     docker run -it --name ubuntu
    
  3. Containers created within the dind container are completely encapsulated from the host system and other containers.

Advantages:

  • More secure, as the container has its own isolated Docker daemon

  • No conflicts with other containers

  • Useful for testing and development of Docker-based applications

Disadvantages:

  • More complex setup

  • Requires a privileged container

  • Can be resource-intensive, as it requires running an additional Docker daemon inside the container, which consumes more memory and CPU resources

Note: Method 2 is more secure than Method 1 because it provides complete isolation between the containers.

When to use each method:

  • DOOD: Consider this for simple tasks where security is not a major concern and you want to avoid the overhead of a separate Docker daemon.

  • DIND: Choose this for more secure scenarios, testing and development, or when you need complete isolation between containers.

Real-world Examples :

  • DOOD: The popular development tool docker-compose uses the DOOD method to manage multiple containers on the same host during the development process.

  • DIND: The Jenkins Docker Pipeline plugin leverages the DIND method to enable building, testing, and deploying Docker-based applications within Jenkins pipelines.

Conclusion

Running Docker in a Container opens up new possibilities for containerization and software development workflows. DIND's nested containerization raises security concerns, making it less preferable for production environments where isolation and hardened security are paramount. Meanwhile, DOOD, by running containers directly on the host's Docker daemon, offers a more straightforward and secure model.

Whether you choose to mount the host's Docker socket Docker with Docker method (DOOD) or use the DIND, careful consideration of security implications and resource requirements is important.

2
Subscribe to my newsletter

Read articles from Hrushikesh Dagwar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hrushikesh Dagwar
Hrushikesh Dagwar

Adaptable individual with a passion for continuous learning and growth, I bring a diverse skill set, leadership and mentoring abilities, and technical and creative expertise to any team or individual.