如何在 docker 容器中使用 matplotlib.pyplot?

新手上路,请多包涵

我在名为 deep 的 docker 镜像中有特定的 Python 设置。我曾经运行 python 代码

docker run --rm -it -v "$PWD":/app -w /app deep python some-code.py

有关信息, -v-w 选项用于将当前路径中的本地文件链接到容器。

但是,我不能使用 matplotlib.pyplot 。假设 test.py

import matplotlib.pyplot as plt
plt.plot([1,2], [3,4])
plt.show()

我收到了这个错误。

 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3147, in plot
   ax = gca()
 File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 928, in gca
   return gcf().gca(**kwargs)
 File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 578, in gcf
   return figure()
 File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
 File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
   return new_figure_manager_given_figure(num, figure)
 File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
   window = Tk.Tk()
 File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1818, in __init__
   self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

通过解决方案搜索, 我只有一个解决方案。我想如果

$ xauth list
xxxx/unix:0 yyyy 5nsk3hd                                # copy this list
$ docker run --rm -it -v "$PWD":/app -w /app \
             --net=host -e DISPLAY \
             -v /tmp/.X11-unix:/tmp/.X11-unix \
             deep bash

inside-container$ xauth add xxxx/unix:0 yyyy 5nsk3hd    # paste the list
inside-container$ python test.py                        # now the plot works!!

我的问题是,而不是所有那些启动 bash ,设置 xauth ,并 在容器内 运行 Python, 我可以用 docker run 做这样的设置吗?在容器外运行代码

我试过了

docker run --rm -it -v "$PWD":/app -w /app \
           --net=host -e DISPLAY \
           -v /tmp/.X11-unix:/tmp/.X11-unix \
           -e "xauth add xxxx/unix:0 yyyy 5nsk3hd" \
           deep python test.py

使用 --entry 参数,但没有用。请帮忙。

原文由 YW P Kwon 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

有趣的是,我在 ROS 社区中找到了非常好的和彻底的解决方案。 http://wiki.ros.org/docker/Tutorials/GUI

对于我的问题,我最终的选择是教程中的第二种方式:

 docker run --rm -it \
   --user=$(id -u) \
   --env="DISPLAY" \
   --workdir=/app \
   --volume="$PWD":/app \
   --volume="/etc/group:/etc/group:ro" \
   --volume="/etc/passwd:/etc/passwd:ro" \
   --volume="/etc/shadow:/etc/shadow:ro" \
   --volume="/etc/sudoers.d:/etc/sudoers.d:ro" \
   --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
   deepaul python test.python

原文由 YW P Kwon 发布,翻译遵循 CC BY-SA 3.0 许可协议

我以自己的方式尝试过,效果很好。#To run “matplotlib” inside docker, you must install these inside containers:-# $ yum install python3 -y //(必须)

$ yum install python3-tkinter -y //(必须)

$ pip3 install matplotlib //(必须)

$ pip3 install pandas (如果需要)

$ pip3 install scikit-learn (如果需要)

现在,运行你的容器。

\( `docker run --rm -it --env=DISPLAY --workdir=/app -v="\)PWD:/app python3 model.py`

原文由 vivek kumar sahu 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题