如何在Docker容器中使用matplotlib.pyplot? [英] How can I use matplotlib.pyplot in a docker container?

查看:335
本文介绍了如何在Docker容器中使用matplotlib.pyplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a certain setting of Python in an docker image named deep. I used to run python code

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

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

For information, -v and -w options are to link a local file in the current path to the container.

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

However, I can't use matplotlib.pyplot. Let's say test.py is

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

通过解决方案搜索,我只有一个解决方案

With solution search, I am having only one solution. I figured out I can do if

$ 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 ,这样我就可以在容器外部运行代码

My question is, instead of all those launching bash, setting xauth, and running Python inside container, can I do such setting with docker run so that I can just run the code outside of the container?

我尝试过

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 参数,但是它没有用。请帮忙。

using --entry parameter, but it didn't work. Please help.

推荐答案

有趣的是,我在ROS社区中找到了相当不错且彻底的解决方案。 http://wiki.ros.org/docker/Tutorials/GUI

Interestingly, I found quite nice and thorough solutions in ROS community. http://wiki.ros.org/docker/Tutorials/GUI

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

For my problem, my final choice is the second way in the tutorial:

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

这篇关于如何在Docker容器中使用matplotlib.pyplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆