KZiovas
KZiovas

Reputation: 4779

How to debug a Flask app which runs with gunicorn in a container?

I have been looking for a way to start a python debugger so I can debug my flask app which is being executed with gunicorn inside a docker container and then connect to it with my VSCode from outside.

But I dont find any solutions. In fact someone here suggests that it is not possible at all?

Is there a way to debug my flask app executed by gunicorn?

Upvotes: 2

Views: 1739

Answers (1)

KZiovas
KZiovas

Reputation: 4779

So it looks like this is very difficult, if not impossible atm, to do with gunicorn. So what I did was

  1. Create a debug_app.py file in my project with :
from myapp.api import create_app


if __name__=="__main__":
    app = create_app()
    app.run('0.0.0.0', 8000, debug=False)
  1. I created a debug container which runs nothing on start it just waiting idle like this in my docker-compose file:
 api-debug:
        image: "myapp:latest"
        restart: on-failure:3
        environment:
        volumes:
          - ./:/usr/src/app
        depends_on:
          - rabbitmq
          - redis
          - mongo
        tty: true
        stdin_open: true
        command: tail -F anything
        ports:
          - 8000:8000
  1. Then using VSCode with the Remote Container pluggin i attached to that container. This starts a new VSCode window and shows you the files inside the container.

Note Since the VSCode is now connected to the container I had to re-install the Python extension (you can look this up but it is easy just go to pluggins and re-install to container)

  1. I created a launch.json inside the container to run the degub_app.py that I mentioned above like this:

{ "version": "0.2.0", "configurations": [ { "name": "Python: Debug API", "type": "python", "request": "launch", "program": "${workspaceFolder}my_path/debug_api.py", "console": "integratedTerminal", "justMyCode": false } ] }

Upvotes: 3

Related Questions