我在一個名爲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' –