无法使用 dockerized django 项目运行 vscode 的调试器



每次运行调试器时,都会发生很多事情,但并不是我所期望的。

我正在用docker-compose up运行一个项目

检查本地主机后端是否正常。它倒下了。有趣的是,容器正在运行,因为我与vscode的远程容器相连。

已安装debugpy库。

运行调试器的第一种方法是在调试控制台中显示这样的信息:

Attached!
System check identified some issues:
WARNINGS:
workflow.State.additional_values: (fields.W904) django.contrib.postgres.fields.JSONField is deprecated. Support for it (except in historical migrations) will be removed in Django 4.0.
HINT: Use django.db.models.JSONField instead.
Operations to perform:
Apply all migrations: accounts, auth, contenttypes, files, mambu, otp_totp, sessions, token_blacklist, workflow, zoho
Running migrations:
No migrations to apply.

然后它就倒下了。后端也出现故障。

第二次尝试:

Attached!
System check identified some issues:
WARNINGS:
workflow.State.additional_values: (fields.W904) django.contrib.postgres.fields.JSONField is deprecated. Support for it (except in historical migrations) will be removed in Django 4.0.
HINT: Use django.db.models.JSONField instead.
Zoho Configuration failed, check that you have all variables ZOHO_TOKEN_URL, ZOHO_REST_API_KEY, ZOHO_CURRENT_USER_EMAIL

它关闭了,但后端启动了-我可以登录等

第三次尝试以这样的错误connect ECONNREFUSED 127.0.0.1:5678结束。

有什么建议吗?

代码:

管理员.py

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def initialize_debugger():
import debugpy
debugpy.listen(("0.0.0.0", 5678))
debugpy.wait_for_client()
print('Attached!')
def main():
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == "__main__":
initialize_debugger()
main()

本地码头组合yml

version: "3.2"
services:
backend:
container_name: xxx
build:
context: ./backend
dockerfile: ../build/backend.Dockerfile
volumes:
- ./backend:/opt/app
command: ./run.sh
ports:
- "8000:8000"
- "5678:5678"
env_file:
- build/.env-local
links:
- db:db
- rabbit:rabbit
- memcached:memcached
celery:
container_name: xxx
restart: always
build:
dockerfile: ../build/backend.Dockerfile
context: ./backend
command: ./run_celery.sh
env_file:
- build/.env-local
working_dir: /opt/app/
volumes:
- ./backend/:/opt/app
links:
- db:db
- rabbit:rabbit
frontend:
container_name: xxx
build:
context: frontend
dockerfile: ../build/frontend.Dockerfile
environment:
- BROWSER=none
- CI=true
volumes:
- ./frontend/src/:/frontend/src
- ./frontend/public/:/frontend/public
nginx:
container_name: xxx
build:
dockerfile: build/nginx.Dockerfile
context: .
args:
REACT_APP_GOOGLE_ANALYTICS_TOKEN: $REACT_APP_GOOGLE_ANALYTICS_TOKEN
REACT_APP_PAGESENSE_LINK: $REACT_APP_PAGESENSE_LINK
REACT_APP_CHATBOT_TOKEN: $REACT_APP_CHATBOT_TOKEN
REACT_APP_SENTRY_DSN: $REACT_APP_SENTRY_DSN
REACT_APP_SENTRY_ENVIRONMENT: $REACT_APP_SENTRY_ENVIRONMENT
REACT_APP_SENTRY_TRACES_SAMPLE_RATE: $REACT_APP_SENTRY_TRACES_SAMPLE_RATE
REACT_APP_THIRD_PARTY_API_URL: $REACT_APP_THIRD_PARTY_API_URL
ports:
- "5000:80"
depends_on:
- backend
- frontend
env_file:
- build/.env-local
volumes:
- ./build/nginx/nginx.conf:/etc/nginx.conf
db:
container_name: xxx
image: postgres:12
ports:
- "5432:5432"
restart: on-failure
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
rabbit:
container_name: xxx
image: rabbitmq
ports:
- "5672:5672"
memcached:
container_name: xxx
image: memcached
ports:
- "11211:11211"
restart: always
flower:
image: mher/flower:0.9.5
environment:
- CELERY_BROKER_URL=amqp://xxx-rabbitmq//
- FLOWER_PORT=8888
ports:
- 8888:8888

和launch.json

{
"version": "0.2.0",
"configurations": [
{
"name": "CF: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}/backend",
"remoteRoot": "/opt/app/"
}
],
"django": true
}
]
}

调试不能按预期工作可能有很多原因。故障排除通常是合理的做法。从简单的事情开始,增加复杂性,直到找出哪一步没有按预期工作。我建议在添加VS代码复杂性之前,先使用pdb进行简单的调试会话。为了实现这一点,您只需要在想要调试的后端代码中添加一个breakpoint()。在您的docker-compose.yaml中,您希望将以下附加配置添加到backend服务中

services:
backend:
- tty: true
- stdin_open: true

在您的终端中,使用docker-compose up启动您的应用程序。打开第二个端子,用docker attach <project name>_backend连接到您的集装箱上。您通常应该在breakpoint被击中的位置得到一个提示pdb>

根据你的描述,以下是我要调查的要点。

调试安装

确保debugpy安装在Docker镜像中,而不是本地安装。

WSGI HTTP服务器

我认为您正在使用python manage.py runserver 0.0.0.0:8000来启动WSGI HTTP服务器。为了防止你使用类似gunicorn的东西,值得一提的是,你应该只使用一个工人。例如,如果使用gunicorn,则可以在命令行提供工人数量:gunicorn --workers=1 --timeout=1200 --bind 0.0.0.0:8000 your_application.wsgi:application

还要注意巨大的超时。您可能希望为WSGI HTTP服务器和Nginx设置一个高值。如果其中一个在调试时超时,则会收到502或504错误,具体取决于哪个先超时,并且调试会话将终止。

调试位置

我通常将导入debugpy的代码放在wsgi.py中,就在调用get_wsgi_application()之前

"""
WSGI config for {{ project_name }} project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/{{ docs_version }}/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{ project_name }}.settings')
import debugpy
debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()
print('Attached!')
application = get_wsgi_application()
Django不支持通过自己的进行调试

这就是我在2分钟内冲浪的发现

这可能会帮助你

最新更新