简体   繁体   English

在开发容器中进行 VS Code Python Django 调试

[英]VS Code Python Django debugging in a dev container

I am developing a Python Django app in a Dockerized container.我正在 Dockerized 容器中开发 Python Django 应用程序。 I have successfully setup remote debugging to attach to my Django server inside of a container.我已成功设置远程调试以连接到容器内的 Django 服务器。 Me configuration is as follows.我的配置如下。

launch.json启动文件

{
  "name": "Remote Django App",
  "type": "python",
  "request": "attach",
  "pathMappings": [
    {
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/app"
    }
  ],
  "port": 9001,
  "host": "localhost"
}

manage.py管理文件

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'llf_api.settings')
    try:
        from django.core.management import execute_from_command_line
        from django.conf import settings

        if settings.DEBUG:
          if os.environ.get('RUN_MAIN') or os.environ.get('WERKZEUG_RUN_MAIN'):
              import ptvsd
              ptvsd.enable_attach(address=('0.0.0.0', 8001))
              print("Attached remote debugger")

    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__':
    main()

docker-compose.yml docker-compose.yml

services:
  api:
    image: ${DOCKER_IMAGE_BASE}:${DOCKER_TAG}
    build:
      context: .
      dockerfile: ./Dockerfile.development
    env_file:
      - .env
    environment:
      - DATABASE_URL=postgres://username:password@db/db_name
    volumes:
      - .:/app
    command: >
      bash -c "wait-for-it --service db:5432
      && python3 manage.py runserver 0.0.0.0:8000"
    ports:
      - "9000:8000"
      - "9001:8001"
    depends_on:
      - db
    tty: true
    stdin_open: true

The problem is that I run VS Code inside of a dev container (the Dockerfile.development above).问题是我在开发容器(上面的Dockerfile.development )中运行 VS Code。 So VS Code is essentially running within the same container the Django server is running which would make me think I need to attach to the local port (8001) the ptvsd is running on by setting my launch.json to the following:所以 VS Code 本质上是在 Django 服务器运行的同一个容器中运行的,这让我觉得我需要通过将我的launch.json设置为以下内容来附加到运行ptvsd的本地端口(8001):

launch.json启动文件

{
  "name": "Local Django App",
  "type": "python",
  "request": "attach",
  "host": "api",
  "port": 8001
}

However this doesn't work.然而这行不通。 When I try to attach the debugger in VS Code it appears to eventually timeout.当我尝试在 VS Code 中附加调试器时,它似乎最终会超时。 Does anyone know how this could be accomplished?有谁知道如何做到这一点?

My understanding of how VS Code and my server were running was inherently wrong.我对 VS Code 和我的服务器如何运行的理解本质上是错误的。 The server and VS Code are running off of the same image but NOT the same containers.服务器和 VS Code 运行的是同一个镜像,但不是同一个容器。 The containers are running side by side and therefore the local networking is not available to either.容器并排运行,因此两者都无法使用本地网络。

To make this work I realized I needed the VS Code container to access the server's container via the debugging port opened on the host.为了完成这项工作,我意识到我需要 VS Code 容器通过主机上打开的调试端口访问服务器的容器。 The only way I know how to do that is by using docker.for.mac.localhost as the host.我知道如何做到这一点的唯一方法是使用docker.for.mac.localhost作为主机。 So all that needed to change from my original setup was the launch.json configuration.所以所有需要从我的原始设置中改变的是launch.json配置。

launch.json启动文件

{
  "name": "Remote Django App",
  "type": "python",
  "request": "attach",
  "pathMappings": [
    {
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/app"
    }
  ],
  "port": 9001,
  "host": "docker.for.mac.localhost"
}

VS Code now attaches to port 9001 which has been exposed on the host and connects to the host using docker.for.mac.localhost . VS Code 现在连接到已在主机上公开的端口 9001,并使用docker.for.mac.localhost连接到主机。 It works!有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM