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


image-20220314143716315

昨天在一台新的 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了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM