科技 > 操作系统 > Unix

【已解决】Cannot connect to the Docker daemon at unix:///var/run/docker.sock.

164人参与 2024-07-31 Unix

最近遇到一台服务器重启后docker.service无法启动,记录解决过程。

问题现象

1.执行docker ps后,终端报错如下:

cannot connect to the docker daemon at unix:///var/run/docker.sock. is the docker daemon running?

2.执行systemctl status docker查看详细信息如下:
在这里插入图片描述

解决过程

1. 检查docker.service和docker.socket文件是否异常

不看不知道,一看吓一跳,好家伙,这台服务器上这两个文件都不翼而飞,于是从另一台docker功能正常的服务器拷贝上述两个文件到本机,由于docker安装时未修改默认配置,所以两个文件均未修改.

1.1. 修改docker.service

执行vim /lib/systemd/system/docker.service,添加以下内容:

[unit]
description=docker application container engine
documentation=https://docs.docker.com
after=network-online.target docker.socket firewalld.service containerd.service time-set.target
wants=network-online.target containerd.service
requires=docker.socket

[service]
type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
execstart=/usr/bin/dockerd -h fd:// --containerd=/run/containerd/containerd.sock
execreload=/bin/kill -s hup $mainpid
timeoutstartsec=0
restartsec=2
restart=always

# note that startlimit* options were moved from "service" to "unit" in systemd 229.
# both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
startlimitburst=3

# note that startlimitinterval was renamed to startlimitintervalsec in systemd 230.
# both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
startlimitinterval=60s

# having non-zero limit*s causes performance problems due to accounting overhead
# in the kernel. we recommend using cgroups to do container-local accounting.
limitnofile=infinity
limitnproc=infinity
limitcore=infinity

# comment tasksmax if your systemd version does not support it.
# only systemd 226 and above support this option.
tasksmax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
delegate=yes

# kill only the docker process, not all processes in the cgroup
killmode=process
oomscoreadjust=-500

[install]
wantedby=multi-user.target

1.2. 修改docker.socket

执行vim /lib/systemd/system/docker.socket,添加以下内容:

vim /lib/systemd/system/docker.socket

[unit]
description=docker socket for the api

[socket]
# if /var/run is not implemented as a symlink to /run, you may need to
# specify listenstream=/var/run/docker.sock instead.
listenstream=/run/docker.sock
socketmode=0660
socketuser=root
socketgroup=docker

[install]
wantedby=sockets.target

2. 重启docker

systemctl daemon-reload
systemctl restart docker

# 验证是否修复
docker ps
# 相同报错
cannot connect to the docker daemon at unix:///var/run/docker.sock. is the docker daemon running?

执行以上命令重启docker后,发现报错还在,执行journalctl -u docker.service -f查看docker服务详细信息,终端输出如下:
在这里插入图片描述

3. 检查containerd

执行命令which containerd, 发现服务器未安装containerd,执行以下命令安装

## 安装containerd
apt-get install containerd

## 再次查看
which containerd
## 输出如下
/usr/bin/containerd

4. 执行步骤2,再次重启docker

docker重启后,执行docker ps,此时docker已经可以正常使用:
在这里插入图片描述

5. 另一个小问题

使用过程中发现docker命令的tab键命令补全功能失效,在网上找到了解决方式:

## 1.下载自动补全脚本:
curl -o /etc/bash_completion.d/docker https://raw.githubusercontent.com/docker/cli/master/contrib/completion/bash/docker
## 2.重新加载bash配置:
source /etc/bash_completion.d/docker

至此,该问题已修复,docker可正常使用

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

docker报错Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the d

07-31

解决没有足够权限访问Docker守护进程的问题permission denied while trying to connect to the Docker daemon socket at unix

07-31

【supervisor】 问题处理 unix:///var/run/supervisor/supervisor.sock no such file

07-31

C语言UNIX域套接字CS模型

07-31

Unix 哲学到底给现代编程带来哪些重要启示?

07-31

IO进程——系统IO与文件IO

07-31

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论