Skip to content

Instantly share code, notes, and snippets.

@cloudhan
Last active May 17, 2025 03:37
Show Gist options
  • Save cloudhan/4c4a7376972f3a3fc21b7558ef194c24 to your computer and use it in GitHub Desktop.
Save cloudhan/4c4a7376972f3a3fc21b7558ef194c24 to your computer and use it in GitHub Desktop.
launch VS Code ssh connect to remote and attach to a container with a path opened
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--target-host", "-t", default=None)
parser.add_argument("container", help="container name or id")
parser.add_argument("path", help="path to open in devcontainer")
args = parser.parse_args()
if args.target_host:
# # NOTE: both are OK
# # https://github.com/microsoft/vscode-remote-release/issues/2133
# # https://github.com/microsoft/vscode-remote-release/issues/8764#issuecomment-1701046905
# remote_desc = json.dumps({"containerName": f"/{args.container}"}).encode("utf8").hex()
# remote_uri = f"vscode-remote://attached-container+{remote_desc}@ssh-remote+{args.target_host}{args.path}"
remote_desc = json.dumps({
"containerName": f"/{args.container}",
"settings": {
"host": f"ssh://{args.target_host}"
}
}).encode("utf8").hex()
remote_uri = f"vscode-remote://attached-container+{remote_desc}{args.path}"
if args.path.endswith(".code-workspace"):
# https://stackoverflow.com/a/78779725/2091555
print("code --file-uri " + remote_uri)
else:
print("code --folder-uri " + remote_uri)
@cloudhan
Copy link
Author

cloudhan commented Feb 12, 2025

python remote-devcontainer.py -t <hostname> <container_name> /path/in/container | sh
python remote-devcontainer.py -t <hostname> <container_name> /path/in/container | Invoke-Expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment