前回の投稿でWSL2を動作させることはできました。今回は一番やりたかったWSL2上でdockerを動かすことを試してみます。
Windows Subsystem for Linux 2 (WSL2)
結論から言うと、普通のLinuxのようにはまだ単独で動作しないです。
- プレビュー版のDocker DesktopをWindows10上で稼働させる
- WSL2にdockerをインストールして、WSL2からWindows10上のdockerデーモンを参照させる
これでWSL2上からDockerが普通に使えるようになります。てっきりWSL2であれば本物に近いカーネルが動作するので、Linux版のDockerがすんなり動くかと思ってました。
WSL2内にDockerをインストールする
https://docs.docker.com/install/linux/docker-ce/ubuntu/
dockerのリポジトリを参照するようにしてから最新版をインストールする
> sudo apt-get remove docker docker-engine docker.io containerd runc
> sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
> curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
> sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
> sudo apt-get update
> sudo apt-get install docker-ce docker-ce-cli containerd.io
> docker --version
Docker version 19.03.5, build 633a0ea838
ところが、dockerを使おうとすると、エラーになってしまいます。dockerのデーモンが起動しないので、何もできないです。
> docker info
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
> sudo systemctl start docker
System has not been booted with systemd as init system (PID 1). Cannot operate.
> 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'.
https://www.tomoyan.net/windows/wsl
https://github.com/systemd/systemd/issues/8036
困り果ててたところ、以下に詳細がありました。どうも開発中みたいです。
5 Things to Try with Docker Desktop WSL 2 Tech Preview
https://www.docker.com/blog/5-things-docker-desktop-wsl2-tech-preview/
Docker Desktop WSL 2 backend
https://docs.docker.com/docker-for-windows/wsl-tech-preview/
Docker Desktop WSL 2 Tech Previewインストール
2019-11-24時点での最新版インストールを取得してWindows10上にインストールします。
https://download.docker.com/win/edge/36883/Docker Desktop Installer.exe
デフォルトのままインストールします。
起動すると、アップデーターが起動してマイナーバージョンアップを行います。
無事Windows10上でDockerが起動したら、タスクバーのdockerアイコンから右クリックメニューを出して、Settingsを選択する。下図のようにWSL2云々のオプションをONする。
もう一つ、WSL INTEGRATIONで稼働中のWSLを選択してオプションをONする。
ここまで準備ができたら、WSL2のコンソールを起動してコンテナダウンロードから実行までできるかを確認する。
> sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:4df8ca8a7e309c256d60d7971ea14c27672fc0d10c5f303856d7bc48f8cc17ff
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/
> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f144782b01de hello-world "/hello" 8 seconds ago Exited (0) 7 seconds ago affectionate_montalcini
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 10 months ago 1.84kB
ネイティブとはまだ差がありますが、使うときにはWindows10が稼働しているわけですし、特にひと手間必要なわけではないで、とりあえずOKかと思っています。あとはdocker-compose
なども問題なく動くかは別途機会を設けて確認してみることにします。
WSL(またはWSL2)を使うことによって、一般的な仮想化による。端末内のリソースが分断されることもないし、別途マシンを準備する必要もないです。メインのWindows10マシンへのリソースやコストを集中できるので、WSL2はどんどん発展していってほしいです。
WSLとDocker for Windowsの組み合わせ
この場合、Docker for Windowsはリリースされている最新版をインストールしてもOK。
settingsの以下のオプションをONする。
WSL側では.bashrc
に以下を追加してDockerのホストをTCP経由で参照するように変更すると、使えるようになる。
export DOCKER_HOST=tcp://localhost:2375