root@devops:~# docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock docker:latest sh / # docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world c1ec31eb5944: Pull complete Digest: sha256:1408fec50309afee38f3535383f5b09419e6dc0925bc69891e79d84cc4cdcec6 Status: Downloaded newer image for hello-world:latest
Hello from Docker! This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
/ #
这样启动的 hello-world 会再宿主机上真实的启动一个容器
1 2 3 4 5 6 7
root@devops:~/poc# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 174757c01f08 hello-world "/hello" About a minute ago Exited (0) 59 seconds ago inspiring_morse c0d09eee09a6 docker:latest "dockerd-entrypoint.…" About a minute ago Up About a minute 2375-2376/tcp sharp_dubinsky e244d45174b3 dockerhub.qingcloud.com/doubao/rancher:latest "entrypoint.sh" 2 weeks ago Up 39 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp rancher root@devops:~/poc#
root@devops:~# docker run --privileged --name dind-container -d docker:latest 8cecade0bdc3e3a8de171caaf5706253bdc7920473512692a5f5ea6610725ef2 root@devops:~# docker exec -it dind-container sh / # docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world c1ec31eb5944: Pull complete Digest: sha256:1408fec50309afee38f3535383f5b09419e6dc0925bc69891e79d84cc4cdcec6 Status: Downloaded newer image for hello-world:latest
Hello from Docker! This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
/ # docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1253974190f1 hello-world "/hello" About a minute ago Exited (0) About a minute ago thirsty_mclean / #
root@devops:~/poc# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8cecade0bdc3 docker:latest "dockerd-entrypoint.…" About a minute ago Up About a minute 2375-2376/tcp dind-container e244d45174b3 dockerhub.qingcloud.com/doubao/rancher:latest "entrypoint.sh" 2 weeks ago Up 56 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp rancher root@devops:~/poc#
踩坑
第一种 DinD 的启动方式,是直接挂载 sock,然后直接 docker run -it 容器 sh 就直接进入到容器中了,就可以直接操作容器内部的 docker。
第二种 DinD 的启动方式,是没有挂载 sock,所以不可以直接 docker run -it 容器 sh,这样进入到容器中,docker 的 sock 还没有完全启动,所以需要先 docker run 启动容器,然后再 docker exec 进入容器,然后就可以操作容器内的 docker 了。