背景と目的
Windows10 に Docker Desktop をインストールした環境で、 久しぶりに Python でグラフをプロットしようとしたら以下のエラーメッセージが表示された。
TclError: couldn't connect to display "docker.for.win.localhost:0.0"
そこで原因と対処方法を調べた。
結論
コンテナ起動時に設定する環境変数 DISPLAY
の値を以下の通りに変更する。
docker.for.win.localhost:0.0 ↓ host.docker.internal:0.0
原因
Docker Desktop 3.2.0 から docker.for.win.localhost
が非サポートとなっていたため。
以下にリリースノート[1] からの引用を示す。
Removed the deprecated DNS name
docker.for.win.localhost
. Use DNS namehost.docker.internal
in a container to access services that are running on the host. docker/for-win#10619
その他
環境変数 DISPLAY
を docker.for.win.localhost
に設定していた理由は、筆者が使用している X server である X410 のドキュメント[2]に記載があったからである。
以下に公式ドキュメントからの引用を添付する。
X410 can seamlessly be used with Docker for Windows. You don't need to enable any special option in X410 or Windows to have X-Window GUI apps running in Docker containers show up in Windows; you simply need to set the DISPLAY environment variable with a special address, i.e., docker.for.win.localhost, before launching your X-Window GUI apps:
余談ですが、下の方に host.docker.internal:0.0
が使える旨が書いてありましたね。全然気づいていませんでした…。
参考資料
[1] Docker Documentation, "Docker for Windows release notes", https://docs.docker.com/docker-for-windows/release-notes/#docker-desktop-320 (2021/04/04 に確認)
[2] X410, "Cookbook", https://x410.dev/cookbook/ (2021/04/04 に確認)