VSCode:无法在 Docker 容器上远程调试 python 脚本,因为连接失败

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

我读了很多类似的问题,但找不到适合我的答案。我还浏览了文档:here and here

我是 运行 Windows 上的一个 Ubuntu 容器,我读到了 limitation in Networking

I 运行 带-p的容器3000:3000

错误 => 连接 ETIMEDOUT

这是我的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/"
            }
          ]
    },

这是我的 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"
            }
        }
    ]
}

您可以尝试使用 remote extensions,因为它应该透明地处理所有这些。

This 是 Visual Studio 容器中远程调试代码的扩展。您可以将 python 脚本附加到 运行 容器,它将导入所有需要的依赖项。