2

第一次使用 Docker(版本 19.03.5)并尝试本教程
我被困在步骤 2.3.4 运行图像
当我去http://localhost:8888我看到
Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

我更新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 --name flask-app 11111111/flask-app
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

当我访问提示时看到的页面时
172.17.0.1 - - [05/Jan/2020 07:14:34] "GET / HTTP/1.1" 500 -



我的 app.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,这可能会发生冲突,但不确定是否是这种情况和/或如何修复它。

4

3 回答 3

1

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

  1. ENV FLASK_DEBUG=1通过添加到 Dockerfile 来打开 Flask 调试器
  2. 尝试在 Docker 之外运行 Flask 应用程序。在 IDE(VSCode 或 PyCharm)中设置断点并调试应用程序可能更容易。
  3. 尝试pdb调试容器内的应用程序。对于初学者来说可能很难,但总的来说,这是一项基本技能。检查在 Docker 容器中调试 Python Flask 应用程序以获取分步指南。
于 2020-02-01T21:06:41.650 回答
1

Flask 可能无法找到您的模板。尝试改变

COPY templates/index.html templates

COPY templates templates

将里面的所有内容复制./templates<WORKDIR>/templates.

使用COPY templates/index.html templates会将 index.html 作为文件复制到 path <WORKDIR>/templates,而不是将其复制到该目录下。

于 2020-02-02T15:23:33.810 回答
0

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

首先要做的是在没有应用程序的情况下启动容器。为此,您将 替换CMD ["python", "app.py"]CMD ["sleep", "inf"]。现在,启动容器后,您可以在容器中获取一个shell,使用docker exec -it flask-app /bin/bash获取一个shell。在 shell 中,您可以使用常规 Python 调试器在/处理程序中设置断点,然后单步执行代码以跟踪 Python 正在做什么。

于 2020-02-01T19:43:03.517 回答