Pycharm 在 docker compose 中调试 manage.py 命令
Pycharm debugging manage.py commands in docker compose
我有一个非常简单的设置。我正在 运行ning Pycharm 2018.2.3 并使用 docker compose 启动 3 个容器。
- 我的 Django 应用程序
- NGINX 静态服务
- Postgres 数据库
我已经配置了用于调试容器的远程解释器,并且断点在大多数情况下工作得很好,至少当我点击我的 API 端点或对 django 应用程序进行一些其他操作时。
什么不起作用,是当我 运行 我的管理自定义 manage.py 自定义命令之一时。到目前为止,我已经尝试了这两种方法。
我在 PyCharm 中设置了另一个调试配置来执行命令。这导致另一个容器旋转起来(代替原来的容器。运行 命令,没有任何换行符中断。然后整个容器关闭。
我已经登录容器了,运行直接通过命令行执行manage.py命令,在容器中执行,同样没有断点。
该文档似乎在正常情况下有效,但我找不到任何有关在容器中调试这些命令的帮助。
感谢您的帮助或提示。
这可能取决于您的 docker-compose.yml
确切内容。
例如参见“An interactive debugger inside a running container!" of the article "A Simple Recipe for Django Development In Docker (Bonus: Testing with Selenium)" from Adam King.
部分
他的 docker-compose.ym
l 包括:
version: "2"
services:
django:
container_name: django_server
build:
context: .
dockerfile: Dockerfile
image: docker_tutorial_django
stdin_open: true
tty: true
volumes:
- .:/var/www/myproject
ports:
- "8000:8000"
其中,见:
stdin_open: true
tty: true
[Those 2 lines] are important, because they let us run an interactive terminal.
Hit ctrl-c to kill the server running in your terminal, and then bring it up in the background with docker-compose up -d
.
docker ps
tells us it’s still running:
We need to attach to that running container, in order to see its server output and pdb breakpoints.
The command docker attach django_server
will present you with a blank line, but if you refresh your web browser, you’ll see the server output.
Drop import pdb; pdb.set_trace()
in your code and you’ll get the interactive debugger, just like you’re used to.
只是一个更新,以防有人遇到类似问题。我个人的解决方案是不使用 manage.py 命令,而是通过 http 调用提供这些相同的命令。
我发现简单地拥有一个像 myserver 这样的端点更容易(而且通常更有用)。com/api/do-admin-function 并将其限制为管理访问。
当我在我的代码中放置一个断点时,即使是 运行 在容器中,它也会按预期正常中断并允许我按照我想要的方式进行调试
为了在 Docker 容器中调试 Django 命令,您可以使用以下设置创建一个新的 Run/Debug 配置
- 使用 Python 配置模板
- 脚本路径:
manage.py
的绝对位置
- 参数:你要debug/execute
的Django命令
!important!
Python 口译员:Docker Compose interpreter
我有一个非常简单的设置。我正在 运行ning Pycharm 2018.2.3 并使用 docker compose 启动 3 个容器。
- 我的 Django 应用程序
- NGINX 静态服务
- Postgres 数据库
我已经配置了用于调试容器的远程解释器,并且断点在大多数情况下工作得很好,至少当我点击我的 API 端点或对 django 应用程序进行一些其他操作时。
什么不起作用,是当我 运行 我的管理自定义 manage.py 自定义命令之一时。到目前为止,我已经尝试了这两种方法。
我在 PyCharm 中设置了另一个调试配置来执行命令。这导致另一个容器旋转起来(代替原来的容器。运行 命令,没有任何换行符中断。然后整个容器关闭。
我已经登录容器了,运行直接通过命令行执行manage.py命令,在容器中执行,同样没有断点。
该文档似乎在正常情况下有效,但我找不到任何有关在容器中调试这些命令的帮助。
感谢您的帮助或提示。
这可能取决于您的 docker-compose.yml
确切内容。
例如参见“An interactive debugger inside a running container!" of the article "A Simple Recipe for Django Development In Docker (Bonus: Testing with Selenium)" from Adam King.
部分他的 docker-compose.ym
l 包括:
version: "2"
services:
django:
container_name: django_server
build:
context: .
dockerfile: Dockerfile
image: docker_tutorial_django
stdin_open: true
tty: true
volumes:
- .:/var/www/myproject
ports:
- "8000:8000"
其中,见:
stdin_open: true
tty: true
[Those 2 lines] are important, because they let us run an interactive terminal.
Hit ctrl-c to kill the server running in your terminal, and then bring it up in the background withdocker-compose up -d
.
docker ps
tells us it’s still running:We need to attach to that running container, in order to see its server output and pdb breakpoints.
The commanddocker attach django_server
will present you with a blank line, but if you refresh your web browser, you’ll see the server output.
Dropimport pdb; pdb.set_trace()
in your code and you’ll get the interactive debugger, just like you’re used to.
只是一个更新,以防有人遇到类似问题。我个人的解决方案是不使用 manage.py 命令,而是通过 http 调用提供这些相同的命令。
我发现简单地拥有一个像 myserver 这样的端点更容易(而且通常更有用)。com/api/do-admin-function 并将其限制为管理访问。
当我在我的代码中放置一个断点时,即使是 运行 在容器中,它也会按预期正常中断并允许我按照我想要的方式进行调试
为了在 Docker 容器中调试 Django 命令,您可以使用以下设置创建一个新的 Run/Debug 配置
- 使用 Python 配置模板
- 脚本路径:
manage.py
的绝对位置
- 参数:你要debug/execute 的Django命令
!important!
Python 口译员:Docker Compose interpreter