LoginSignup
8
6

More than 3 years have passed since last update.

DockerコンテナのホストでGUIを起動する

Last updated at Posted at 2020-06-21

環境

Ubuntu20.04
Docker 19.03

1.ホストでの準備

下記のコマンドでX WindowサーバがContainer内からの接続を受け入れるように設定します。

xhost local:

参照

2.コンテナを起動

GUIを使えるようにオプションを設定しコンテナ起動する。
下記のコマンドを実行するとpython3.8.3が使えるコンテナをインタラプタモードで起動する。

  • コマンドのオプションの説明

    • -e DISPLAY=$DISPLAY : GUIを転送するX11サーバーのアドレスを設定
    • --net host : コンテナ上のX11クライアントがサーバーにアクセスできるよう、コンテナのネットワーク設定をホストと共有する
docker run -v $PWD:/working -e DISPLAY=$DISPLAY --net host --name python -it python:buster  /bin/bash

参照

3.GUIが使えるかの確認

コンテナのbash上で下記のコマンドを実行してGUIが使えるかを確認

1.xeyesを使う

apt update
apt install x11-apps
xeyes

2.matplotlibを使う

test.py
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
img=list([[2,3,4],[5,6,7]])
plt.imshow(img, cmap='Greys')
plt.show() #画像を表示させる

上記のコードをpythonで実行
参照

うまく表示されないときは?

nano ~/.bashrc

環境変数を追加

export QT_X11_NO_MITSHM=1

反映させる

source ~/.bashrc

参考

8
6
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
8
6