|
| 1 | +# Docker Inside Docker (DinD) Guide 🚀 |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This guide explains how to set up Docker inside Docker (DinD). This approach is useful for CI/CD pipelines, enabling Docker commands to be executed within a Docker container. |
| 6 | + |
| 7 | +## Prerequisites 📋 |
| 8 | + |
| 9 | +- Docker installed on the host machine |
| 10 | +- Basic knowledge of Docker commands |
| 11 | +- Docker Compose (optional, for easier orchestration) |
| 12 | + |
| 13 | +## Step-by-Step Guide 🛠️ |
| 14 | + |
| 15 | +### Step 1: Install Docker 🐳 |
| 16 | + |
| 17 | +If Docker is not already installed on your host machine, follow these steps: |
| 18 | + |
| 19 | +1. **Update the package database:** |
| 20 | + |
| 21 | + ```sh |
| 22 | + sudo apt-get update |
| 23 | + |
| 24 | +2. **Install prerequisites:** |
| 25 | + ```sh |
| 26 | + sudo apt-get install apt-transport-https ca-certificates curl software-properties-common |
| 27 | +
|
| 28 | +3. **Add Docker’s official GPG key:** |
| 29 | + ```sh |
| 30 | + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
| 31 | + |
| 32 | + |
| 33 | +4. **Add Docker’s repository:** |
| 34 | + ```sh |
| 35 | + sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" |
| 36 | +
|
| 37 | +
|
| 38 | +5. **Install Docker CE:** |
| 39 | + ```sh |
| 40 | + sudo apt-get update |
| 41 | + sudo apt-get install docker-ce |
| 42 | + |
| 43 | + |
| 44 | +6. **Verify Docker installation:** |
| 45 | + ```sh |
| 46 | + docker --version |
| 47 | +
|
| 48 | +
|
| 49 | +### Step 2: Create a Dockerfile for DinD 📄 |
| 50 | +**Create a Dockerfile that uses the official Docker-in-Docker image:** |
| 51 | +```dockerfile |
| 52 | +# Dockerfile |
| 53 | +FROM docker:20.10.7-dind |
| 54 | + |
| 55 | +# Optional: Install any additional packages |
| 56 | +RUN apk add --no-cache git |
| 57 | + |
| 58 | +# Optional: Create a working directory |
| 59 | +WORKDIR /app |
| 60 | + |
| 61 | +# Optional: Copy application files |
| 62 | +COPY . /app |
| 63 | + |
| 64 | +# Entry point for the Docker container |
| 65 | +CMD ["sh"] |
| 66 | +``` |
| 67 | + |
| 68 | +### Step 3: Build the Docker Image 🏗️ |
| 69 | +**Build the Docker image from the Dockerfile:** |
| 70 | +```sh |
| 71 | +docker build -t my-dind-image . |
| 72 | +``` |
| 73 | + |
| 74 | +### Step 4: Run the DinD Container ▶️ |
| 75 | +**Run a container using the DinD image with necessary Docker daemon options:** |
| 76 | +```sh |
| 77 | +docker run --privileged --name my-dind-container -d docker:20.10.7-dind |
| 78 | +``` |
| 79 | +**--privileged grants the container extended privileges.** |
| 80 | +**--name assigns a name to the container.** |
| 81 | +**-d runs the container in detached mode.** |
| 82 | + |
| 83 | + |
| 84 | +### Step 5: Interact with Docker Inside the Container 🐙 |
| 85 | +1. **Access the running DinD container:** |
| 86 | + ```sh |
| 87 | + docker exec -it my-dind-container sh |
| 88 | + |
| 89 | +2. **Verify Docker daemon is running inside the container:** |
| 90 | + ```sh |
| 91 | + docker info |
| 92 | +
|
| 93 | +3. **Run Docker commands inside the container:** |
| 94 | + ```sh |
| 95 | + docker run hello-world |
| 96 | + |
| 97 | +### Step 6: Clean Up 🧹 |
| 98 | +**To stop and remove the DinD container:** |
| 99 | +```sh |
| 100 | +docker stop my-dind-container |
| 101 | +docker rm my-dind-container |
| 102 | +``` |
| 103 | +**To remove the Docker image:** |
| 104 | +```sh |
| 105 | +docker rmi my-dind-image |
| 106 | +``` |
| 107 | +### Conclusion 🎉 |
| 108 | +You have successfully set up Docker inside Docker (DinD). This configuration is ideal for CI/CD pipelines and allows you to run Docker commands within a Docker container. |
| 109 | + |
| 110 | + |
0 commit comments