Docker无法在WSL2的Ubuntu2004启动的问题

上一级页面:index-wsl

前言

今天在更新了WSL2上的Ubuntu22.04版本,

在安装Docker后无法启动,

排查问题

正确配置了systemctl,wsl2开启systemctl命令简单方法

并且enable了docker的服务,

1
2
$ sudo systemctl enable docker
$ sudo systemctl start docker

重启后发现docker.service和docker.socket依旧无法启动

切换到root用户,查看Docker的日志

  • xe是排查问题时最常用的参数:
  • e 从结尾开始看
  • x 相关目录(如:问题相关的网址)
1
2
3
4
journalctl -xe | grep docker
#
journalctl -xeu docker.socket
journalctl -xeu docker.service

显示如下的错误

Pasted%20image%2020220705192216.png

1
2
3
4
5
INFO[2022-04-22T16:14:55.718999500+08:00] stopping event stream following graceful shutdown  error="<nil>" module=libcontainerd namespace=moby
INFO[2022-04-22T16:14:55.719787400+08:00] stopping event stream following graceful shutdown error="context canceled" module=libcontainerd namespace=plugins.moby
INFO[2022-04-22T16:14:55.719906400+08:00] stopping healthcheck following graceful shutdown module=libcontainerd
failed to start daemon: Error initializing network controller: error obtaining controller instance: unable to add return rule in DOCKER-ISOLATION-STAGE-1 chain: (iptables failed: iptables --wait -A DOCKER-ISOLATION-STAGE-1 -j RETURN: iptables v1.8.7 (nf_tables): RULE_APPEND failed (No such file or directory): rule in chain DOCKER-ISOLATION-STAGE-1
(exit status 4))

核心错误就是最后这一块

1
failed to start daemon: Error initializing network controller: error obtaining controller instance: unable to add return rule in DOCKER-ISOLATION-STAGE-1 chain:  (iptables failed: iptables --wait -A DOCKER-ISOLATION-STAGE-1 -j RETURN: iptables v1.8.7 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain DOCKER-ISOLATION-STAGE-1

找到关键词iptables failed nf_tables

查询原因,发现是由于新的ubuntu系统使用了iptables-nft,而WSL2不支持导致的。

可以通过以下命令进行修改

1
2
sudo update-alternatives --config iptables
sudo update-alternatives --config ip6tables

切换为/usr/sbin/iptables-legacy即可

1
2
3
4
5
6
7
8
9
10
There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/sbin/iptables-nft 20 auto mode
1 /usr/sbin/iptables-legacy 10 manual mode
2 /usr/sbin/iptables-nft 20 manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/sbin/iptables-legacy to provide /usr/sbin/iptables (iptables) in manual mode

或者直接使用命令

1
2
update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

修改完成后重启Docker服务即可。

详见:添加 nftables 集在 WSL 2 上不起作用 ·第6044期 ·微软/WSL (github.com)

参考、引用、致谢

(26条消息) Docker无法在WSL2的Ubuntu启动的问题_世宝宝的博客-CSDN博客_wsl2 无法启动docker

(26条消息) journalctl -xe命令(系统日志查询)的使用_chushiyunen的博客-CSDN博客_journalctlxe怎么解决