2020年7月6日月曜日

WSL2のubuntu20.04でdockerを動かす


windows10のwsl2 に docker環境を作ったときのメモ
wsl2 のOSは下記のとおり
ubuntu@MyComputer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04 LTS
Release:        20.04
Codename:       focal

下記URLを参照してdockerをインストールしました。

dockerを使おうとしたら、下記のようなエラーがでたので
docker: Error response from daemon: cgroups: cannot find cgroup mount destination: unknown.

いろいろ調べていたら、下記のページを見つけた。

見つけたページを参考にして、wsl2のubuntu にdaemonize パッケージをいれて
sudo apt install daemonize

下記のシェルを実行すると、systemctl が使用できるようになった。
ubuntu@MyComputer:~/docker/vpn$ cat ~/start_systemd.sh
# https://qiita.com/matarillo/items/f036a9561a4839275e5f
#

#
SYSTEMD_PID="$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')"
if [ -z "$SYSTEMD_PID" ]; then
  sudo daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
  sleep 1
fi

#
exec sudo nsenter --target $(pidof systemd) --all su - $LOGNAME

上記のシェル(start_systemd.sh)を実行する前のプロセスは下記のとおり。
ubuntu@MyComputer:~$ ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 09:56 ?        00:00:00 /init
root         6     1  0 09:56 ?        00:00:00 /init
root         7     6  0 09:56 ?        00:00:00 /init
ubuntu       8     7  0 09:56 pts/0    00:00:00 -bash

上記のシェル(start_systemd.sh)を実行した後のプロセスは下記のとおり。
ubuntu@MyComputer:~$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 10:06 ?        00:00:00 /lib/systemd/systemd --system-unit=basic.target
root          42       1  0 10:06 ?        00:00:00 /lib/systemd/systemd-journald
root          63       1  0 10:06 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+      67       1  0 10:06 ?        00:00:00 /lib/systemd/systemd-networkd
root         219       0  0 10:06 pts/0    00:00:00 su - ubuntu
ubuntu       220     219  0 10:06 pts/0    00:00:00 -bash
systemd+     239       1  0 10:06 ?        00:00:00 /lib/systemd/systemd-resolved
systemd+     240       1  0 10:06 ?        00:00:00 /lib/systemd/systemd-timesyncd
root         245       1  0 10:06 ?        00:00:00 /usr/lib/accountsservice/accounts-daemon
message+     246       1  0 10:06 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfil
root         252       1  0 10:06 ?        00:00:00 /usr/sbin/irqbalance --foreground
root         253       1  0 10:06 ?        00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
syslog       255       1  0 10:06 ?        00:00:00 /usr/sbin/rsyslogd -n -iNONE
root         256       1  1 10:06 ?        00:00:01 /usr/lib/snapd/snapd
root         260       1  0 10:06 ?        00:00:00 /lib/systemd/systemd-logind
root         267       1  0 10:06 ?        00:00:00 /usr/sbin/cron -f
root         281       1  0 10:06 ?        00:00:00 /usr/bin/containerd
daemon       282       1  0 10:06 ?        00:00:00 /usr/sbin/atd -f
root         283       1  0 10:06 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.so
root         293       1  0 10:06 tty1     00:00:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root         307       1  0 10:06 ?        00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root         319       1  0 10:06 ?        00:00:00 /usr/lib/policykit-1/polkitd --no-debug
root         320       1  0 10:06 ?        00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-s
ubuntu       583     220  0 10:08 pts/0    00:00:00 ps -ef

これで、docker は使えるようになったが
DNSの名前解決ができなくなったので、/etc/systemd/resolved.conf を修正
[Resolve]
DNS=8.8.8.8
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#DNSOverTLS=no
#Cache=yes
#DNSStubListener=yes
#ReadEtcHosts=yes

下記のコマンドで変更を反映したら、DNSの名前解決できるようになった。
systemctl restart systemd.resolved