LoginSignup
16

More than 1 year has passed since last update.

WSL2 (Ubuntu 20.04) + docker が動作しなかったことと解決策

Last updated at Posted at 2021-09-09

非常に間抜けなことにこれに1日つぶしたので、ちゃんとブログに書いておく。
結論からすると非常に間抜けな結果なのだが調べている間はわからなかなったので、ソリューション含めて書いておく。

WSL2 のインストール

WSL2のインストールは、Microsoft Store から行える。Ubuntuを選ぶ。
この時点で間違いが発生している。

Docker のインストール

Docker のインストールの手順はInstall using the repository何も特殊なことはない。
ただ、このままだと、20.04 の場合は、docker の起動に失敗する。理由は、iptable の新しいバージョンがdocker とともにうまく動かない。であるので、次のようにして、古いバージョンを使う。

sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
$ sudo update-alternatives --config iptables
[sudo] password for ushio:
There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

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

しっかりと切り替わっている。dockerd を起動する

INFO[2021-09-08T16:25:31.245371400-07:00] [graphdriver] using prior storage driver: overlay2
WARN[2021-09-08T16:25:31.264203900-07:00] Your kernel does not support cgroup memory limit
WARN[2021-09-08T16:25:31.264788700-07:00] Unable to find cpu cgroup in mounts
WARN[2021-09-08T16:25:31.265711400-07:00] Unable to find blkio cgroup in mounts
WARN[2021-09-08T16:25:31.266660400-07:00] Unable to find cpuset cgroup in mounts
WARN[2021-09-08T16:25:31.267471900-07:00] Unable to find pids cgroup in mounts
INFO[2021-09-08T16:25:31.268758900-07:00] Loading containers: start.
WARN[2021-09-08T16:25:31.282965200-07:00] Running iptables --wait -t nat -L -n failed with message: `iptables v1.8.4 (legacy): can't initialize iptables table `nat': Table does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.`, error: exit status 3
INFO[2021-09-08T16:25:31.505445700-07:00] stopping event stream following graceful shutdown  error="<nil>" module=libcontainerd namespace=moby
INFO[2021-09-08T16:25:31.508975100-07:00] stopping healthcheck following graceful shutdown  module=libcontainerd
INFO[2021-09-08T16:25:31.508988100-07:00] stopping event stream following graceful shutdown  error="context canceled" module=libcontainerd namespace=plugins.moby
WARN[2021-09-08T16:25:32.514096600-07:00] grpc: addrConn.createTransport failed to connect to {unix:///var/run/docker/containerd/containerd.sock  <nil> 0 <nil>}. Err :connection error: desc = "transport: Error while dialing dial unix:///var/run/docker/containerd/containerd.sock: timeout". Reconnecting...  module=grpc
failed to start daemon: Error initializing network controller: error obtaining controller instance: failed to create NAT chain DOCKER: iptables failed: iptables -t nat -N DOCKER: iptables v1.8.4 (legacy): can't initialize iptables table `nat': Table does not exist (do you need to insmod?)
Perhaps iptables or your kernel needs to be upgraded.
 (exit status 3)

これが問題のあらまし。さてみなさんはどこが問題かおわかりだろうか?大変まぬけである。私は一日かけていろいろな問題を調査したが解決にいたらず途方に暮れていた。
同僚に助けを求めてみたら次の通りの回答だった。ちなみにほかの同僚は同じことをしてもちゃんと動くという。

解決編

最初に同僚がいってくれたのはuname -ar -a print all information そして -r print the kernel release

$ uname -ar
Linux DESKTOP-N96ION0 4.4.0-19041-Microsoft #1151-Microsoft Thu Jul 22 21:05:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux

ちなみに同僚のは

Linux 5.10.16.3-microsoft-standard-WSL2+

次に Powershell を開けて

wsl- l -v

ここで自分の愚かさに気づいたのだが、私のはWSL1だった。もうWSL2がでて相当経つのでデフォルトWSL2となぜか思い込んでいた。しかし、実際には、WSL2はデフォルトではなく、WSL2をデフォルトにする作業が必要である。

wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         1

あーもう。2にするぞ。

wsl --set-default-version 2
wsl --set-version Ubuntu 2
wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         2

なぜはまったか?

問題はなんでこんなくそしょうもないことで1日つぶしたかである。はっきり言ってこれを解くのになんの知識も必要ない「WSL2のインストール手順」だけの話だ。
ちなみにその間、iptable じゃなくて、bridge を使うようにしてみたり、いろんなログを調査したりとかめっちゃした。自分の脳みそどうなっとるんじゃと言いたくなる。
だって知識とちゃうから。自分の頭からWSL2はデフォルトではないが抜けていただけだから。

じゃあ、なんで解けなかったかというと、思い込みがあって、もっと難しい問題と思っていたことにあるから。多分何かをインストールするときはもう一度オフィシャルをさぐったほうがよさそうだ。自分が普段使い慣れてるやつでも。あと、同僚がやったみたいに、知らないことを調べようとするのではなくて、確実に一歩一歩進んでいるのかをコマンドで確認したほうがよさそうだ。さあ、今から仕事するか。5時だけど。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16