python-3.x - 远程调试时按 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 - 重定向视图中的 Django NoReverseMatch 错误
- javascript - Swift 中的 ArrayBuffer 和 Float64Array
- embedded - 将项目从 ICCAVR 移植到 Atmel Studio 7.0 _flash 问题
- swift - 在 Swift 中向 xib 视图添加阴影
- regex - 无法选择正确的数据
- node.js - 如何将 POST 请求从 NodeJS 应用程序发送到 AWS Lambda?
- python - 在文件路径列表中查找以相同字符串开头的 python 文件
- css - 两个单词之间水平线的 CSS 技术
- python - 在专用内核上启动应用程序
- javascript - 与 node10 相比,Nodejs 自定义排序在 node12 中的行为不同