【WSL】UbuntuからWindows版Docker Desktopを使いたい
WSL の Ubuntu から Windows 版 Docker Desktop を使いたいーーー!
ということで設定していきます。
手順が公式ドキュメントにあるので、その通りにやってみます
Docker Engine 等をアンインストール
Ubuntu に Docker をインストールしている場合は、
Docker Desktop と競合するため、アンインストールします。
// インストールされているか確認
$ apt list --installed | grep "docker\|containerd.io"
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
containerd.io/jammy,now 1.6.22-1 amd64 [installed]
docker-buildx-plugin/jammy,now 0.11.2-1~ubuntu.22.04~jammy amd64 [installed]
docker-ce-cli/jammy,now 5:24.0.5-1~ubuntu.22.04~jammy amd64 [installed]
docker-ce-rootless-extras/jammy,now 5:24.0.5-1~ubuntu.22.04~jammy amd64 [installed,automatic]
docker-ce/jammy,now 5:24.0.5-1~ubuntu.22.04~jammy amd64 [installed]
docker-compose-plugin/jammy,now 2.20.2-1~ubuntu.22.04~jammy amd64 [installed]
// Dockerとその他いらないパッケージを削除
$ sudo apt-get remove --purge --auto-remove -y containerd.io docker-buildx-plugin docker-ce-cli docker-ce-rootless-extras docker-ce docker-compose-plugin
ker-ce-rootless-extras docker-ce docker-compose-plugin
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
containerd.io* docker-buildx-plugin* docker-ce* docker-ce-cli* docker-ce-rootless-extras* docker-compose-plugin*
0 upgraded, 0 newly installed, 6 to remove and 37 not upgraded.
1 not fully installed or removed.
After this operation, 414 MB disk space will be freed.
(Reading database ... 53749 files and directories currently installed.)
Removing docker-ce (5:24.0.5-1~ubuntu.22.04~jammy) ...
invoke-rc.d: could not determine current runlevel
* Docker already stopped - file /var/run/docker-ssd.pid not found.
Removing containerd.io (1.6.22-1) ...
Removing docker-buildx-plugin (0.11.2-1~ubuntu.22.04~jammy) ...
Removing docker-ce-cli (5:24.0.5-1~ubuntu.22.04~jammy) ...
Removing docker-ce-rootless-extras (5:24.0.5-1~ubuntu.22.04~jammy) ...
Removing docker-compose-plugin (2.20.2-1~ubuntu.22.04~jammy) ...
Processing triggers for man-db (2.10.2-1) ...
(Reading database ... 53515 files and directories currently installed.)
Purging configuration files for docker-ce (5:24.0.5-1~ubuntu.22.04~jammy) ...
Purging configuration files for containerd.io (1.6.22-1) ...
// 削除されたか確認
$ apt list --installed | grep "docker\|containerd.io"
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
↓ のようなエラーが出て削除できない場合は...
* Stopping Docker: docker start-stop-daemon: warning: failed to kill 2034: No such process
No process in pidfile '/var/run/docker-ssd.pid' found running; none killed.
PID ファイルが残っていて Docker が終了していないと判定されています。 いらないのでファイルを消して再度 apt-get remove を実施してください
$ cd /var/run
$ sudo rm -rf docker-ssd.pid
Docker Desktop で WSL 2 をオンにする
Windows 用 Docker Desktop をインストールします。
※すでにインストールされている方は不要
Docker Desktop を起動して WSL2 がオンになっているか確認します
Settings > Generalを確認し、赤枠内にチェックが入っていれば OK!
チェックが入っていない方は、チェックを入れて「Apply & restart」をクリックしてください
ディストリビューションが WSL 2 か確認する
PowerShell または コマンド プロンプト で実行してください!
// WSLモードの確認
> wsl.exe -l -v
NAME STATE VERSION
* Ubuntu Running 1
docker-desktop-data Running 2
docker-desktop Running 2
// UbuntuがWSL1なので、2に上げます
> wsl.exe --set-version Ubuntu 2
WSL 2 との主な違いについては、https://aka.ms/wsl2
を参照してください
変換中です。これには数分かかる場合があります。
この操作を正しく終了しました。
// 確認
> wsl.exe -l -v
NAME STATE VERSION
* Ubuntu Running 2
docker-desktop-data Running 2
docker-desktop Running 2
Docker Desktop 側で設定する
Docker Desktop に戻って、Settings > Resources > WSL Integrationを開きます。
Ubuntu のトグルスイッチをオンにし、「Apply & restart」をクリックします。
Ubuntu から確認
WSL の Ubuntu を起動して docker コマンドが使用できるか確認します
// dockerコマンドが使えるか確認
$ docker --version
Docker version 20.10.21, build baeda1f
// docker-composeコマンドが使えるか確認
$ docker-compose --version
Docker Compose version v2.13.0
試しにコンテナを起動してみます
$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'
docker コマンドを実行するには権限が足りないので、ユーザーを docker グループにいれます
// docker グループにユーザーを追加
$ sudo usermod -aG docker <ユーザー名>
Ubuntu を立ち上げなおして、コンテナを起動してみます。
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
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 Desktop のほうにもコンテナが表示されていれば OK です!
後片付け
動作確認用に起動したコンテナやイメージを消しておきます
// コンテナを削除
// CONTAINER ID を確認
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
740f76f83d3d hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago agitated_mirzakhani
// 引数に CONTAINER ID を指定して、コンテナを削除
$ docker rm -f 740f76f83d3d
740f76f83d3d
// 削除確認
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
// イメージを削除
// IMAGE ID を確認
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 9c7a54a9a43c 3 months ago 13.3kB
// 引数に IMAGE ID を指定して、イメージを削除
$ docker rmi 9c7a54a9a43c
Untagged: hello-world:latest
Untagged: hello-world@sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Deleted: sha256:9c7a54a9a43cca047013b82af109fe963fde787f63f9e016fdc3384500c2823d
Deleted: sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1
// 削除確認
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
参考文献
Discussion