在 Windows 上想跑 Linux 容器,最省事的组合是 WSL2 + Debian + docker-ce。这套组合真正的难点在于「拉不下来镜像」时的排查:docker pull 停在某一个 layer,日志看起来一堆 206 Partial Content 全部成功,最后 image 却不在 docker images 里。

这篇是把它彻底跑通之后整理的操作记录。整个安装过程前前后后花了四个小时,绝大部分时间不在装 Docker,而在解决下面这五个坑。

适用环境

  • Windows 10 22H2 / Windows 11(WSL2)
  • Debian 13 (trixie),Ubuntu 22.04+ 同理
  • systemd 已在 WSL 中启用:/etc/wsl.conf 里有 [boot] systemd=true,否则 systemctl 完全不可用,Docker 装上也起不来

坑一:sudo 在 WSL 里卡死

WSL 里 Debian 默认的 sudoers 要求用 TTY 输密码。结果就是所有这种写法都会永久挂住,看不到任何提示:

1
wsl -d Debian -- bash -c "sudo apt update"   # 卡住不动

解法很直接:需要 root 的命令一律用 --user root 跑,别指望 sudo 在非交互环境里工作。

1
wsl -d Debian --user root -- bash -c "apt update"

坑二:默认 apt 源连不上

deb.debian.org 在国内直连经常超时。换成清华源,注意 Debian 13 用 deb822 格式,不要写成老的 deb ... 单行。

/etc/apt/sources.list.d/0000debian.sources:

1
2
3
4
5
6
7
8
9
10
11
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/debian
Suites: trixie trixie-updates trixie-backports
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp

Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/debian-security
Suites: trixie-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp

坑三:docker-ce 源在个别包上被限速

用某些国内镜像源装 docker-ce 时,绝大多数包正常,唯独 docker-buildx-plugin 只有几十 KB/s,装到一半像卡死了。换清华的 docker-ce 源就好,同样必须是 deb822 格式:

/etc/apt/sources.list.d/docker.sources:

1
2
3
4
5
6
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian
Suites: trixie
Components: stable
Architectures: amd64
Signed-By: /etc/apt/keyrings/docker.gpg

坑四:containerd-snapshotter 在 WSL 上解包失败(最坑)

Docker 29.x 起默认启用 containerd-snapshotter。在 WSL2 的 overlayfs 上,它会在解包阶段出问题,而日志看上去全是成功的:

  • HTTP 层能看到 fetch response received 206 Partial Content
  • 但写盘时冒出 failed to cleanup "extract-xxx sha256:..." NotFound: snapshot does not exist
  • 结果是 pull 卡在某个 layer,最后失败,docker images 里什么都没有

定位方法:先把 daemon 日志开到 debug,再盯着 fetch 和 extract 两段对比。

1
2
3
# /etc/docker/daemon.json 里临时加 "log-level": "debug"
systemctl restart docker
journalctl -u docker -f | grep -iE 'snapshot|extract|cleanup|partial'

看到「HTTP 成功但 cleanup NotFound」就是它了。 解法是关掉 containerd-snapshotter,退回传统 overlay2:

1
{ "features": { "containerd-snapshotter": false } }

坑五:IPv6 握手卡死

部分加速镜像回源到 CloudFront 域名,走 IPv6 时链路有问题:能看到 HTTP 200 的响应头,但 TLS 握手超时。同一个域名,Windows 侧直连正常,WSL 里就是不通。

1
2
curl -4 https://example-mirror.example.com/   # 200,0.3s
curl https://example-mirror.example.com/ # TLS handshake timeout

先把宿主的 IPv6 关掉:

1
2
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1

持久化写进 /etc/sysctl.d/99-disable-ipv6.conf。同时 daemon.json 里加 "ipv6": false,让 Docker 不要自己再碰 IPv6。

可用的 daemon.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{
"data-root": "/var/lib/docker",
"iptables": false,
"ip-forward": false,
"ip-masq": false,
"ipv6": false,
"features": {
"containerd-snapshotter": false
},
"registry-mirrors": [
"https://docker.1ms.run"
],
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "3"
}
}

iptables: false 这一项在 WSL 里是必须的。WSL2 自己已经有 NAT,Docker 再去清空 iptables 规则会直接把网络搞断,表现为容器起来但宿主机失联。

镜像源怎么选

加速站 可用镜像 速度 备注
docker.1ms.run 大部分 一般 默认选它,偶尔个别 layer 卡在 99%
docker.m.daocloud.io 白名单 一般 非主流仓库直接拒
各类小站 不稳定 波动 经常 502,不建议长期用
官方 registry-1.docker.io 全部 拉不动 国内直连基本不可用

还有一个容易忽略的点:用具体版本号比 :latest 稳。同样是拉一个镜像,:latest 反复在最后一个 layer 失败,换成 :0.2.3 就正常了。原因多半是 latest 那个 manifest 指向的层更大,被加速站截断。优先去 Docker Hub tags 或项目 release 页挑一个确定的版本。

完整安装流程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wsl -d Debian --user root -- bash -c '
set -e
# 1) 换 apt 源(见坑二),2) 写 docker.sources(见坑三)
apt-get update
apt-get install -y ca-certificates curl gnupg
curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian/gpg \
| gpg --dearmor -o /etc/apt/keyrings/docker.gpg
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y \
docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 3) 写 daemon.json(见上),4) 关 IPv6(见坑五)
systemctl enable --now docker
'

# 5) 验证
wsl -d Debian -- docker run --rm hello-world

调试命令备查

1
2
3
4
5
journalctl -u docker -f                                   # 实时日志
journalctl -u docker --no-pager -n 50 | grep -E 'fetch|extract|sha256:'
ss -tan state established | awk '{print $5}' | sed 's/:.*//' | sort -u
du -sh /var/lib/docker
docker system df # 空间分布

几条经验

  1. WSL 不是裸 Linux。overlayfs、snapshotter、IPv6 NAT 的行为都跟标准发行版有差别,网上的教程不能照抄。
  2. 「下载看着成功但镜像不在」立刻查 daemon 日志,八成是 extract 阶段 NotFound,而不是网络。
  3. 大 layer(>100MB)更容易被加速站截断,遇到反复失败先换版本号或换源,而不是重试。
  4. 涉及网络的怪问题,先在 WSL 和 Windows 两侧用同一个命令对比,一次就能分出是 WSL 的问题还是源的问题。

这套配置跑通之后,日常用起来和普通 Linux 上的 Docker 没区别:compose 起服务、docker exec 进容器、端口映射到 Windows 都正常。