在 VS Code 中调试 dockerized Django 导致错误 "Timed out waiting for launcher to connect"

Debugging dockerized Django in VS Code results in error "Timed out waiting for launcher to connect"

我想在 Visual Studio 代码中使用 docker 容器调试我的 Django 应用程序。

Microsoft 发布了如何执行此操作的指南,我逐步遵循了该指南: https://code.visualstudio.com/docs/containers/quickstart-python

但是当我尝试 运行 调试器时,我收到以下错误消息:

Timed out waiting for launcher to connect

这是我一步一步做的:

  1. 我使用 django-admin startproject helloworld
  2. 初始化了一个简单的 Django 应用程序
  3. 在 VS Code 中,我打开了包含 manage.py
  4. 的文件夹
  5. 打开命令面板 Ctrl + Shift + P,然后选择 Docker: Add Docker Files to Workspace...
  6. Select应用平台Python: Django
  7. 包括 Docker 撰写文件 No
  8. 应用入口点的相对路径manage.py
  9. 您的应用监听哪些端口? 8000

VS Codes 然后创建几个文件(见下文)。


当我尝试启动调试器时(如指南中所述),我收到以下错误消息:

终端没有显示任何错误信息,但是执行的命令:


.vscode/launch.json:

{
    "configurations": [
        {
            "name": "Docker: Python - Django",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "/app"
                    }
                ],
                "projectType": "django"
            }
        }
    ]
}

.vscode/tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "python",
            "dockerBuild": {
                "tag": "dockerdebugging:latest",
                "dockerfile": "${workspaceFolder}/Dockerfile",
                "context": "${workspaceFolder}",
                "pull": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": [
                "docker-build"
            ],
            "python": {
                "args": [
                    "runserver",
                    "0.0.0.0:8000",
                    "--nothreading",
                    "--noreload"
                ],
                "file": "manage.py"
            }
        }
    ]
}

Docker文件:

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim-buster

EXPOSE 8000

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1

# Install pip requirements
ADD requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
ADD . /app

# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser && chown -R appuser /app
USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "helloworld.wsgi"]

requirements.txt:

# To ensure app dependencies are ported from your virtual environment/host machine into your container, run 'pip freeze > requirements.txt' in the terminal to overwrite this file
django==3.0.3
gunicorn==20.0.4

与您项目中的 vscode 教程相比,没有错误。因为错误是等待 luncher 连接超时,请尝试重新启动 docker 服务并在 vscode.

中重新加载您的 window

我的问题是缺少包裹。 Docker 通常工作正常,我以前从来没有遇到过任何问题。

我最初按照官方文档中的描述安装了 docker: https://docs.docker.com/engine/install/ubuntu/

但是在我尝试安装 docker.io 包后,在 VS Code 中调试工作正常:

sudo apt install docker.io

在vs code中调试的思路是:

  • 使用 debugpy 通过端口启动代码,例如 5678
  • 在 vscode 到 'Remote Attach' 到那个过程。

我可能错了,但我从你的代码中可以看出,你没有附加到你的过程。

我按照我的方式写 here,我使用 docker-compose 和 docker。我的方法与你的不同,但你会明白的...:)

这里有一个线索和解决方法:

我遵循的说明让我在我的 ubuntu WLS2 实例中打开 VS 代码。注意:我的应用只是一个通用的 python 应用,而不是 django。

如果我单击它并将其更改为作为 windows 文件夹打开,然后进行有趣的调试,一切突然对我有用。 (它启动 docker 并通过调试连接到它,做断点等)

我认为发生在我身上的是“有时”docker 在它自己的 WSL 实例上启动 container/app 而我的 ubuntu 实例无法路由到它进行调试.

我说有时,因为有时它有效。我认为这可能与我启动机器时首先启动哪个应用程序(docker、ubuntu、vscode)有关。 我弄乱了 docker、资源、WSL 集成设置、windows 防火墙,并重新启动了各种东西。

我确信正确的修复并没有那么复杂,但是 运行 本机 windows 中的 VS 代码对我来说已经足够了。我不明白为什么在 WSL 中启动 VS 代码会话的复杂性实际上是必要的。