Launch a container in docker. Further, launch a container then install docker in it and again launch a container in containerized docker.

Sonam Thakur
4 min readSep 14, 2023

--

Launching a Docker container inside another Docker container, often referred to as “Docker-in-Docker” (DinD), can be useful for certain scenarios like testing and development. However, it’s important to note that running Docker inside a Docker container introduces complexities and potential security concerns, so use this approach cautiously and only when necessary. Here are the detailed steps to achieve this:

Step 1: Launch a Docker Container Start by launching a Docker container where you’ll install Docker and subsequently launch another container. Here’s how to do it:

bashCopy code
docker run -it --privileged --name docker-host ubuntu:latest

Explanation:

  • -it: Runs the container in interactive mode with a pseudo-TTY.
  • --privileged: Grants the container elevated privileges to manage containers within it (use with caution in production environments).
  • --name docker-host: Assigns a name to the container, making it easier to manage.

Step 2: Install Docker Inside the First Container Now that you’re inside the first container, you can install Docker. Since you’re using an Ubuntu image in this example, you can use apt to install Docker. Here are the commands:

bashCopy code
apt-get update
apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install -y docker-ce

This sequence of commands updates the package lists, installs the required dependencies, adds the Docker repository, and installs Docker.

Step 3: Launch a Container Inside the First Container (DinD) With Docker installed inside the first container, you can now launch another container inside it. This is where you achieve “Docker-in-Docker.” For this example, we’ll run an Nginx container:

bashCopy code
docker run -it --name dind-container --network host nginx

Explanation:

  • --name dind-container: Assigns a name to the container for easy reference.
  • --network host: Shares the host's network stack with the container, allowing the container to use the host's network directly.

You are now running a Docker container (Nginx in this case) inside another Docker container. You can interact with the Nginx container as you would with any other Docker container.

Important Considerations:

  • Using Docker-in-Docker can introduce complexities and security risks, as containers within containers might not have the same level of isolation as separate Docker instances.
  • It’s recommended to use alternative solutions like Docker Compose or Kubernetes for complex multi-container setups.
  • Be cautious when using the --privileged flag, as it grants extensive privileges to the container, potentially compromising the host system's security.
  • Running Docker inside Docker might not work on all cloud platforms or container runtimes, so be aware of compatibility issues.
  • Consider the use of Docker-in-Docker alternatives, like Docker outside of Docker (DooD), which involves connecting to the Docker socket of the host.

This guide demonstrates how to run Docker inside a Docker container for educational purposes. In production or security-critical environments, consider alternative solutions that offer better isolation and security controls.

Docker in Docker (also known as dind) is, as the name implies, running Docker on top of a Docker container. Controlling containers from a Docker container is not a particular use case but is often necessary to run CI tools such as Jenkins on top of a Docker container. It is not a specific use case but is often needed to run CI tools such as Jenkins on Docker containers.

This article describes two approaches to achieving Docker in Docker and introduces some points to consider when using Docker in Docker.

Docker in Docker Using dind

This method uses a container with Docker installed and runs a Docker daemon in the container separately from the host. Alpine based Docker official image and ubuntu based teracy/ubuntu are available as images for DinD. (dind tag) based on alpine, and teracy/ubuntu based on ubuntu.

The following is an example of the command for Docker-in-Docker using the official Docker image docker:stable-dind.

docker run --privileged --name dind6 -d docker:stable-dind
docker exec -it dind6 /bin/ash

Running Docker Inside Docker:

Hence container has launched inside the container……

THANKYOU !

--

--

Sonam Thakur
Sonam Thakur

Written by Sonam Thakur

Tech enthusiast | AWS Cloud | Devops Aspirant | Computer Sciences Engineer https://www.linkedin.com/in/sonam-thakur-43a447211/

No responses yet