Running Docker inside Docker (DinD) allows you to perform tasks such as building Docker images and starting containers within an already containerized environment.
To achieve dind we have to follow some steps:
Step 1: Install docker with the command
yum install docker -y
Step 2: Start and enable the docker with command
systemctl start docker
systemctl enable docker
Step 3: Pull Docker Image:
docker pull docker:dind
This command pulls the Docker image named docker:dind from the Docker Hub. This image contains the Docker daemon and is suitable for running Docker inside Docker.
Step 4: Run Docker in Docker Container:
docker run --privileged --name my-docker-container -d docker:dind
--privileged: This flag gives extended privileges to the container, which is necessary for running Docker inside Docker.
--name my-docker-container: Assigns a name to the running container (you can choose any name).
-d: Runs the container in the background (detached mode).
Step 5: Set Environment Variables:
export DOCKER_HOST=unix:///var/run/docker.sock
This command sets the DOCKER_HOST
environment variable to specify the location of the Docker daemon socket. In this case, it points to the Unix socket file /var/run/docker.sock
.
Step 6: Install Docker CLI:
docker exec -it my-docker-container sh
This command opens a shell inside the running container (my-docker-container
). From here, you can execute commands within the container.
Step 7: Inside the Container, Install the Docker CLI:
apk add docker-cli
This command uses the package manager apk to install the Docker CLI (docker-cli) inside the running Docker-in-Docker container.
After following these steps, you should have a running Docker-in-Docker container with the Docker CLI installed inside it.