我在一个名为deep
的docker镜像中对Python进行了一定的设置。我用来运行Python代码如何在docker容器中使用matplotlib.pyplot?
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
,使我可以只运行容器外面的代码?
我使用--entry
参数试图
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
,但没有奏效。请帮忙。
你可以通过SSH连接着的显示 –
'-e'是环境变量,顺便说一句。你的命令不使用'--entrypoint' –