使用 docker-compose 的 VScode 调试器

问题描述 投票:0回答:2

最近我开始使用 docker 来运行我的项目,并使用以下堆栈

  • 姜戈
  • Nginx
  • 独角兽
  • 芹菜
  • Postgres
  • Redis

以前设置调试器很容易,但使用 docker-compose 之后我无法做到这一点。一旦我启动调试器,它就会加载一段时间,然后自动停止,任何地方都没有日志或错误。 这是相关的代码文件。

启动.json

{
"configurations": [
    {
        "name": "Python: Remote Attach",
        "type": "python",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 443
        },
        "pathMappings": [
            {
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "."
            }
        ]
    }
]
}

docker-compose.yml

version: '3'

services:
 db:
     image: postgres:12
     env_file: .env
     environment:
        - POSTGRES_DB=${DB_NAME}
        - POSTGRES_USER=${DB_USER}
        - POSTGRES_PASSWORD=${DB_PASSWORD}
    ports:
        - "5431:5432"
    volumes:
        - dbdata:/var/lib/postgresql/data
nginx:
    image: nginx:1.14
    ports:
        - "443:443"
        - "80:80"
    volumes:
        - ./config/nginx/:/etc/nginx/conf.d
        - ./myapp/static:/var/www/myapp.me/static/
web:
    restart: always
    build: ./myapp
    command:  bash -c "
                python manage.py collectstatic --noinput 
                && python manage.py makemigrations
                && python manage.py migrate
                && gunicorn --certfile=/etc/certs/localhost.crt --keyfile=/etc/certs/localhost.key myapp.wsgi:application --bind 0.0.0.0:443 --reload"
    expose:
        - "443"
    depends_on:
        - db
        - nginx
    env_file:
        - .env
    volumes:
        - ./myapp:/opt/myapp
        - ./config/nginx/certs/:/etc/certs
        - ./myapp/static:/var/www/myapp.me/static/

broker:
    image: redis:alpine
    expose: 
        - "6379"

celery:
    build: ./myapp
    command: celery -A myapp worker -l info
    env_file:
        - .env
    volumes:
        - ./myapp:/opt/myapp
    depends_on:
        - broker
        - db

celery-beat:
    build: ./myapp
    command: celery -A myapp beat -l info
    env_file:
        - .env
    volumes:
        - ./myapp:/opt/myapp
    depends_on:
        - broker
        - db

volumes:
    dbdata:
django docker visual-studio-code docker-compose vscode-debugger
2个回答
14
投票

最后我自己解决了。 从这个问题中得出的结论很少。

您需要使用 debugpy 并将其放入 manage.py 文件中才能开始侦听端口。 我做了这样的事情

import debugpy

debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()
debugpy.breakpoint()

除此之外,我们还需要将此端口映射到主机内的端口。为此,我们需要在 docker-compose 的 web 服务中更改并添加一行

ports: - "5678:5678"
我的 launch.json 看起来像这样

{ "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "0.0.0.0", "port": 5678 }, "pathMappings": [ { "localRoot": "${workspaceFolder}/myapp", "remoteRoot": "." } ] } ] }
注意:确保您的需求文件中有 debugpy 或手动安装。


3
投票
如果您将调试器设为模块,则可以使用环境变量打开和关闭它。我只是使用这篇文章作为 fastapi/gunicorn 调试解决方案的基础。

# debugger.py from os import getenv def initialize_flask_server_debugger_if_needed(): if getenv("DEBUGGER") == "True": import multiprocessing if multiprocessing.current_process().pid > 1: import debugpy debugpy.listen(("0.0.0.0", 10001)) print("⏳ VS Code debugger can now be attached, press F5 in VS Code ⏳", flush=True) debugpy.wait_for_client() print("� VS Code debugger attached, enjoy debugging �", flush=True)
请参阅 Adrian 的这篇文章:

VS Code 中的 Flask 调试

© www.soinside.com 2019 - 2024. All rights reserved.