在 Docker 中运行 Docker DinD

Step-By-Step

Step 1: Pull the DinD Image

The first step is to obtain the DinD image. Open your terminal and run the following command:

1
docker pull docker:20.10-dind

This command fetches the DinD image from Docker Hub, tagged as version 20.10.

Step 2: Launch the DinD Container

Now that you have the DinD image, you can spin up a container. Run the following command:

1
docker run --privileged --name dind-container -d docker:20.10-dind

In this command:

  • --privileged: This flag grants the container additional privileges, which are necessary for running Docker within a container.
  • --name dind-container: Assigns a name to the container, allowing you to easily manage it later.
  • -d: Runs the container in detached mode, freeing up your terminal while the container runs in the background.

Step 3: Access the DinD Container

To interact with the DinD container, you need to access its shell. Execute the following command:

1
docker exec -it dind-container sh

This command opens an interactive shell within the DinD container, giving you direct access to its environment.

Step 4: Test Docker Within the DinD Container

Now that you’re inside the DinD container’s shell, you can run Docker commands as if you were working on a standalone Docker host. For instance, you can try:

1
docker info

This command will display information about the Docker environment running inside the DinD container.

Step 5: Run Containers Inside the DinD Container

While the primary purpose of DinD is to run Docker, you can also create and manage containers within the DinD container itself. This can be particularly useful for running tests or isolated development environments.

1
docker run -it --rm alpine

This command will run an Alpine Linux container interactively within the DinD container.

Step 6: Clean Up

When you’re done experimenting with DinD, you can clean up by stopping and removing the container:

1
2
docker stop dind-container
docker rm dind-container

在 Docker 中运行 Docker 可以成为各种开发和测试场景中的强大工具。然而,值得注意的是,DinD 也有其自身的一系列挑战和安全考虑。由于潜在的安全风险和性能开销,不建议将其用于生产用途。然而,对于独立测试和实验,DinD 可以成为 Docker 工具包的宝贵补充。

References


在 Docker 中运行 Docker DinD
https://www.frytea.com/post/20240630125236.html
作者
Tianlun Song
发布于
2024年6月30日
更新于
2024年6月30日
许可协议