DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 Docker 报错 failed to allocate gateway IP 地址池耗尽

分析 Docker 启动容器报 failed to allocate gateway pool does not have available addresses 根因,详解网络清理与子网池规划方案。

问题现象

使用 Docker Compose 或 docker run 创建新容器时抛出错误,容器创建失败退出:

docker: Error response from daemon: failed to create endpoint web on network my-net: failed to allocate gateway (172.18.0.1): pool does not have any available addresses.

或者提示:

Error response from daemon: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network

触发根因

  1. 历史遗留的孤儿 Docker 网络耗尽了默认子网池:每次使用 Docker Compose 构建但未加 --down 清理,生成了大量 project_default 桥接网络,将预留的 172.17~172.31 网段全部占满。
  2. 手动指定的子网(Subnet)范围过小:例如分配了 /30 掩码,除去网络地址、广播地址和网关后无可分配地址。
  3. 本地物理路由与 Docker 默认网段冲突。

诊断排查步骤

  1. 列出当前所有已创建的 Docker 网络:
docker network ls
  1. 检查网络 IP 地址池分配情况:
docker network inspect $(docker network ls -q) | grep -E "Name|Subnet|Gateway"

解决步骤与操作命令

步骤一:安全清理未使用的无用 Docker 网络

执行系统网络修剪清理(不会影响正在运行的容器):

docker network prune -f

步骤二:彻底清理已退出的孤儿容器与网络

docker system prune -f

步骤三:在 daemon.json 中扩大默认预留地址池

如果需要常驻大量独立隔离网络,在 /etc/docker/daemon.json 中配置更宽广的子网段:

{
  "default-address-pools": [
    {
      "base": "172.80.0.0/16",
      "size": 24
    },
    {
      "base": "10.200.0.0/16",
      "size": 24
    }
  ]
}

重启 Docker 守护进程生效:

systemctl restart docker

验证方法

重新启动 Docker Compose 或容器:

docker run -d --name test-net-nginx --net my-custom-net nginx:alpine

容器正常创建并分配到了新的私有 IP,网络网关绑定无任何报错。