如何使用 vscode 命令行附加远程容器?

xs2*_*233 6 visual-studio-code vscode-remote

我想使用命令行工具来附加远程容器。我尝试了这个命令(见下文),但没有用。有谁知道正确的命令吗?

code --folder-uri vscode-remote://dev-container+4aaf623ee98a52fa311226a2c619be19addfa221c090b9a3bc37e7cba03a7fce/easycv
Run Code Online (Sandbox Code Playgroud)

Gei*_*tad 6

为了自动化此操作,我创建了这个跨平台的解决方案:

https://github.com/geircode/vscode-attach-to-container-script

该解决方案根据正在运行的容器的名称创建十六进制。

Windows CMD 脚本:

docker run --rm geircode/string_to_hex bash string_to_hex.bash "<container_name>" > vscode_remote_hex.txt

set /p vscode_remote_hex=<vscode_remote_hex.txt

code --folder-uri=vscode-remote://attached-container+%vscode_remote_hex%/app
Run Code Online (Sandbox Code Playgroud)


Jar*_*lls 4

dev-container+ 之后的字符串是以十六进制编码的 dev 容器文件夹的 ascii 路径。

要打开容器中的文件夹,您可以使用以下样式命令:

code --folder-uri=vscode-remote://dev-container%2B{path-in-hex}/{path-inside-container}
Run Code Online (Sandbox Code Playgroud)

例如,要打开/workspaces/test位于的开发容器中的文件夹/Users/jkells/projects/vscode-devcontainer,我使用以下 CLI 命令。

code --folder-uri=vscode-remote://dev-container%2B2f55736572732f6a6b656c6c732f70726f6a656374732f7673636f64652d646576636f6e7461696e6572/workspaces/test
Run Code Online (Sandbox Code Playgroud)

要将字符串转换/Users/jkells/projects/vscode-devcontainer为十六进制,2f55736572732f6a6b656c6c732f70726f6a656374732f7673636f64652d646576636f6e7461696e6572可以使用以下命令

printf /Users/jkells/projects/vscode-devcontainer | od -A n -t x1 | tr -d '[\n\t ]'
Run Code Online (Sandbox Code Playgroud)