docker服务起不来原因及解决

目录

报错 关键词:
Failed to start Docker Application Container Engine.

Failed to find iptables: exec: \"iptables\": executable file not found in $PATH

failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: Iptables not found

问题

docker指令无法使用,docker服务未正确运行。

执行docker images,报错

复制代码
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

无法连接docker daemon守护程序,无法连接服务socket,docker服务启动失败。

原因排查

查看docker服务的状态

执行 systemctl status docker 查看docker服务的状态

报错

root@test:~# 复制代码
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2024-03-11 20:34:54 CST; 6 days ago
     Docs: https://docs.docker.com
 Main PID: 3548 (code=exited, status=1/FAILURE)

Mar 11 20:34:54 test systemd[1]: docker.service: Service hold-off time over, scheduling restart.
Mar 11 20:34:54 test systemd[1]: docker.service: Scheduled restart job, restart counter is at 3.
Mar 11 20:34:54 test systemd[1]: Stopped Docker Application Container Engine.
Mar 11 20:34:54 test systemd[1]: docker.service: Start request repeated too quickly.
Mar 11 20:34:54 test systemd[1]: docker.service: Failed with result 'exit-code'.
Mar 11 20:34:54 test systemd[1]: Failed to start Docker Application Container Engine.

显示启动 Docker Application Container Engine失败。

尝试重启docker service

执行:systemctl restart docker.service, 尝试重启docker service

复制代码
root@test:/lib/systemd/system# systemctl restart docker.service
Job for docker.service failed because the control process exited with error code.
See "systemctl status docker.service" and "journalctl -xe" for details.

无法启动。

查看 log分析原因

执行:journalctl -xe

复制代码
Mar 18 13:28:40 test dockerd[24951]: time="2024-03-18T13:28:40.215890146+08:00" level=warning msg="Failed to find iptables: exec: \"iptables\": executable file not found in $PATH"
Mar 18 13:28:40 test dockerd[24951]: time="2024-03-18T13:28:40.218084167+08:00" level=info msg="stopping event stream following graceful shutdown" error="<nil>" module=libcontainerd namespace=moby
Mar 18 13:28:40 test dockerd[24951]: failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: Iptables not found
Mar 18 13:28:40 test systemd[1]: docker.service: Main process exited, code=exited, status=1/FAILURE
Mar 18 13:28:40 test systemd[1]: docker.service: Failed with result 'exit-code'.
-- Subject: Unit docker.service has failed
-- Unit docker.service has failed.

可以看出来是iptables找不到,这个服务没开起来,导致docker服务起不来。

执行systemctl status iptables

复制代码
root@test:/lib/systemd/system# systemctl status iptables
Unit iptables.service could not be found.

无法找到 iptables.service;有可能是该服务没起来,没安装,或者 iptables 实际上未作为服务运行,而是系统调用。

解决方案参考

参考chatgpt3.5答案:

如果系统中找不到 iptables 服务单元(Unit),这可能意味着 iptables 实际上未作为服务运行。在某些系统上,iptables 配置可能直接由系统调用而不是通过单独的服务。 在这种情况下,您可以尝试按照以下步骤解决Docker无法启动的问题:

  1. 检查 iptables 是否安装:首先,请确保 iptables 工具已安装在系统中。您可以使用以下命令检查 iptables 的安装状态:

which iptables

如果该命令返回路径,则表示 iptables 已安装。如果没有返回任何内容,您可能需要安装 iptables

  1. 手动加载 iptable_nat 模块:有时,即使 iptables 安装了,其相关内核模块也可能未加载。您可以尝试手动加载iptable_nat内核模块:

modprobe iptable_nat

  1. 启用 IP 转发:确保在 sysctl 配置中启用了 IP 转发。您可以编辑 /etc/sysctl.conf 文件并确保以下行未被注释(去掉前面的 # 符号),(在后面追加下面的代码)

net.ipv4.ip_forward = 1

保存文件后,运行以下命令以使更改生效:

sudo sysctl -p

  1. 重新启动 Docker 服务:一旦您已确保 iptables 正常工作并将所需的内核模块加载到系统中,尝试重新启动 Docker 服务

sudo systemctl restart docker

解决过程

尝试按照上述方案进行修改:

执行:
apt-get update && apt install iptables

显示已经安装

再次执行 systemctl status iptables

显示

复制代码
root@test:/lib/systemd/system# systemctl status iptables
Unit iptables.service could not be found.

说明iptables 配置可能直接由系统调用,而不是服务

执行

复制代码
root@test:/lib/systemd/system# which iptables
/sbin/iptables
root@test:/lib/systemd/system# modprobe iptable_nat
root@test:/lib/systemd/system# vi /etc/sysctl.conf
root@test:/lib/systemd/system# sysctl -p
fs.file-max = 2097152
net.ipv4.ip_forward = 1
root@test:/lib/systemd/system# systemctl status iptables
Unit iptables.service could not be found.
root@test:/lib/systemd/system# systemctl restart docker.service
root@test:/lib/systemd/system# systemctl status docker.service
● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2024-03-18 13:33:44 CST; 7s ago
     Docs: https://docs.docker.com
 Main PID: 28879 (dockerd)
    Tasks: 20
   CGroup: /system.slice/docker.service
           └─28879 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

运行成功

相关推荐
A小辣椒18 小时前
TShark:Wireshark CLI 功能
linux
A小辣椒1 天前
TShark:基础知识
linux
AlfredZhao1 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao2 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi
用户9718356334662 天前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪2 天前
linux 拷贝文件或目录到指定的位置
linux
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush43 天前
嵌入式linux学习记录十四、术语
linux·嵌入式