2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【WSL2】WSL2のdocker 環境構築(何故か必要だった)

Last updated at Posted at 2023-02-25

はじめに

自分のPCでWSL2を使っていますが、docker が使えずに設定をしたのでメモしておきます。これまでずっとWSL2で開発してきて、苦労した記憶がないですが、忘れてしまっただけなのかな。(ぼけ老人)以前こんなことをしたのか分からないのですが。まぁ、メモです。

内容

docker service がない?

自分のWSL2でdocker が使えなかったので、入れました。/usr/bin/docker があるのに service docker start では登録がなく起動できませんでした。良く分からないので、インストールしました。docker 公式サイトにあるubuntu へのインストール手順に従いました。
メモ。

$ sudo apt-get update
$ sudo apt-get install     ca-certificates     curl     gnupg     lsb-release
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
$ echo   "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo chmod a+r /etc/apt/keyrings/docker.gpg
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
$ sudo service docker start
$ docker run hello-world

これで使えるようになりました。

docker build の apt-get で名前解決ができない

いざ、Dockerfile を書いてbuild しますが、apt-get ではまります。涙

 => ERROR [2/8] RUN apt-get update && apt-get install -y curl                                                                  19.7s
------
 > [2/8] RUN apt-get update && apt-get install -y curl:
#0 7.025 Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
#0 7.025   Temporary failure resolving 'archive.ubuntu.com'
#0 7.025 Err:2 http://security.ubuntu.com/ubuntu focal-security InRelease
#0 7.025   Temporary failure resolving 'security.ubuntu.com'
#0 13.28 Err:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
#0 13.28   Temporary failure resolving 'archive.ubuntu.com'
#0 19.51 Err:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
#0 19.51   Temporary failure resolving 'archive.ubuntu.com'
#0 19.52 Reading package lists...

そこで、docker さんがDNSだめなのかも、ということで、下記を実行するとNGだった。

$ docker run busybox nslookup google.com
;; connection timed out; no servers could be reached

下記を参考にしています。

で、docker が動いている WSL2内で名前解決ができていない、といういつもの /etc/resolv.conf の問題のようなので、nameserver 8.8.8.8 を追加したら、動くようになりました。

$ docker run busybox nslookup google.com
Server:         8.8.8.8
Address:        8.8.8.8:53

Non-authoritative answer:
Name:   google.com
Address: 142.250.196.142

Non-authoritative answer:
Name:   google.com
Address: 2404:6800:4004:821::200e

WSLの時刻がおかしい

それでもapt-get が通らず。WSLの時刻がおかしかったようです。
下記のコマンドののち、apt-get は動くようになりました。

# sudo hwclock --hctosys

まとめ

docker が入っていない、DNSがおかしい、時計がおかしい、ということだったのですが、いきなりこの三重苦があり、消耗しました。解決したのでよいですが、メモに残しておきます。
(2023/02/25)

2
4
0

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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?