kenichiro246’s blog

主にC#での仕事をしてます。今はAWSに関わり始めました。

Windows11 WSL2+Debian docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. の解決方法

事象

docker docs の手順に沿ってインストールしてたら、件名のエラーが発生したので、その解決方法となります。

matsuand.github.io

エラーメッセージはこんな感じ

$ sudo service docker start
grep: /etc/fstab: No such file or directory
Starting Docker: docker.

$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

環境

name ver
OS Windows 11 Pro (21H2)
Debian 11.3
docker-ce 20.10.17, build 100c701

解決方法

結論から言えば、私の環境では以下を実行すると解決した。

$ sudo touch /etc/fstab
$ sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
$ sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
$ sudo service docker start

※参考:WSL2 の debian で docker サービスが起動しない

なぜ?

$ sudo update-alternatives --config iptables
There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

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

Press <enter> to keep the current choice[*], or type selection number:

$ ls -l /usr/sbin/iptables
lrwxrwxrwx 1 root root 26 Jun 12 01:02 /usr/sbin/iptables -> /etc/alternatives/iptables
$ ls -l /etc/alternatives/iptables
lrwxrwxrwx 1 root root 22 Jun 12 01:02 /etc/alternatives/iptables -> /usr/sbin/iptables-nft
  • Dockerはiptablesを推奨?
    参考)Docker and iptables

  • iptables-nftで定義を設定すると,iptables-legacyの定義は無視(?)され、外部との通信ができない。そのため、iptables-legacyをデフォルトに変更し,定義をlegacy側に寄せて対処する。

※参考)Debian 10(buster) + ufw + Docker: Dockerコンテナが外部と通信できない問題の対処

手順

解決方法のところで書いた内容の詳細です。(備忘のため)

$ sudo service docker start
grep: /etc/fstab: No such file or directory
Starting Docker: docker.

$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

$ sudo touch /etc/fstab

$ sudo service docker start
Starting Docker: docker.

$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.


$ sudo update-alternatives --config iptables
There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

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

Press <enter> to keep the current choice[*], or type selection number:

$ sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives: using /usr/sbin/iptables-legacy to provide /usr/sbin/iptables (iptables) in manual mode

$ sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
update-alternatives: using /usr/sbin/ip6tables-legacy to provide /usr/sbin/ip6tables (ip6tables) in manual mode

$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

$ sudo service docker start
Starting Docker: docker.

$ sudo update-alternatives --config iptables
There are 2 choices for the alternative iptables (providing /usr/sbin/iptables).

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

Press <enter> to keep the current choice[*], or type selection number:

$ sudo docker run hello-world
:
Hello from Docker!
This message shows that your installation appears to be working correctly.
:

まぁ、慣れてない者には難しい話です・・・。