简体   繁体   English

如何调试在容器中使用 gunicorn 运行的 Flask 应用程序?

[英]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.我一直在寻找一种方法来启动 python 调试器,这样我就可以调试我的烧瓶应用程序,该应用程序在 docker 容器内使用 gunicorn 执行,然后从外部使用我的 VSCode 连接到它。

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?有没有办法调试由 gunicorn 执行的烧瓶应用程序?

So it looks like this is very difficult, if not impossible atm, to do with gunicorn.所以看起来这对于gunicorn来说是非常困难的,如果不是不可能的话。 So what I did was所以我所做的是

  1. Create a degub_app.py file in my project with :在我的项目中创建一个degub_app.py文件:
 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:我创建了一个调试容器,它在启动时什么都不运行,只是在我的docker-compose文件中像这样等待空闲:
 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.然后将VSCode与我附加到该容器的Remote Container插件一起使用。 This starts a new VSCode window and shows you the files inside the container.这将启动一个新的 VSCode 窗口并显示容器内的文件。

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)注意由于 VSCode 现在已连接到容器,因此我必须重新安装 Python 扩展(您可以查看它,但很容易只需转到插件并重新安装到容器)

  1. I created a launch.json inside the container to run the degub_app.py that I mentioned above like this:我在容器内创建了一个launch.json来运行我上面提到的degub_app.py ,如下所示:

{ "version": "0.2.0", "configurations": [ { "name": "Python: Debug API", "type": "python", "request": "launch", "program": "${workspaceFolder}my_path/debug_api.py", "console": "integratedTerminal", "justMyCode": false } ] } { “版本”:“0.2.0”,“配置”:[ { “名称”:“Python:调试 API”,“类型”:“python”,“请求”:“启动”,“程序”:“$ {workspaceFolder}my_path/debug_api.py", "console": "integratedTerminal", "justMyCode": false } ] }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM