LoginSignup
3
5

More than 3 years have passed since last update.

Docker コンテナ内でmatplotlibなどを使った画像表示されない場合の対処法

Last updated at Posted at 2020-07-31

Docker コンテナ内でmatplotlib等で画像表示しようと思ったらこけたので記します
Ubuntu 16.04.6 LTS環境でやってます。

[問題点]
Docker run した後にコンテナに入り、matplotlibでplot.show()すると画像が何も表示されず、コマンドが終わる。
もしくは

docker _tkinter.TclError: couldn't connect to display
_tkinter.TclError: no display name and no $DISPLAY environment variable
_tkinter.TclError: couldn't connect to display :0.0
みたいなエラーが出てくる。

[対処法]
①まずターミナルに以下のコマンドをうつ

$sudo apt-get install x11-xserver-utils
$xhost +

②docker run する時には以下をオプションにつける
-v /tmp/.X11-unix:/tmp/.X11-unix
-e DISPLAY=unix$DISPLAY

③もしmatplotlibがコンテナに入ってなかったらコンテナ起動後のインテラクティブモードで

pip install matplotlib

④ もしtkinterがコンテナに入っていなかったらコンテナ起動後のインテラクティブモードで

apt-get update
apt-get install python3-tk

なおpython3-tkをインストールする際、居住地域を答えるくだりがありました。

これでうまくコンテナ内でも画像表示されるようになりました。:family:

3
5
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
3
5