如何在简单的dockerized Python应用程序中显示输出?

时间:2017-05-09 08:47:57

标签: python matplotlib docker

我刚刚开始使用docker,我仍在苦苦挣扎。我有一个python简单的python应用程序,它使用Matplotlib生成一些简单的图形。我目前有以下Dockerfile:

FROM python:2

COPY requirements.txt /    
RUN pip install --requirement requirements.txt   
COPY my_code.py /

CMD [ "python", "./my_code.py" ]

然而,这会产生以下错误:

Traceback (most recent call last):
  File "./fraud_detection_sample_code.py", line 161, in <module>
    plt.figure(1)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1820, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

解决这个问题的最佳方法是什么?我的方法甚至可能吗?

1 个答案:

答案 0 :(得分:1)

你能测试一下吗?

docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix your_image_name

在类似的项目中,它对我有用。来自here

的想法