场景

执行docker ps命令时发现异常:

[root@administrator yum.repos.d]# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running

执行如下常规操作均无解:

1.重启Docker:systemctl restart docker

2.停止Docker:systemctl stop docker

3.启动Docker:systemctl start docker

原因分析

首先:daemon默认监听/var/run/docker.sock这个文件,docker客户端把请求发往这里,daemon就能收到并做出响应。

其次:看异常信息推测:var/run/docker.sock文件异常,执行systemctl status docker.socket查看其状态

[root@administrator yum.repos.d]# systemctl status docker.socket
● docker.socket - Docker Socket for the API
   Loaded: loaded (/usr/lib/systemd/system/docker.socket; enabled; vendor preset: disabled)
   Active: failed (Result: resources) since 三 2022-06-15 23:36:57 CST; 52s ago
   Listen: /var/run/docker.sock (Stream)

6月 15 23:36:57 administrator systemd[1]: Closed Docker Socket for the API.
6月 15 23:37:20 administrator systemd[1]: docker.socket failed to listen on sockets: Address already in use
6月 15 23:37:20 administrator systemd[1]: Failed to listen on Docker Socket for the API.
6月 15 23:37:20 administrator systemd[1]: Unit docker.socket entered failed state.
6月 15 23:37:36 administrator systemd[1]: docker.socket failed to listen on sockets: Address already in use
6月 15 23:37:36 administrator systemd[1]: Failed to listen on Docker Socket for the API.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
[root@administrator yum.repos.d]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2022-06-15 23:36:57 CST; 1min 7s ago
     Docs: https://docs.docker.com
  Process: 6895 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=0/SUCCESS)
 Main PID: 6895 (code=exited, status=0/SUCCESS)

执行ls -l /var/run/ | grep docker 命令,发现docker.sock是一个目录,正常来说应该是一个文件。

[root@administrator yum.repos.d]# ls -l /var/run/ | grep docker
drwx------  8 root   root    160 6月  15 23:36 docker
drwxr-xr-x  2 root   root     40 6月   8 01:07 docker.sock

解决方案

执行rm -rf /var/run/docker.sock命令将其删除

[root@administrator yum.repos.d]# rm -rf /var/run/docker.sock

重启docker容器后发现其正常

[root@administrator yum.repos.d]# systemctl start docker
[root@administrator yum.repos.d]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED        STATUS         PORTS                    NAMES
02688799076d   portainer/portainer      "/portainer"             6 months ago   Up 4 seconds   0.0.0.0:9000->9000/tcp   portainer