6

I want to use a command line tool to attach a remote container. I tried this command (see below), but it's of no use. Does anyone know the correct command?

code --folder-uri vscode-remote://dev-container+4aaf623ee98a52fa311226a2c619be19addfa221c090b9a3bc37e7cba03a7fce/easycv
1
  • Can you specify what you meant by of no use ? I tried to attach to a container without use of devcontainer.json, so the uri contained attached-container instead of dev-container, so the command was: code --folder-uri vscode-remote://attached-container+7b22636f6e7461696e65724e616d65223a222f70726963656c6573735f686f70706572227d/app. Replacing the container id with the container name was similar, so I got an error message with id or name being shown as garbage. But a prompt appeared asking to choose a running container, and from there it was possible to attach the wanted container. Commented Apr 16, 2020 at 20:35

6 Answers 6

7

To automate this, I created this cross-plattform-ish solution:

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

This solution creates the hex based on the name of the running container.

Windows CMD script:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Since I stumbled on this during my search, I had a hard time getting this to work until I realized that it was because I was using Docker Desktop for Linux. Docker Desktop for Linux uses a different context than the default Docker context, so you'll need to modify the payload of your JSON string in this command to: {"conainerName":"CONTAINER_NAME","settings":{"context":"desktop-linux"}} The new addition is context within the settings object.
4

That string of characters after dev-container+ is an ascii path to your dev container folder encoded in hexadecimal.

To open a folder in a container you can use the following style command:

code --folder-uri=vscode-remote://dev-container%2B{path-in-hex}/{path-inside-container}

For example to open the folder /workspaces/test in the development container located in /Users/jkells/projects/vscode-devcontainer I use the following CLI command.

code --folder-uri=vscode-remote://dev-container%2B2f55736572732f6a6b656c6c732f70726f6a656374732f7673636f64652d646576636f6e7461696e6572/workspaces/test

To convert the string /Users/jkells/projects/vscode-devcontainer into the hexadecimal 2f55736572732f6a6b656c6c732f70726f6a656374732f7673636f64652d646576636f6e7461696e6572 you can use the following command

printf /Users/jkells/projects/vscode-devcontainer | od -A n -t x1 | tr -d '[\n\t ]'

Comments

4

From Contributor chrmarti on Janv. 30, 2023:

code --folder-uri vscode-remote://attached-container+$(printf "$CONTAINER_NAME_OR_ID" | xxd -p)/home

To open a workspace instead of a folder, use --file-uri instead, and target the .code-workspace:

code --file-uri vscode-remote://attached-container+$(printf "$CONTAINER_NAME_OR_ID" | xxd -p)/home/my_workspace.code-workspace

1 Comment

xxd -p breaks if it has to wrap. xxd -p -c1000 works.
3

This shell script does the job:

#!/usr/bin/env bash

case $# in
1) ;;
*) echo "Usage: code-remote-container <directory>"; exit 1 ;;
esac

dir=`echo $(cd $1 && pwd)`
hex=`printf ${dir} | od -A n -t x1 | tr -d '[\n\t ]'`
base=`basename ${dir}`
code --folder-uri="vscode-remote://dev-container%2B${hex}/workspaces/${base}"

I have saved it under the name code-remote-container, which then e.g. can be used as:

code-remote-container .

which would open the current directory in the remote container.

Obviously this expects that the remote container has already been setup for vsc.

Comments

1

To make it clear: you need to use

code --folder-uri vscode-remote://attached-container+<X>/workspaces/bla_bla`

where X is containerName in hex encoding.

Container name is this one:

literally name of container(not always biggest label in GUIs)

you can find it in docker inspect json in field "Name"

Literally name of container(not always biggest label in GUIs).

You can find it in docker inspect json in field "Name".

Slash at the beginning doesn't matter(can be or not).

To make conversion to hex use online converters, or maybe set /a on Windows (I don't think it's gonna work) or

printf some_name | od -A n -t x1 | tr -d '[\n\t ]'

on Linux(get from here)

or you can use bash(like Git Bash)

or use some cross-plattform-ish solution from Geir

If you want to specify more(like Garrett) than name - encode json with container settings

Like this

printf {\"conainerName\":\"ecstatic_shamir\",\"settings\":{\"context\":\"desktop-linux\"}} | od -A n -t x1 | tr -d '[\n\t ]'

and put encoded hex number in X.

Don't forget to escape quotation marks in json with backslash "\".

If you want to start container at first - try to replace attached-container with dev-container

You can probably use Id instead of Name in X, but it already in hex(do I need to encode it again?), and should i use full version or just short?

My Windows desktop icon to start VSCode with docker container now looks like:

"C:\Program Files\VSCode\Code.exe" --folder-uri vscode-remote://attached-container+2f65637374617469635f7368616d6972/workspaces/sapfor

where X is printf ecstatic_shamir | od -A n -t x1 | tr -d '[\n\t ]'

Comments

0

I found that using the command that another user mentioned works well, but instead of using xxd I find it better to use od since it seems to be pre-installed on linux.

code --folder-uri vscode-remote://attached-container+$(printf "$CONTAINER_NAME_OR_ID" | od -An -tx1 | tr -d ' \n')

Edit: I created a new answer instead of commenting because I don't have enough reputation to comment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.