1

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?

KZiovas
  • 3,491
  • 3
  • 26
  • 47
  • Can you debug your application in a simpler environment; for example, with the Flask dev server locally on your system, without Docker or GUnicorn; even if you're eventually going to deploy with those tools? – David Maze Jul 07 '22 at 10:36
  • Locally no, but what I am trying now is to have a second debug container where I run the app just with the flask server and I ll use that for debbuging. I am trying that at the moment. – KZiovas Jul 07 '22 at 11:09
  • For debugging, I might not use a container at all. Create a local virtual environment, install your application in it, and reproduce the issue there. – David Maze Jul 07 '22 at 11:14
  • yeah no I want to use the container, for various reasons (well basically all the reasons that make containers usefull, not having to install locally anything, avoiding environment management locally, not having to reconfigure service to look to local machine for infra services which also run in containers etc etc) – KZiovas Jul 07 '22 at 11:18

1 Answers1

1

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

  1. Create a degub_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 } ] }

KZiovas
  • 3,491
  • 3
  • 26
  • 47