在Docker容器中运行Docker:无法连接到Docker守护程序 [英] Running Docker inside Docker container: Cannot connect to the Docker daemon
问题描述
我创建了一个Dockerfile以在Docker内部运行Docker:
I created a Dockerfile to run Docker inside Docker:
FROM ubuntu:16.04
RUN 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 - &&\
apt-key fingerprint 0EBFCD88
RUN 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 && \
systemctl enable docker
启动容器并运行docker ps后,我得到了:
无法在unix:///var/run/docker.sock上连接到Docker守护程序。docker守护程序正在运行吗?
After i launched my container and run docker ps i got: "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"
我执行了我的容器中的命令dockerd导致:
i executed the command dockerd inside my container resulted:
启动守护程序时出错:初始化网络控制器时出错:获取控制器实例时出错:创建NAT链文档失败:iptables失败:iptables -t nat -N DOCKER:iptables v1.6.0:无法初始化iptables表`nat':权限被拒绝(您必须是root用户)
也许iptables或您的内核需要升级。
(退出状态3)
Error starting daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.6.0: can't initialize iptables table `nat': Permission denied (you must be root) Perhaps iptables or your kernel needs to be upgraded. (exit status 3)
请告知
推荐答案
如果您真的想在其他Docker容器中运行Docker容器,则应使用Docker提供的现有映像( https://hub.docker.com/_/docker ),而不是创建自己的基本映像:选择标记为 dind
( d ocker 中 d ocker)或< docker_version> -dind
(如 18.09.0-dind
)。如果您想运行自己的映像(尽管不建议这样做),请不要忘记使用-privileged
选项运行它(这就是为什么会出错的原因)。
If you really want to run a Docker container inside an other Docker container, you should use already existing images provided by Docker (https://hub.docker.com/_/docker) instead of creating your own base image : choose images tagged as dind
(docker in docker) or <docker_version>-dind
(like 18.09.0-dind
). If you want to run your own image (not recommended though), don't forget to run it with --privileged
option (that's why you get the error).
带有 docker
官方图片的示例:
# run Docker container running Docker daemon
docker run --privileged --name some-docker -d docker:18.09.0-dind
# run hello-world Docker image inside the Docker container previously started
docker exec -i -t some-docker docker run hello-world
不过,我同意@DavidMaze的评论以及他提到的参考博客文章(请勿将Docker-in-Docker用于CI ):应尽可能避免使用Docker-in-Docker。
Nevertheless, I agree with @DavidMaze comment and the reference blog post he referred to (Do not use Docker-in-Docker for CI) : Docker-in-Docker should be avoided as much as possible.
这篇关于在Docker容器中运行Docker:无法连接到Docker守护程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!