【问题标题】:VScode debugger with docker-compose带有 docker-compose 的 VScode 调试器
【发布时间】:2021-01-25 07:37:18
【问题描述】:

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

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

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

launch.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


    【解决方案1】:

    最后我自己解决了。 从这个问题中得到的几点启示。

    您需要使用 debugpy 并将其放在您的 ma​​nage.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}/censr",
                    "remoteRoot": "."
                }
            ]
        }
    ]
    }
    

    注意:确保您的需求文件中有 debugpy 或手动安装。

    【讨论】:

      【解决方案2】:

      如果您将调试器设为模块,则可以使用环境变量打开和关闭它。我只是将这篇文章作为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 的这篇文章:Flask Debugging in VS Code

      【讨论】:

      • 谢谢。似乎很有帮助。将尝试在我的项目中实现这一点。
      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2018-09-03
      • 2019-10-28
      相关资源
      最近更新 更多