Skip to main content

VSCode

自動化 attach to container

visual studio code - How to attach a remote container using vscode command line? - Stack Overflow

code CLI 呼叫格式如下:

code --folder-uri vscode-remote://attached-container+<container_name_in_hex>/<path>

注意 <container_name_in_hex> 是 container name 的 hex 而不是 id

例如我開一個 python:3.12 image 的 container,然後把他命名為 python-3.12-container

隨便網上找一個 string 轉換 hex 的工具 (Google)

python-3.12-container 的 hex 是 707974686f6e2d332e31322d636f6e7461696e6572

docker container run ^
--detach ^
--interactive ^
--name "python-3.12-container" ^
--tty ^
python:3.12

code --folder-uri vscode-remote://attached-container+707974686f6e2d332e31322d636f6e7461696e6572/

--detach, --interactive--tty 是為了令 container 不要一打開就退出