Search code examples
pythonvisual-studio-coderemote-debuggingdocker-container

VSCode: cannot remote debug python script on Docker container because the connection fails


I read many similar questions but I couldn't find any answer for me. I also went through the documentation: here and here.

I'm running an Ubuntu container on Windows and I read about the limitation in Networking.

I ran the container with -p 3000:3000

The error => connect ETIMEDOUT

This is my launch.json

"configurations": [
    {
        "name": "Python: Remote Attach",
        "type": "python",
        "request": "attach",
        "port": 3000,
        "host": "172.17.0.2",
        "pathMappings": [
            {
              "localRoot": "${workspaceFolder}/somefolder/somefolder",
              "remoteRoot": "/rootfolder/"
            }
          ]
    },

This is my tasks.json:

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

Solution

  • You can try using the remote extensions as it should handle all of this transparently.

    This is the extension for Visual Studio Code to remote debug in containers. You can attach a python script to a running container and it will import all the needed dependencies.