【问题标题】:Unable to run vscode's debugger with dockerized django project无法使用 dockerized django 项目运行 vscode 的调试器
【发布时间】:2022-01-18 13:49:04
【问题描述】:

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

我正在使用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结束。

有什么建议吗?

代码:

manage.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()

本地 docker-compose.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
        }
    ]
}

【问题讨论】:

  • 是否在不包含 debugpy 的情况下启动?
  • @pygeek 是的,确实如此。

标签: python django docker visual-studio-code vscode-debugger


【解决方案1】:

Django 不支持自行调试

这就是我在 2 分钟内冲浪的源泉

this might help you

【讨论】:

    【解决方案2】:

    调试无法按预期工作的原因可能有很多。故障排除通常是合理的做法。从简单的事情开始并增加复杂性,直到弄清楚哪个步骤没有按预期工作。我建议先使用pdb 进行简单的调试会话,然后再添加 VS Code 复杂性。为了实现这一点,您只需在要调试的后端代码中添加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 之类的东西,值得一提的是,您应该只使用 1 个工人。例如,如果使用 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()
    

    【讨论】:

      猜你喜欢
      • 2017-04-23
      • 2021-11-27
      • 2020-08-31
      • 1970-01-01
      • 2018-05-24
      • 2018-11-06
      • 2022-11-04
      • 2019-12-16
      • 2022-07-06
      相关资源
      最近更新 更多