xdhmoore
xdhmoore

Reputation: 9876

VS Code Remote Debugging Python in a Docker container

I am using VS Code's "Remove - WSL" extension to try to connect to a Docker container (backed by WSL2) running a Python job.

The python job is started somewhat like this:

exec python3 -m debugpy --listen 5678 --wait-for-client some_file.py

In my docker-compose.yml file I have set

ports:
  - "5678:5678"

And I run the service with

docker-compose up

My VS Code launch.json contains the following:

    {
        "name": "Python: Remote Attach",
        "type": "python",
        "request": "attach",
        "connect": {
            "host": "localhost",
            "port": 5678
        },
        "pathMappings": [
            {
                "localRoot": "/home/myname/tensorflow_models/research",
                "remoteRoot": "/home/tensorflow/models/research"
            }
        ]
    }

However, running the above task briefly opens the debug bar and then closes it with no error message.

When I exec into the container I can run netcat localhost 5678 and it will return json. However, it doesn't return anything when I run the same from WSL:

netcat localhost 5678

or on Windows from powershell:

powercat -c localhost -p 5678

Or even from within the container using the container's ip:

# Manually get ip address
ip a
# Doesn't return anything
netcat <ip> 5678

Upvotes: 1

Views: 1465

Answers (1)

xdhmoore
xdhmoore

Reputation: 9876

The problem was that by default debugpy only listens on localhost (local connections only). The solution was to set the listen address to 0.0.0.0 (all addresses bound to the host):

exec python3 -m debugpy --listen 0.0.0.0:5678 --wait-for-client some_file.py

Upvotes: 2

Related Questions