docker出现Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon run

问题描述

docker服务正常启动,状态是running

[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/docker.service.d
           └─flannel.conf
   Active: active (running) since Sat 2018-11-03 05:55:57 CST; 17s ago
     Docs: http://docs.docker.com
 Main PID: 4417 (dockerd-current)
   Memory: 14.9M
   CGroup: /system.slice/docker.service
           ├─4417 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current --default-runtime=docker-runc --exec-opt native.cgroupdriver=systemd --userland...
           └─4421 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libc...

但是在操作docker时候(例如 docker search redis)

出现Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

[root@localhost ~]# docker search swarm
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

查询解决办法一:docker服务没有启动,重新启动docker服务

https://blog.csdn.net/zxzxzxzx2121/article/details/61914870/

查询解决办法二:重新载入systemd,扫描新的或者变动的单元,然后重新启动docker服务

https://www.cnblogs.com/forturn/p/9371841.html

查询解决办法三:权限问题,使用root账户或者使用sudo 重新启动docker服务

https://segmentfault.com/q/1010000005040763

解决办法四:使用 -H 可以改变docker进程监听指定的IP和端口。默认情况下,docker会监听 unix:///var/run/docker.sock,只允许本地的root用户连接。

https://www.widuu.com/archives/01/947.html 

从这里我们看到大致是要么用户没有权限或者是监听的端口不正确。

回想到我是在根据此文档http://dockone.io/article/227搭建Swarm集群的时候,出现这个问题。

文档中明确说centos7 在docker的配置中OPENTOS添加-H tcp://0.0.0.0:2375 因为我的系统是centos7添加了这个iP和端口的监听。

那么我们来查看一下docker的配置文件

cat /etc/sysconfig/docker

配置文件如下:

[root@localhost ~]# cat /etc/sysconfig/docker
# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled=false -H tcp://0.0.0.0:2375'
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

# Do not add registries in this file anymore. Use /etc/containers/registries.conf
# instead. For more information reference the registries.conf(5) man page.

# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp

# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
# LOGROTATE=false

# docker-latest daemon can be used by starting the docker-latest unitfile.
# To use docker-latest client, uncomment below lines
#DOCKERBINARY=/usr/bin/docker-latest
#DOCKERDBINARY=/usr/bin/dockerd-latest
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest
[root@localhost ~]# 

文档中提到centos6.6 还必须再追加-H unix:///var/run/docker.sock

抱着侥幸的姿势再试一试喽..

追加完-H unix:///var/run/docker.sock监听查看docker配置信息

[root@localhost ~]# cat /etc/sysconfig/docker
# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled=false -H tcp://0.0.0.0:2375  -H unix:///var/run/docker.sock'
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

# Do not add registries in this file anymore. Use /etc/containers/registries.conf
# instead. For more information reference the registries.conf(5) man page.

# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp

# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
# LOGROTATE=false

# docker-latest daemon can be used by starting the docker-latest unitfile.
# To use docker-latest client, uncomment below lines
#DOCKERBINARY=/usr/bin/docker-latest
#DOCKERDBINARY=/usr/bin/dockerd-latest
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest

刷新配置,重新启动docker ,完美解决问题..

回顾反思:

其实,我们也可以删除对-H tcp://0.0.0.0:2375 因为他默认监听的是-H unix:///var/run/docker.sock。

总结:

要么权限不够,要么docker服务没有启动,再要么是监听的ip和端口不正确。

 


 

  • 11
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值