Skip to content
  • 安装:
bash
git clone --branch master --depth 1 https://gh-proxy.org/https://github.com/nelvko/clash-for-linux-install.git \
  && cd clash-for-linux-install \
  && bash install.sh
  • 配置:/opt/clash/runtime.yaml
    • tun.enable: false,如果开启了会导致如 docker 等的使用异常
    • system-proxy.enable: true,系统代理

对于 Linux如果开启过,那么所有的代理都会走 7890 端口

bash
env | grep -i proxy
# 会输出下面的内容
HTTP_PROXY=http://127.0.0.1:7890
https_proxy=http://127.0.0.1:7890
http_proxy=http://127.0.0.1:7890
ALL_PROXY=socks5h://127.0.0.1:7890
no_proxy=localhost,127.0.0.1,::1
NO_PROXY=localhost,127.0.0.1,::1
HTTPS_PROXY=http://127.0.0.1:7890
all_proxy=socks5h://127.0.0.1:7890
  • 直接clash off && clash tun off 是不会恢复的
  • 需要通过unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy no_proxy NO_PROXY 进行恢复

Docker镜像下载问题排查日志

时间: 2025-12-29 20:35 问题: Docker无法正常下载镜像


问题现象

  • docker pull 命令超时或失败
  • 日志显示: context deadline exceededEOF 错误

排查过程

1. 环境检查

代理环境变量:

HTTP_PROXY=http://127.0.0.1:7890
https_proxy=http://127.0.0.1:7890
http_proxy=http://127.0.0.1:7890
ALL_PROXY=socks5h://127.0.0.1:7890
NO_PROXY=localhost,127.0.0.1,::1

Clash状态:

  • 进程运行中: /opt/clash/bin/mihomo -d /opt/clash -f /opt/clash/runtime.yaml
  • 系统代理已启用
  • TUN模式已启用 - 这是问题的关键!

Docker服务状态:

2. 网络测试

代理端口测试:

bash
curl -x http://127.0.0.1:7890 -I https://www.google.com --connect-timeout 5

结果: 超时 (代理连接不稳定)

直接连接测试:

bash
curl -I https://registry-1.docker.io --connect-timeout 5

结果: SSL连接错误 (说明直接访问Docker Hub有问题)

镜像源测试:

bash
curl -I https://docker.m.daocloud.io/v2/ --connect-timeout 5

结果: 成功 (HTTP/2 401, 正常响应)

3. Clash配置分析

Clash运行时配置中已包含Docker相关域名的DIRECT规则:

yaml
rules:
  - DOMAIN-SUFFIX,docker.io,DIRECT
  - DOMAIN-SUFFIX,registry-1.docker.io,DIRECT
  - DOMAIN-SUFFIX,auth.docker.io,DIRECT
  - DOMAIN-SUFFIX,production.cloudflare.docker.com,DIRECT
  - DOMAIN-SUFFIX,docker.m.daocloud.io,DIRECT
  - DOMAIN-SUFFIX,docker.mirrors.ustc.edu.cn,DIRECT
  - DOMAIN-SUFFIX,mirror.ccs.tencentyun.com,DIRECT

关键发现: Clash启用了TUN模式(透明代理):

yaml
tun:
  enable: true
  stack: system
  auto-route: true
  auto-redir: true
  auto-redirect: true
  auto-detect-interface: true
  dns-hijack:
    - any:53
    - tcp://any:53
  strict-route: true

TUN模式会创建一个虚拟网络接口(Meta),并修改系统路由表,拦截所有网络流量。即使规则中配置了DIRECT,TUN模式仍然会干扰Docker的网络连接。

4. Docker服务配置

当前状态:

  • /etc/systemd/system/docker.service.d/ 目录下:
    • proxy.conf.bak - 备份的代理配置
    • proxy.conf.disabled - 禁用的代理配置
  • 没有激活的代理配置文件

代理配置内容:

ini
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"

问题根因

  1. Clash TUN模式拦截了所有网络流量,包括Docker的连接
  2. TUN模式的DNS劫持dns-hijack: any:53)干扰了Docker的DNS解析
  3. 路由表被Clash修改,流量被强制走Clash的TUN接口
  4. Clash代理连接不稳定,导致Docker通过代理拉取镜像时超时

解决方案

最终解决方案: 禁用Clash TUN模式

步骤:

  1. 修改Clash配置文件 /opt/clash/runtime.yaml:
    yaml
    tun:
      enable: false  # 改为false
  2. 停止Clash服务: killall mihomo
  3. 重新启动Clash: /opt/clash/bin/mihomo -d /opt/clash -f /opt/clash/runtime.yaml

验证:

bash
# 检查路由表中没有Meta接口
ip route show

# 测试Docker镜像拉取
docker pull hello-world

Clash与Docker共存建议

可以开启Clash,但必须禁用TUN模式:

  1. 禁用TUN模式 - 这是关键!

    yaml
    tun:
      enable: false
  2. 保持系统代理模式 - Clash会通过HTTP/HTTPS代理工作

    yaml
    system-proxy:
      enable: true
  3. Docker服务配置 - 保持无代理配置

    • 确保 /etc/systemd/system/docker.service.d/ 目录下没有激活的proxy.conf
  4. 环境变量 - 系统环境变量中的代理设置不影响Docker daemon(因为Docker服务没有配置代理)


验证结果

测试1: hello-world镜像(小镜像)

bash
docker pull hello-world

结果: ✅ 成功

测试2: alpine镜像(小镜像)

bash
docker pull alpine:latest

结果: ✅ 成功

测试3: nginx镜像(大镜像)

bash
docker pull nginx:latest

结果: ⚠️ 超时(可能是镜像源网络问题,与Clash无关)


总结

问题: Clash的TUN模式(透明代理)拦截了Docker的网络流量,导致镜像下载失败。

解决方案: 禁用Clash的TUN模式,使用系统代理模式(HTTP/HTTPS代理)。

Clash与Docker可以共存,前提是:

  1. 禁用TUN模式
  2. 使用系统代理模式
  3. Docker服务不配置代理

配置文件位置:

  • Clash配置: /opt/clash/runtime.yaml
  • Docker配置: /etc/docker/daemon.json
  • Docker服务代理配置: /etc/systemd/system/docker.service.d/

修复命令

bash
# 1. 禁用Clash TUN模式
python3 << 'PYEOF'
with open('/opt/clash/runtime.yaml', 'r') as f:
    content = f.read()
content = content.replace('tun:\n  enable: true', 'tun:\n  enable: false')
with open('/opt/clash/runtime.yaml', 'w') as f:
    f.write(content)
print("TUN模式已禁用")
PYEOF

# 2. 重启Clash
killall -9 mihomo
sleep 2
/opt/clash/bin/mihomo -d /opt/clash -f /opt/clash/runtime.yaml > /dev/null 2>&1 &

# 3. 验证路由表(应该没有Meta接口)
ip route show

# 4. 测试Docker
docker pull hello-world

修复完成时间: 2025-12-29 20:35 修复状态: ✅ 成功

正在精进