Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugger is not started correctly when debugging containerized Python app in WSL #2641

Closed
tjohanssonn opened this issue Jan 19, 2021 · 10 comments · Fixed by #2650
Closed

Debugger is not started correctly when debugging containerized Python app in WSL #2641

tjohanssonn opened this issue Jan 19, 2021 · 10 comments · Fixed by #2650

Comments

@tjohanssonn
Copy link

tjohanssonn commented Jan 19, 2021

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
@bwateratmsft
Copy link
Collaborator

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.

@bwateratmsft
Copy link
Collaborator

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

@tjohanssonn
Copy link
Author

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

@tjohanssonn
Copy link
Author

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

@kristjanvalur
Copy link

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

@bwateratmsft
Copy link
Collaborator

bwateratmsft commented Jan 19, 2021

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.

@tjohanssonn
Copy link
Author

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 debugAdapterHost to localhost seems to work (Python Debug Console starts), but after that it timeouts.
image

@bwateratmsft
Copy link
Collaborator

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.

@tjohanssonn
Copy link
Author

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.

@bwateratmsft bwateratmsft removed their assignment Jan 20, 2021
@bwateratmsft bwateratmsft changed the title Debugger is not started correctly when debugging containerized Python app. Debugger is not started correctly when debugging containerized Python app in WSL Feb 1, 2021
@bwateratmsft
Copy link
Collaborator

This is now released in Docker extension version 1.10.0.

@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants