首页 > 解决方案 > 远程调试时按 F5 在 VS Code 中启动 Docker 容器

问题描述

我想远程调试一个 docker 容器并在按下 F5(调试)时启动它。它可以很好地解决那个特定的问题。容器已启动,但外壳并未退出,因此没有任何反应。Python源代码只是为了检查它是否有效:

import ptvsd
import time
import os

print("Waiting to attach")

address = ('0.0.0.0', 3000)
ptvsd.enable_attach(address)
ptvsd.wait_for_attach()

time.sleep(2)
print('something changed')
print("end")

我尝试使用 launch.json 的preLaunchTask运行它:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Terminal (integrated)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "DOCKER (Remote Debug)",
            "type": "python",
            "request": "attach",
            "port": 3000,
            "host": "localhost",
            "pathMappings": [
                {"localRoot": "${workspaceFolder}", "remoteRoot": "/app/"}
            ],
            "preLaunchTask": "start_docker_compose",
            "redirectOutput": false
        }
    ]
}

另外,我创建了一个task.json来运行容器:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "start_docker_compose",
            "type": "shell",
            "command": [
                "docker run -it -p 5000:5000 dockertest",

            ],
            "problemMatcher": []

        }
    ]
}

在 shell 命令启动容器并且字符串“Waiting to attach”出现之前,它工作得很好。然后什么也没有发生,因为 shell 等待,因此远程调试过程也无法连接。是否有解决方法(我也尝试过 bash,结果相同)还是我错了,并且有一个更流畅的解决方案。如果有人能指出我正确的方向,那就太好了。

标签: python-3.xdockervisual-studio-coderemote-debuggingvscode-debugger

解决方案


推荐阅读