1. 程式人生 > 其它 >WSL2-Debian 安裝 docker 後無法啟動的解決辦法

WSL2-Debian 安裝 docker 後無法啟動的解決辦法

昨天在一臺新的 Windows 11 中安裝了 WLS2,目的就是使用 docker。

Microsoft Store 中預設的 Debian 版本是 11,我在很久之前安裝的是 10,照著之前的經驗添加了北外映象的 docker-ce 源,安裝很順利。

使用過 WSL2 的人肯定知道,WSL2 中不能使用systemctl,需要使用service來啟動 docker 或其他服務。

於是我便啟動 docker:

sudo service docker start

此時竟無法啟動,檢視日誌:

$ cat /var/log/docker.log
...
Sep 13 20:47:37 xxx dockerd: failed to start daemon: Error initializing network controller: error
 obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N D
OCKER: iptables v1.4.21: can't initialize iptables table `nat': Table does not exist (do you need to ins
mod?)

可以看到是 iptables 的問題,此問題在其 github 倉庫中有人提出過#1105,幸運的是,這個問題正好能被簡單地解決。

首先,將iptablesiptables-legacy替換:

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy

然後,開啟 ipv4 的包轉發功能:

sudo sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf

最後,重啟 WSL2,下面的程式碼在管理員模式下的 powershell 中執行:

wsl --shutdown

此時再啟動 Debian,就能順利啟動 docker了。