Python Docker:如何调试localhost“;内部服务器错误";?

Python Docker:如何调试localhost“;内部服务器错误";?,python,docker,flask,Python,Docker,Flask,首次使用Docker(版本19.03.5)并尝试此功能 我被困在运行图像的步骤2.3.4上 当我去看电影时,我看到了 内部服务器错误 服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。 我将Dockerfile更新到此文件以匹配我的目录: # our base image FROM alpine:3.5 # Install python and pip RUN apk add --update py2-pip # install Python modules needed by

首次使用Docker(版本19.03.5)并尝试此功能
我被困在运行图像的步骤2.3.4上
当我去看电影时,我看到了
内部服务器错误
服务器遇到内部错误,无法完成您的请求。服务器过载或应用程序出错。

我将
Dockerfile
更新到此文件以匹配我的目录:

# our base image
FROM alpine:3.5

# Install python and pip
RUN apk add --update py2-pip

# install Python modules needed by the Python app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# copy files required for the app to run
COPY app.py .
COPY templates/index.html templates

# tell the port number the container should expose
EXPOSE 5000

# run the application
CMD ["python", "app.py"]
在我的命令行中,我有
C:\Users\user\docker\flask app>docker run-p 8888:5000——名称flask app 11111111/flask app

*在上运行http://0.0.0.0:5000/ (按CTRL+C退出)


当我访问页面时,我会在提示符上看到
172.17.0.1---[05/Jan/2020 07:14:34]“GET/HTTP/1.1”500-



我的应用程序中有这个。py

from flask import Flask, render_template
import random

app = Flask(__name__)

# list of cat images
images = [
   "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26388-1381844103-11.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr01/15/9/anigif_enhanced-buzz-31540-1381844535-8.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26390-1381844163-18.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-1376-1381846217-0.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3391-1381844336-26.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/10/anigif_enhanced-buzz-29111-1381845968-0.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr03/15/9/anigif_enhanced-buzz-3409-1381844582-13.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr02/15/9/anigif_enhanced-buzz-19667-1381844937-10.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr05/15/9/anigif_enhanced-buzz-26358-1381845043-13.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-18774-1381844645-6.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr06/15/9/anigif_enhanced-buzz-25158-1381844793-0.gif",
    "http://img.buzzfeed.com/buzzfeed-static/static/2013-10/enhanced/webdr03/15/10/anigif_enhanced-buzz-11980-1381846269-1.gif"
    ]

@app.route('/')
def index():
    url = random.choice(images)
    return render_template('index.html', url=url)

if __name__ == "__main__":
    app.run(host="0.0.0.0")
我不明白为什么我的页面没有加载。任何帮助都将不胜感激

注意:我安装了WAMP,这可能有冲突,但不确定是否存在这种情况和/或如何修复它。

注意:这更多是针对“如何调试”的问题。尚不清楚OP是否真正想要一个解决方案,而不是解决问题的方法


要做的第一件事是启动容器,但容器中没有应用程序。为此,将
CMD[“python”,“app.py”]
替换为
CMD[“sleep”,“inf”]
。现在,在启动容器之后,您可以使用
docker exec-it flask app/bin/bash
在容器中获取shell。在shell中,您可以使用常规Python调试器在
/
处理程序中设置断点,然后单步通过代码跟踪Python正在做什么。

如何在Docker中调试Flask应用程序:

  • 通过将
    ENV Flask_DEBUG=1
    添加到Dockerfile来打开Flask调试器
  • 尝试在Docker外部运行Flask应用程序。在IDE(VSCode或PyCharm)内设置断点并调试应用程序可能更容易
  • 尝试
    pdb
    在容器内调试应用程序。这对初学者来说可能很难,但总的来说,这是一项基本技能。检查是否有分步指南

  • Flask可能找不到您的模板。试着改变

    COPY templates/index.html templates
    

    /templates
    中的所有内容复制到
    /templates


    使用
    COPY templates/index.html templates
    将index.html复制为路径
    /templates
    处的文件,而不是将其复制到该目录下。

    看起来您的烧瓶正在容器中运行,但当您访问(在本地主机:8888)时,它返回500错误。嗯,似乎错误是正确的-你的flask应用程序可能有错误。尝试将你的flask应用程序替换为一个你能看到运行的最小应用程序:如果运行正常,-问题出在你的应用程序中。如果没有,您需要修复烧瓶的docker容器app@BrendaJ.Butler我将app.py代码添加到我的postAt Docker级别,如果出现500个错误,那么一切都应该正常工作。应用程序日志应该有更多的细节(比如回溯)来解释哪里出了问题,你应该看看那里。@DavidMaze如何获得回溯?我一直在想如何在Docker中调试它,但没有成功。几个月前我确实在我的机器上安装了WAMP,所以可能会有冲突?请尝试将
    COPY templates/index.html templates
    更改为
    COPY templates
    。Flask可能找不到您的模板
    COPY templates templates