Vscode-docker: Debugger is not started correctly when debugging containerized Python app in WSL

Created on 19 Jan 2021  路  10Comments  路  Source: microsoft/vscode-docker

Hi,

Similar issue as #2313. That solution didn't solve the issue to me. I have tried adding "debugAdapterHost": "172.17.0.1", but it still doesn't work.

Debugger doesn't connect to debugpy when starting my container. The container is built and run, but when handing over to debugpy nothing happens.

  • Python Debug Console is not started.

This is printed in the console, and then nothing more happens.

Executing task: docker-run: debug <

> docker run -dt -P --name "test-dev" --label "com.microsoft.created-by=visual-studio-code" -v "/home/<user>/.vscode-server/extensions/ms-python.python-2020.12.424452561/pythonFiles/lib/python/debugpy:/debugpy:ro" --entrypoint "python" "test:latest" <

094d9d0df53c4814600415523605eee4e444bcff6f5175219aa31808df3e55d8

Terminal will be reused by tasks, press any key to close it.

launch.json - I've tried setting host to localhost too.

{
    "configurations": [
        {
            "name": "Docker: Python - General",
            "platform": "python",
            "type": "docker",
            "request": "launch",
            "preLaunchTask": "docker-run: debug",
            "debugAdapterHost": "172.17.0.1",
            "python": {
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "/app"
                    }
                ],
                "projectType": "general",
                "host": "0.0.0.0",
                "port": 5678
            }
        }
    ]
}

task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "docker-build",
            "label": "docker-build",
            "platform": "python",
            "dockerBuild": {
                "tag": "test:latest",
                "dockerfile": "${workspaceFolder}/Dockerfile",
                "context": "${workspaceFolder}",
                "pull": true
            }
        },
        {
            "type": "docker-run",
            "label": "docker-run: debug",
            "dependsOn": [
                "docker-build"
            ],
            "python": {
                "file": "main.py"
            }
        }
    ]
}

Dockerfile

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

# 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
COPY requirements.txt .
RUN python -m pip install -r requirements.txt

WORKDIR /app
COPY . /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 ["python", "main.py"]

Environment

VSCode

  • Version: 1.52.1 (user setup)
  • Commit: ea3859d4ba2f3e577a159bc91e3074c5d85c0523
  • Date: 2020-12-16T16:34:46.910Z
  • Electron: 9.3.5
  • Chrome: 83.0.4103.122
  • Node.js: 12.14.1
  • V8: 8.3.110.13-electron.0
  • OS: Windows_NT x64 10.0.18363

Running remote VSCode in WSL2

  • Docker extension v1.9.0
  • Python extension v2020.12.424452561
Python Debug fix released

Most helpful comment

The logic for determining that value is this, basically (code here and here):

  1. If the user specifies anything, we use it
  2. Otherwise, on Mac and Windows, we use localhost; on Linux we try to inspect the bridge network and get the gateway IP address
  3. If we are not able to do get the gateway IP, we use 172.17.0.1, which is the default gateway IP for bridge.

localhost does not work on Linux for whatever reason. The flaw in our logic is that in WSL, apparently localhost _does_ work, so we need to treat WSL differently from Linux.

All 10 comments

I found a possible workaround, if @sobait or @kristjanvalur can try it. I set "debugAdapterHost": "localhost", and it seemed to work:

image

You can ignore the yellow squiggles / warning, the debugAdapterHost property doesn't technically exist on the docker launch configuration, but we pass through all properties (except a few) when resolving to a python launch configuration--so, debugAdapterHost gets passed along to the Python extension.

If I had to take a guess I'd say that WSL works with "localhost", unlike Linux.

Thanks for that fast response. It works! I tinkered a lot with host and ports but never thought about testing "debugAdapterHost": "localhost".

And indeed this issue was the same as #2642. It also worked for me when running locally on Windows, but failed in WSL.

Cool, thanks. By the way, what is the default value for this value?

The logic for determining that value is this, basically (code here and here):

  1. If the user specifies anything, we use it
  2. Otherwise, on Mac and Windows, we use localhost; on Linux we try to inspect the bridge network and get the gateway IP address
  3. If we are not able to do get the gateway IP, we use 172.17.0.1, which is the default gateway IP for bridge.

localhost does not work on Linux for whatever reason. The flaw in our logic is that in WSL, apparently localhost _does_ work, so we need to treat WSL differently from Linux.

This might be another issue, or just me not understanding how to configure everything correctly.

@bwateratmsft How to successfully debug when running vscode-docker extension inside a devcontainer? I have a devcontainer.json based on docker-from-docker, and trying to debug the same Python app as previously (which is in its own container).

Setting debugAdapterHostto localhost seems to work (Python Debug Console starts), but after that it timeouts.
image

Docker-from-Docker unfortunately doesn't really permit debugging. Bind mounts are used for Python debugging (and extensively used for .NET debugging), which are difficult or impossible to make work in a Docker-from-Docker arrangement.

GitHub Codespaces recently changed over to a Docker-_in_-Docker setup for their default image; among other advantages it supports debugging Python and .NET.

Thanks, I will look into Codespaces and see if I can learn something from it.

It would be very nice to have a containerized dev environment for each app our team is working on, but have the same debugging capabilities as when running everything locally.

This is now released in Docker extension version 1.10.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yusufkaratoprak picture yusufkaratoprak  路  4Comments

chrisdias picture chrisdias  路  7Comments

sajayantony picture sajayantony  路  6Comments

revmischa picture revmischa  路  6Comments

gngchrs picture gngchrs  路  3Comments