[英]How to debug a dockerized Django app with PyCharm
I've a Django app and its dependencies running in docker containers (on Windows 10 with WSL2).我有一个 Django 应用程序及其在 docker 容器中运行的依赖项(在带有 WSL2 的 Windows 10 上)。 This is working fine but I'd like to be able to debug it through PyCharm Pro.
这工作正常,但我希望能够通过 PyCharm Pro 对其进行调试。 Here's my web container in the docker-compose
这是我在 docker-compose 中的 web 容器
# The main container - containing the Django code
web:
build: ./web
ports:
- "8000:8000"
depends_on:
- postgres #wait for postgres to be started, not for ready
- redis
- npm_watch
volumes:
- ./web:/usr/src/app
- ./web/collectstatic:/usr/src/app/collectstatic
env_file: .env
environment:
DEBUG: 'true'
LOCAL: 'true'
command: su myuser -c "/usr/local/bin/python manage.py runserver 0.0.0.0:8000"
I configured a Docker Compose interpreter targeting the Django app container and created a Django Server Run/Debug configuration see below (up command option is to have all containers started)我配置了一个Docker Compose 解释器,针对 Django 应用程序容器,并创建了一个Django 服务器运行/调试配置见下文(向上命令选项是启动所有容器)
When starting the docker-compose, the web container exits almost immediately with exit code 0. After manually restarting that node, it stays up but visiting the homepage only display a connection error.启动 docker-compose 时,web 容器几乎立即退出,退出代码为 0。手动重新启动该节点后,它保持运行,但访问主页仅显示连接错误。
When connecting to that container I can see the following process running, but only the port 53240 was listened to.连接到该容器时,我可以看到以下进程正在运行,但只监听了端口 53240。 Container is not listening on port 8000
python -u /opt/.pycharm_helpers/pydev/pydevd.py --port 53240 --file /opt/project/web/myproject/settings.py runserver --noreload 0.0.0.0:8000
容器未监听端口 8000
python -u /opt/.pycharm_helpers/pydev/pydevd.py --port 53240 --file /opt/project/web/myproject/settings.py runserver --noreload 0.0.0.0:8000
I also tried to add a Docker configuration but it seems that you cannot debug it (debug bottom panel logs nothing)我还尝试添加 Docker 配置,但似乎您无法调试它(调试底部面板不记录任何内容)
Any idea how to make this work?知道如何进行这项工作吗? I couldn't find a lot examples for this kind of setup.
我找不到很多这种设置的例子。
Or is it impossible this way and I need to either use remote debugging with PyDevd or run de Django app outside the docker env?或者这种方式不可能,我需要使用PyDevd 进行远程调试,或者在 docker 环境之外运行 de Django 应用程序? That would be a bummer... Would it work on other IDEs like Visual Studio Code?
那将是一个无赖......它可以在其他 IDE 上工作,比如 Visual Studio Code 吗?
Thanks:-)谢谢:-)
It looks like the unusual project structure screwed with PyCharm.看起来像 PyCharm 的不寻常项目结构。 My project contains docker configuration, one of which contains the Django app:
我的项目包含 docker 配置,其中一个包含 Django 应用程序:
Pydevd is running its own command to start the server (after --file) and ignores the command defined in docker-compose. Pydevd 正在运行自己的命令来启动服务器(在 --file 之后)并忽略 docker-compose 中定义的命令。 It appears that it relies on the way the Django framework is configured in settings.
它似乎依赖于 Django 框架在设置中的配置方式。 The
Manage Script
property was weirdly set to settings.py
instead of C:/Users/Martin/workspace/myproject/web/manage.py
. Manage Script
属性被奇怪地设置为settings.py
而不是C:/Users/Martin/workspace/myproject/web/manage.py
。 This helped pydevd know it should use manage.py instead of settings.py这帮助 pydevd 知道它应该使用 manage.py 而不是 settings.py
I also had to specify the environment variable DJANGO_SETTINGS_MODULE
in the Django Server configuration, with value myapp.settings
instead of settings
我还必须在 Django 服务器配置中指定环境变量
DJANGO_SETTINGS_MODULE
,其值为myapp.settings
而不是settings
I set the Working directory
to C:\Users\Martin\workspace\myproject\web
in Django Server configuration.我在 Django 服务器配置中将
Working directory
设置为C:\Users\Martin\workspace\myproject\web
。 It looks like it was using C:\Users\Martin\workspace\mypoject\web\myapp
.看起来它正在使用
C:\Users\Martin\workspace\mypoject\web\myapp
。 When changing this, the default folder when connecting with docker-compose exec web bash
changed accordingly更改此设置时,与
docker-compose exec web bash
连接时的默认文件夹相应更改
After those changes it worked like a... charm:).在这些变化之后,它就像一个......魅力:)。 Here's the command that is used when starting the container
这是启动容器时使用的命令
python -u /opt/.pycharm_helpers/pydev/pydevd.py --multiprocess --qt-support=auto --port 56563 --file /opt/project/web/manage.py runserver 0.0.0.0:8000
I also marked the web folder as source for PyCharm but I'm unsure that helped我还将 web 文件夹标记为 PyCharm 的源,但我不确定是否有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.