VS Code Remote SSH连接树莓派Docker,需修改launch.json添加--privileged参数
在VS Code中配置Docker容器的特权模式与DBus挂载
要在VS Code的调试配置中实现终端docker run --privileged -ti --name mycontainername myimagename:latest的等效效果,核心是在launch.json里配置特权模式、DBus挂载及终端交互参数,以下是两种常见场景的配置示例:
通用Docker调试配置(适用于Docker扩展)
打开VS Code调试面板,创建或编辑launch.json,添加如下配置:
{ "version": "0.2.0", "configurations": [ { "name": "Docker: 运行蓝牙应用", "type": "docker", "request": "launch", "preLaunchTask": "docker-build", // 关联构建镜像的任务,若无则可移除 "dockerRun": { "image": "myimagename:latest", "name": "mycontainername", "privileged": true, // 对应--privileged参数 "volumes": [ { "source": "/var/run/dbus", "target": "/var/run/dbus", "type": "bind" // 挂载主机DBus目录到容器 }, { "source": "${workspaceFolder}", "target": "/workspace", "type": "bind" // 挂载本地代码目录到容器,方便调试 } ], "tty": true, // 对应-t参数 "detach": false // 对应-i参数,调试时保持前台运行 }, "sourceFileMap": { "/workspace": "${workspaceFolder}" // 映射容器内代码路径到本地 } } ] }
语言专属调试配置(以Python为例)
如果是基于Python的蓝牙应用,可直接在Python调试配置中嵌入Docker参数:
{ "version": "0.2.0", "configurations": [ { "name": "Python: Docker蓝牙应用", "type": "python", "request": "launch", "program": "${file}", // 指定要调试的入口文件 "console": "integratedTerminal", "docker": { "image": "myimagename:latest", "privileged": true, "volumes": [ {"source": "/var/run/dbus", "target": "/var/run/dbus", "type": "bind"}, {"source": "${workspaceFolder}", "target": "/workspace", "type": "bind"} ], "tty": true } } ] }
注意事项
- 确保VS Code已通过Remote SSH连接到树莓派,且Docker扩展已安装并正常连接到远程Docker守护进程
- 若之前运行过同名容器,先执行
docker stop mycontainername && docker rm mycontainername清理,避免启动冲突 - 调试时若需后台运行容器,可将
detach设为true,但会失去终端交互能力
内容的提问来源于stack exchange,提问作者TommyArduino
相关产品推荐
相关产品推荐

