Geir Ivar Jerstad
Geir Ivar Jerstad

Reputation: 546

How to use the Visual Studio Code *CLI* to Remote-Container Attach to a running Container

I am looking for a way to open Visual Studio Code using the CLI and attach it directly to a container running instead of using GUI.

Attach

Example:

vscode <my-running-container>

Thanks:)

Upvotes: 4

Views: 140

Answers (1)

wyndhamwynne
wyndhamwynne

Reputation: 1

Assuming that your container is running locally, you can do this with the following process:

  1. Identify either the Name or ID of the running container that you would like to attach to. This can be the full (64) or the short (12) character ID, or the Name with or without the leading "/" character. Using the --detach flag along with the docker run command, the full container ID is returned.
  2. Convert the container Name or ID into a continuous hex string.
  3. Use this hex string and your target working directory (within the container) along with the Remote Development (and specifically, Dev Containers) extension pack protocol, vscode-remote://.

Here is an example implementation using commonly available commands:

HEX_CONTAINER_ID=$(echo -n $CONTAINER_NAME_OR_ID | od -A n -t x1 | tr -d ' \n')
code --folder-uri="vscode-remote://attached-container+$HEX_CONTAINER_ID/$WORKDIR"

Upvotes: 0

Related Questions