我想转移到Docker,所以我刚刚开始摆弄它。我已经在VirtualBox Ubuntu 15.10 (Wily Werewolf)安装上安装了Docker,然后按照这里的建议,我尝试运行一个基本的nginx Docker镜像:

$ docker run --name mynginx1 -P -d nginx
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

所以我检查了Docker是否在运行:

$ sudo service docker status
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since vr 2015-11-06 08:41:48 CET; 15min ago
     Docs: https://docs.docker.com
 Main PID: 7542 (docker)
   CGroup: /system.slice/docker.service
           └─7542 /usr/bin/docker daemon -H fd://

nov 06 08:41:47 kramer65-VirtualBox systemd[1]: Starting Docker Application Container Engine...
nov 06 08:41:47 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:47.900410966+01:00" level=info msg="API ...ock"
nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.033514149+01:00" level=info msg="Fire...lse"
nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.141594321+01:00" level=info msg="Defa...ess"
nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.416294436+01:00" level=warning msg="Y...it."
nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.565507576+01:00" level=info msg="Load...rt."
nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.567907022+01:00" level=info msg="Load...ne."
nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.567945214+01:00" level=info msg="Daem...ion"
nov 06 08:41:48 kramer65-VirtualBox docker[7542]: time="2015-11-06T08:41:48.567969891+01:00" level=info msg="Dock....9.0
nov 06 08:41:48 kramer65-VirtualBox systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

这表明Docker守护进程实际上已经在运行,但为了确保我只是手动启动了Docker守护进程:

$ sudo docker daemon
INFO[0000] API listen on /var/run/docker.sock           
INFO[0000] [graphdriver] using prior storage driver "aufs" 
INFO[0000] Firewalld running: false                     
INFO[0000] Default bridge (docker0) is assigned with an IP address XXX.XX.X.X/XX. Daemon option --bip can be used to set a preferred IP address 
WARN[0000] Your kernel does not support swap memory limit. 
INFO[0000] Loading containers: start.                   

INFO[0000] Loading containers: done.                    
INFO[0000] Daemon has completed initialization          
INFO[0000] Docker daemon                                 commit=76d6bc9 execdriver=native-0.2 graphdriver=aufs version=1.9.0

然后我尝试再次运行图像,但结果相同:

$ docker run --name mynginx1 -P -d nginx
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

我试着执行命令,但没有效果。我哪里做错了?


当前回答

给予非根访问-从docker

如果docker组不存在,请添加。

$ sudo groupadd docker

将连接用户“${user}”添加到docker组中。

更改用户名以匹配您的首选用户。

您可能需要注销并重新登录 这才生效。

$ sudo gpasswd -a ${USER} docker

重新启动Docker守护进程。

$ sudo service docker restart

其他回答

我也有同样的问题。为了解决这个问题我已经挣扎了两天了。

只有当我这样做的时候,它才起作用:

根据Docker的教程,你需要添加Docker密钥,如果还没有添加,使用: $ sudo wget - qo - https://get.docker.com/gpg | sudo apt-key add - 然后确保你为自己授予docker特权,使用: $ sudo usermod -aG docker $USER

希望这也能帮助到你。

由于docker在启动时绑定到一个由root拥有的unix套接字,因此使用'sudo'和docker命令一起工作。

通常,下面的命令可以达到目的:

sudo service docker restart

这,而不是docker启动的情况下,docker似乎已经在运行。

如果这是可行的,那么在另一个答案和这个GitHub问题中,如果你还没有将自己添加到docker组中,请运行:

sudo usermod -aG docker <your-username> 

你很可能已经准备好了。


至于其他人遇到这个问题,在某些操作系统的docker在你安装后没有立即启动,因此,同样的不能连接到守护进程消息出现。在这种情况下,您可以首先通过执行以下命令检查Docker服务的状态来验证Docker确实没有运行:

sudo service docker status

如果输出看起来像:docker stop/waiting,而不是docker start/running, process 15378,那么它显然意味着docker不是活动的。在这种情况下,确保你的开头是:

sudo service docker start

和之前一样,你很有可能就可以出发了。

我在亚马逊EC2实例上也遇到过同样的错误。该问题在重新启动实例后得到修复。

I also had the same issue. The problem was in sockets allocated to docker-daemon and docker-client. First, permission was not set for the docker-client on docker.sock You can set it using "sudo usermod -aG docker $USER" Then check your bash file where the docker-client is running, For me it was on 0.0.0.0:2375, while docker-daemon was running on unix socket.(It was set in the configuration file of dockerd). Just comment the bash-line and it'll work fine. But if you want to make it work on TCP port instead of unix socket, change the configuration file of dockerd and set it on 0.0.0.0.2375 and keep the line in bash as it is if present or set it to 0.0.0.0:2375.