通过按F5进行远程调试时,以VS Code启动Docker容器

时间:2018-10-16 10:37:18

标签: python-3.x docker visual-studio-code remote-debugging vscode-debugger

我想远程调试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”之前,它可以正常工作。然后什么也不会发生,因为外壳程序正在等待,因此远程调试过程也无法连接。是否有解决方法(我也尝试过bash并得到相同的结果)还是我做错了,并且有一个更流畅的解决方案。 如果有人能指出我正确的方向,那就太好了。

0 个答案:

没有答案