【问题标题】:Flask debug console with docker not working带有 docker 的 Flask 调试控制台不工作
【发布时间】:2021-09-12 16:09:02
【问题描述】:

我在 docker 中有这个烧瓶应用程序,调试模式设置为开启:

app_1  |  * Serving Flask app 'my_app' (lazy loading)
app_1  |  * Environment: development
app_1  |  * Debug mode: on
app_1  |  * Running on all addresses.
app_1  |    WARNING: This is a development server. Do not use it in a production deployment.
app_1  |  * Running on http://172.22.0.2:5000/ (Press CTRL+C to quit)
app_1  |  * Restarting with stat
app_1  |  * Debugger is active!
app_1  |  * Debugger PIN: 140-110-975

当我遇到错误并单击控制台小图标时,系统会询问我输入的 PIN 码,然后得到:[console ready]

但是,当我在控制台中输入内容时,我有:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

到网址:http://localhost:5000/submit?&__debugger__=yes&cmd=dump()&frm=140300814179632&s=ljASKJ6S2EwWtVcN8EHR

我认为可能是正确的端口未打开,因为它可能是用于 websockets(webpack 中的热重载),但在这里,端口似乎与 web 应用程序相同(5000)。

那么知道什么地方会出错吗?

谢谢

我正在使用:

Python 3.9.5
Flask 2.0.1

编辑

这里是docker-compose.yml

version: "3.3"

services:
  app:
    build: .
    command: flask run --host=0.0.0.0 --debugger
    volumes:
      - .:/app
    working_dir: /app
    ports:
      - 5000:5000
    env_file:
      - .env

【问题讨论】:

  • 请添加 docker run 命令(或 docker compose)。你是用端口映射还是别的什么?
  • 让flask监听所有IP地址:app.run(host='0.0.0.0').
  • @ofirule 我用我的 docker-compose.yml 文件进行了编辑。 anemyte,这已经通过 CLI 完成了。

标签: docker flask


【解决方案1】:

通过使用DEBUG=True,您是在告诉 Flask 在每次main.py 更改时重新加载服务器。这样做时,它每次都会调用main.py,杀死应用程序,然后在进程中的端口5000 中重新启动它。这是预期的行为。

您的问题与 docker 无关,更多的是在 Flask 应用程序中设置 prometheus。

有一些扩展可以帮助解决这个问题,例如:

https://github.com/sbarratt/flask-prometheushttps://github.com/hemajv/flask-prometheus 也许这也为解决方案提供了一些启示,但请注意,这不是您在这里要求的。

我建议首先尝试一下这些扩展,这意味着重构您的代码。从那里开始,如果在实施扩展时遇到问题,则创建另一个问题,提供mcve

编辑:

https://www.agiratech.com/debugging-python-flask-app-in-docker-container也有关于这个问题的文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 2012-06-08
    • 2021-02-17
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多