💡

[WSL2]"Docker Desktop for Window"なしでdockerを動かしてみる

2022/06/22に公開

備忘録に

環境

  • OS: windows 10 Pro Version 21H1 (OS build 1903.1766)
  • WSL2: ubuntu 22.04 LTS
  • Docker Desktop: なし

手順

$ sudo apt update && sudo apt upgrade

docker公式が配布しているスクリプトからdockerをインストール: 公式手順

$ curl -fsSL https://get.docker.com/ | sh

statusを確認

$ service docker status
 * Docker is not running

起動できない

$ sudo service docker start
$ service docker status
 * Docker is not running

logを見ると以下のメッセージが

$ tail /var/log/docker.log
略
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))

この事象はWSLのgithub issueにも起票されていた(2022/6/22時点 openのまま)
Adding nftables sets doesn't work on WSL 2 · Issue #6044 · microsoft/WSL
issueコメントにあるようにiptables-legacyを使用することで解決できるらしい

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

無事起動

$ sudo service docker start
$ service docker status
 * Docker is running

動作確認

$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:13e367d31ae85359f42d637adf6da428f76d75dc9afeb3c21faea0d976f5c651
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Discussion