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

Viewed 1574

I want to debug my Django application using a docker container in Visual Studio Code.

Microsoft published a guide how to do that, which I followed step by step: https://code.visualstudio.com/docs/containers/quickstart-python

But when I try to run the debugger, I get the following error message:

Timed out waiting for launcher to connect

Here is what I did step by step:

  1. I initialized a simple Django application using django-admin startproject helloworld
  2. In VS Code I opened the folder including the manage.py
  3. Opened Command Palette Ctrl + Shift + P, and then selected Docker: Add Docker Files to Workspace...
  4. Select Application Platform Python: Django
  5. Include Docker Compose files No
  6. Relative path to the app's entrypoint manage.py
  7. What ports does your app listen on? 8000

VS Codes then creates several files (see below).


When I try to start the debugger (like in the guide), I get the following error message:

Docker: Python - Django Error: Timed out waiting for launcher to connect

The terminal doesn't show any error messages, but the commands executed: terminal 1

terminal 2


.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"
            }
        }
    ]
}

Dockerfile:

# 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

  • VS Code Version: 1.47.1
  • Python Extension Version: v2020.7.94776
4 Answers

The idea of debugging in vs code is to:

  • use debugpy to launch your code with a port, say 5678
  • in vscode to 'Remote Attach' to that process.

I may be wrong but what i can see from your code, you didn't attach to your process.

I wrote the way I did here, I use docker-compose and docker. My way is different with yours but you will get the idea...:)

My issue were missing packages. Docker usually works fine, I haven't had any issues before at all.

I originally installed docker like described in the official documentation: https://docs.docker.com/engine/install/ubuntu/

But after I tried installing the docker.io package, debugging worked fine in VS Code:

sudo apt install docker.io

There's no errors compared to the vscode tutorial in your project. Cause the error is timeout waiting for luncher to connect, try restart docker service and reload your window in vscode.

Here is a clue and a workaround:

The instructions I followed had me open VS code inside my ubuntu WLS2 instance. Note: my app is just a generic python app, not django.

VS code on WSL

If I click this and change it to open as a windows folder, then fun debug, everything suddenly works for me. (It spins up the docker and connects to it with debug, does breakpoints etc.)

VS code native windows

I think what is happening for me is that "sometimes" docker is starting the container/app on its own WSL instance and my ubuntu instance cannot route to it for the debug.

enter image description here

I say sometimes, because sometimes it works. I think it might be related to which application (docker, ubuntu, vscode) I start first when I boot my machine. I've messed with the docker, resources, WSL integration settings, the windows firewall, and restarted various things.

I'm sure a proper fix is not that complicated, however running VS code in native windows is enough of a fix for me. I don't see why the added complexity of starting the VS code session inside WSL is actually necessary.

Related