Skip to content

Instantly share code, notes, and snippets.

@niderhoff
Last active June 30, 2021 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niderhoff/e8ee7a76ee2328d35dce16c36385efd5 to your computer and use it in GitHub Desktop.
Save niderhoff/e8ee7a76ee2328d35dce16c36385efd5 to your computer and use it in GitHub Desktop.
VS Code Attach to running container fromCLI

Attach to VS Code Running Container from CLI

Usage:

chmod +x code_attach
code_attach <container_name> <path> <docker_context>
  • container_name: name of the running container
  • path: path inside container (optional)
  • docker_context: name of the docker context (optional)

Get the Container Name:

$ docker ps
CONTAINER ID   IMAGE           COMMAND       CREATED        STATUS       PORTS     NAMES
c37f6631e013   deepstream-hp   "/bin/bash"   19 hours ago   Up 2 hours             hardcore_wu <----------
b5a123ad0c58   deepstream      "/bin/bash"   8 days ago     Up 6 days              epic_cannon

If not is not present it may be stopped? (docker container ls -a), then you need to start it first: docker start hardcore_wu

In my case I am connected to a custom docker context called "xavier" (you can see the active docker context using docker context ls):

code_attach hardcore_wu /workspace xavier
#!/bin/bash
echo "Trying to attach to: $1 on path $2 on context $3"
context_ascii=",\"settings\":{\"context\":\"${3}\"}"
remote_ascii="{\"containerName\":\"/${1}\""
end_ascii="}"
hex=$(printf "$remote_ascii$context_ascii$end_ascii" | od -A n -t x1 | tr -d '[\n\t ]')
codestring="code --folder-uri=vscode-remote://attached-container%2B$hex$2"
echo $codestring
$codestring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment