Search code examples
pythondjangodockervisual-studio-codevscode-debugger

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


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

Solution

  • 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