Skip to content

Instantly share code, notes, and snippets.

@CandyMi
Last active March 15, 2024 10:17
Show Gist options
  • Save CandyMi/c1ed7d67468dc019896ee7d41d81fc8f to your computer and use it in GitHub Desktop.
Save CandyMi/c1ed7d67468dc019896ee7d41d81fc8f to your computer and use it in GitHub Desktop.
在 WSL2 中安装 Docker

在 WSL2 中安装 Docker

安装

使用 WSL2 时,可以按照官方说明安装 Docker, 安装 Docker 后只需运行 sudo service docker start 启动 Docker

问题

但是你使用 Debian 12WSL2 中安装 Docker 时,会发现运行 sudo service docker start 命令后 Docker 依旧无法启动。

排查

可是启动容器并无报错,并且 sudo service docker status 只会告诉你 Docker 没有运行。

奇怪的是在命令行没有输出任何错误信息, 那么就只能进行日志排查! 我们知道Docker的日志存放路径在/var/log/docker.log, 因此我们重点排查这个文件.

但是因为日志文件输出信息太多了, 所以可以利用一些命令(比如: grep)过滤关键词. 这样可以加快日志的筛选速度. (如果是自家产品的服务器, 你可以让SA协助排查).

Docker 的日志是有很明确的格式的, 但是这里我在筛选时发现有一段日志信息很“突兀”. 为什么用这个词呢? 就是说它不像是打印的错误,而像是运行时出错了:

failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to register "bridge" driver: 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.9 (nf_tables):  RULE_APPEND failed (No such file or directory): rule in chain DOCKER-ISOLATION-STAGE-1  ... )
time="2024-03-15T17:21:43.338233200+08:00" level=warning msg="failed to load plugin io.containerd.snapshotter.v1.devmapper" error="devmapper not configured"
time="2024-03-15T17:21:43.339442500+08:00" level=error msg="failed to initialize a tracing processor \"otlp\"" error="no OpenTelemetry endpoint: skip plugin"
...
..
.

经过我多方咨询发现 Debian 12 及其相似的发行版已经默认使用 iptables-nft, 而这个提示显然意味着我们的新版本命令与它不兼容. 所以问题很可能就在此.

解决

然后我尝试切换到 iptables-legacy, 这需要执行下列2步操作:

  • 运行 sudo update-alternatives --config iptables

  • 输入 1 选择 iptables-legacy

再次运行 sudo service docker start! 正常启动 , 收工!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment