简体   繁体   English

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

[英]How to display output in a simple dockerized Python application?

I just started using docker and am still struggling a bit in it's usage. 我刚刚开始使用docker,我仍然在使用中苦苦挣扎。 I have a python simple python application that uses Matplotlib to generate some simple graphs. 我有一个python简单的python应用程序,它使用Matplotlib生成一些简单的图形。 I currently have the following Dockerfile: 我目前有以下Dockerfile:

FROM python:2

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

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

This however generates the following error: 但是,这会产生以下错误:

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

What would be the best way to solve this? 解决这个问题的最佳方法是什么? Is it even possible with my approach? 我的方法甚至可能吗?

Could you test this? 你能测试一下吗?

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

It worked for me in a similar project. 它在类似的项目中对我有用。 Idea from here 来自这里的想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM