Flask debug console with docker not working

Viewed 446

I have this flask app in a docker, with debug mode set to on:

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

When I have a bug and I click the little console icon, I am queried the PIN, which I enter, and get: [console ready].

But then, when I type something in the console, I have:

Not Found

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

to the URL: http://localhost:5000/submit?&__debugger__=yes&cmd=dump()&frm=140300814179632&s=ljASKJ6S2EwWtVcN8EHR

I thought it could be that the correct port is not open, as it can be for websockets (hot reloading in webpack), but here, it seems like the port is the same (5000) as the web app.

So any idea what could go wrong?

Thanks

I'm using:

Python 3.9.5
Flask 2.0.1

EDIT

Here is 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
1 Answers

By using DEBUG=True you are telling Flask to reload the server each time main.py changes. In doing so, it calls main.py each time, killing the app and then restarting it in port 5000 in the process. That is expected behaviour.

Your problem has nothing to do with docker, and is more about setting up prometheus inside a Flask application.

There are some extensions that help with this, for instance:

https://github.com/sbarratt/flask-prometheus or https://github.com/hemajv/flask-prometheus Maybe this sheds some light on the solution as well, but please note that this is not what you are asking here.

I suggest first giving these extensions a shot, which means refactoring your code. From there and if there is a problem implementing the extensions, then create another question providing a mcve.

EDIT:

There is also a document about this problem at https://www.agiratech.com/debugging-python-flask-app-in-docker-container

Related