Ubuntu 系统 Docker 启动失败(iptables/nf\_tables)

Ubuntu 系统 Docker 启动失败(iptables/nf_tables)问题排查与修复详解

在 Ubuntu 系统中安装 Docker 后,有部分用户在启动 Docker 服务时遇到如下报错:

bash 复制代码
Failed to start Docker Application Container Engine.
failed to create NAT chain DOCKER: iptables v1.8.7 (nf_tables): Could not fetch rule set generation id: Invalid argument

本文将详细解释该问题的原因,并提供完整的修复方案。


问题现象

使用以下命令启动 Docker:

bash 复制代码
sudo systemctl start docker

执行后,使用以下命令查看docker运行情况

bash 复制代码
sudo systemctl status docker

发现状态为 failed

使用以下命令查看详细错误日志

bash 复制代码
sudo journalctl -u docker.service --no-pager --since "10 minutes ago"

伴随日志提示:

复制代码
failed to create NAT chain DOCKER: iptables v1.8.7 (nf_tables): Could not fetch rule set generation id: Invalid argument

这是 Docker 在使用默认 iptables-nft 后端时,与内核的 nf_tables 实现存在兼容性问题,无法创建 NAT 表中用于容器网络的 DOCKER chain。


原因分析

Ubuntu 从 20.04 起,iptables 默认使用 nf_tables 模式,而 Docker 更稳定地支持 iptables-legacy 模式。

这就导致 Docker 启动时无法正确配置网络桥接规则,从而守护进程失败退出。


解决方案:切换为 iptables-legacy 模式

1. 确保系统已安装 iptables(非 nft 工具包)

bash 复制代码
sudo apt install iptables

2. 执行切换命令(无需单独安装 iptables-legacy 包)

bash 复制代码
sudo update-alternatives --config iptables

系统会提示你可选项,例如:

选择带有 iptables-legacy 的序号(如 1),然后回车。

同理,切换 IPv6:

bash 复制代码
sudo update-alternatives --config ip6tables

3. 若缺少 iptables-legacy 可用项,可软链接模拟:

bash 复制代码
sudo ln -sf /usr/sbin/iptables /usr/sbin/iptables-legacy
sudo ln -sf /usr/sbin/ip6tables /usr/sbin/ip6tables-legacy

sudo update-alternatives --install /usr/sbin/iptables iptables /usr/sbin/iptables-legacy 100
sudo update-alternatives --install /usr/sbin/ip6tables ip6tables /usr/sbin/ip6tables-legacy 100

然后再次运行 --config 命令进行选择。

4. 重启 Docker 服务

bash 复制代码
sudo systemctl daemon-reexec
sudo systemctl restart docker

5. 验证是否恢复正常

bash 复制代码
docker info
docker run hello-world

如无报错即修复成功。


相关推荐
lcreek3 小时前
Linux信号机制详解:阻塞信号集与未决信号集
linux·操作系统·系统编程
shandianchengzi3 小时前
【记录】Tailscale|部署 Tailscale 到 linux 主机或 Docker 上
linux·运维·docker·tailscale
John Song4 小时前
Linux机器怎么查看进程内存占用情况
linux·运维·chrome
sichuanwuyi4 小时前
Wydevops工具的价值分析
linux·微服务·架构·kubernetes·jenkins
持戒波罗蜜5 小时前
ubuntu20解决intel wifi 驱动问题
linux·驱动开发·嵌入式硬件·ubuntu
不做无法实现的梦~5 小时前
使用ros2来跑通mid360的驱动包
linux·嵌入式硬件·机器人·自动驾驶
点云SLAM5 小时前
C++内存泄漏检测之Windows 专用工具(CRT Debug、Dr.Memory)和Linux 专业工具(ASan 、heaptrack)
linux·c++·windows·asan·dr.memory·c++内存泄漏检测·c++内存管理
肉肉心很软5 小时前
使用onlyoffice实现文件预览编辑 + Docker一键部署流程
运维·docker·容器
-大头.5 小时前
Docker实战:构建高性能MySQL主从复制集群(读写分离)
mysql·docker·容器
LuiChun5 小时前
Docker Compose 容器服务查询与文件查看操作指南(Windows Docker Desktop 版)【一】
linux·运维·windows·docker·容器