-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to launch .devcontainer project from command line #2133
Comments
I was able to sort of get it to work using the following invocation: code --folder-uri vscode-remote://dev-container+2f55736572732f6a6f687269612f446576656c6f706d656e742f70726f71756573742d646f776e6c6f61646572/app which I found by grepping through Unfortunately I can't find how to get the mapping of my folder to this random hash: |
I am also interested in this to support CI requests. See tensorflow/addons#1309 (comment) /cc @Chuxel |
actually that is your path represented as hex encoded. The upper string decodes to: seems that splits the part outside and inside the container. Fyi for attaching to existing containers on a remote docker host this snippet might help: remote_description = json.dumps(
{"containerName":"/containername","settings":{"host":"ssh://remote-host"}}
)
remote_uri = urlunparse(('vscode-remote', f'attached-container+{remote_description.encode("utf8").hex()}', '/path/inside/container', '', '', ''))
print(remote_uri) I guess that should be most of the stuff one could need at some point. But since Microsoft did not document any of this. I expect this might be subject to change at any point. |
I'm finding that the "reopen in container" dialog is getting harder and harder to pin down. Recently when I open my project, I get enough other dialogs about things to do (use Pylance, install a linter, install a test framework) that the "reopen in container" dialog gets hidden until I dismiss some of the other dialogs. The movie below shows how the dialog shows up initially and then gets hidden. It is also a bit unsettling to have another dialog/prompt immediately take its place after clicking the "reopen in container" button (but this is another issue). |
@tschaub I'd open a new issue for this. Cause it is unrelated to this issue here. |
I wrote a simple posh script. It works on the Windows side. It doesn't work run form wsl2. I don't know why. $p = "\\wsl$\Ubuntu\home\takekazu\projects\hoge".ToCharArray() | %{$h=''}{$h += ('{0:x}' -f [int]$_)}{$h}
code --folder-uri "vscode-remote://dev-container+$p/workspaces/hoge" |
Based on @takekazuomi script I've managed to create single-line scripts that work on every environments. Just replace Powershell$path = "<hostfolder>"; $p = $path.ToCharArray() | %{$h=''}{$h += ('{0:x}' -f [int]$_)}{$h}; code --folder-uri "vscode-remote://dev-container+$p/<containerfolder>" Bashpath='<hostfolder>' && p=$(printf "%s" "$path" | xxd -p) && code --folder-uri "vscode-remote://dev-container+${p//[[:space:]]/}<containerfolder>" WSLFor WSL you can just use the bash script above using the "\wsl$" prefixed path, however, if you really need to open the devcontainer within the WSL shell, then you can use this command:
|
@Yehonal Your Bash code is actually PowerShell, you probably made a mistake while copypasting. For anyone interested in streamlining this as much as possible, I wrote a Python script that fully automates the process of assembling a https://gist.github.com/ilyasotkov/27e77fd4a1a045bb17f5bcf7f20c1f2e You may put the script to e.g. your user home directory, then run code --folder-uri $(python3 ~/make_devcontainer_folder_uri.py) or to avoid typing so many characters, create an executable script in your #!/usr/bin/env bash
set -e
uri="$(python3 ~/make_devcontainer_folder_uri.py)"
code --folder-uri $uri |
Thanks! I've just fixed it |
If the GUI |
Thanks to the work of you guys, I created a js script that opens my container and prints the vscode command (in color, why not). I then added it to my
I thought I'd share, if it helps someone. const { exec } = require('child_process');
const printColor = (...args) => console.log('\x1b[36m%s\x1b[0m', ...args);
const workingDirectory = process.cwd().replace(/\\/g, '/');
const hostWorkspaceFileHex = Buffer.from(
`${workingDirectory}/my.code-workspace`,
).toString('hex');
const containerWorkspaceFile = '/workspace/myProject/my.code-workspace';
const command = `code --file-uri "vscode-remote://dev-container+${hostWorkspaceFileHex}${containerWorkspaceFile}"`;
printColor(command);
exec(command); |
Building off of @Yehonal's bash one-liner shared above ☝️ If you're using
Usage: Example: Where Be sure to note what @Yehonal said about .devcontainer folder vs file:
|
Hi all and thank you very much for the discussion. I tried several of the examples but couln't make it work. Perhaps someone can help. I'd like to start vscode from a shell script attached to a running docker container NOT created by a .devcontainer definition. The container is selectable within the VSCode UI, but I can't make it work in bash. For example: Sorry and thank you for your help! EDIT: Finally I created a feature request as it seems there's no option to attach to a running container from CLI by anything else than the cryptic ascii ID, which is unusable for typical cases: #5278 |
Hi all, I'm happy to report that there is a new CLI released as part of the 0.188.0 release (currently insiders only). With this version, you can use the "Remote-Containers: Install devcontainer CLI" command to install the CLI. After that you can use |
Is there a way to install the |
I think this issue can be closed: |
So when I install the latest NPM package there is no
|
@metaskills I installed it from within VSCode: |
@baruchiro @metaskills When installing via the extension, open is available. This also makes sure that the version of the CLI is in sync with the currently installed version of the extension in stable / insiders (no functional differences). When installing via npm ( Is there a scenario you had in mind for using the CLI from npm where open is needed? Certainly could be something we look at doing there too. |
Doesn't this already exist? https://code.visualstudio.com/docs/remote/devcontainer-cli |
Great, thank you! TLDR
|
If you are using Docker desktop on Linux, path needs to be like below. Bashpath='{"hostPath":"<hostfolder>","localDocker":false,"settings":{"context":"desktop-linux"}}' && p=$(printf "%s" "$path" | xxd -p) && code --folder-uri "vscode-remote://dev-container+${p//[[:space:]]/}<containerfolder>" |
When I have a vscode devcontainer in a repository, I want the README instructions for how to use VS Code in the repository to go like this:
If there’s a bunch of stuff about first “installing the devcontainer CLI” or “clicking on the Reopen in container pop-up window” then that ruins the simplicity of the instructions.
|
I did something like that here. Is that helpful to folks? https://github.com/customink/crypteia#dev-container-cli |
Also interested in this functionality as like the proposed one by @jamesdbrock |
Came here because all of a sudden Anyway thankfully I remembered using these little shell aliases back before the devcontaner CLI was actually a thing. path="$(wslpath -w $PWD)" && p=$(printf "%s" "$path" | xxd -p) && code --folder-uri "vscode-remote://dev-container+${p//[[:space:]]/}/workspaces/$(basename $PWD)" I don't think |
@brad-jones Thank you! I've been wanting use I also made a bash function from your command with a refactor to read up to 256 bytes at a time, eliminating the need for whitespace manipulation. 😃 function devopen() {
local workspace_folder="$(readlink -f "$1")"
if [ -d "$workspace_folder" ]; then
local wsl_path="$(wslpath -w "$workspace_folder")"
local path_id=$(printf "%s" "$wsl_path" | xxd -ps -c 256)
code --folder-uri "vscode-remote://dev-container%2B${path_id}/workspaces/$(basename "$workspace_folder")"
else
echo "Usage: devopen <directory>" 1>&2
echo "" 1>&2
echo "Error: Directory ${1@Q} does not exist" 1>&2
false
fi
} |
Just to mention, the function above only works if the workspace folder is mounted at |
@felipecrs The value of $ devcontainer read-configuration --workspace-folder . | json
[4 ms] @devcontainers/cli 0.30.0. Node.js v18.12.1. linux 5.15.79.1-microsoft-standard-WSL2 x64.
{
"configuration": {
"name": "nodejs",
"image": "mcr.microsoft.com/devcontainers/javascript-node:0-18-bullseye",
"remoteUser": "node",
"configFilePath": {
"$mid": 1,
"path": "/home/user/dev/project/.devcontainer/devcontainer.json",
"scheme": "vscode-fileHost"
}
},
"workspace": {
"workspaceFolder": "/workspaces/project",
"workspaceMount": "type=bind,source=/home/user/dev/project,target=/workspaces/project"
}
} However, I do not have any devcontainer using something other than the default |
That's a great suggestion, thank you! |
Any ideas on what would be the URI for opening the devcontainer from within a SSH target? |
However it does not work for devcontainers within SSH remotes. Thanks to microsoft/vscode-remote-release#2133 (comment)
BTW, adding |
Actually I just realized this. I'll try to reimplement it in Bash. |
I am currently working on a CLI tool for this, written in Rust - since this is a functionality that I use multiple times a day and I want it to work absolutely reliably with some more features that I need. You can try it out here: https://github.com/michidk/vscli (binaries available - also on cargo and homebrew) Just do USAGE:
vscli [FLAGS] [OPTIONS] <path> [args]...
FLAGS:
-h, --help Prints help information
-i, --insiders Whether to launch the insiders version of vscode
-V, --version Prints version information
OPTIONS:
-b, --behaviour <behaviour> Launch behaviour [default: detect] [possible values: detect, force-container, force-
classic]
-v, --verbosity <verbosity> The verbosity of the output [default: info]
ARGS:
<path> The path of the vscode project to open
<args>... Additional arguments to pass to vscode It works similarly to the bash function from @CodeMan99 but with more error checking and functionality. Also, it reads the In future, I also want add a small TUI that will list the recently opened projects and allow to quickly launch them. |
This has actually been pretty confusing for my team! Some of us installed the |
When can I have |
@AceHack Issue I have with I prefer to use function code_remote {
p=$(printf "%s" "$1" | xxd -p) && code --remote "dev-container+${p//[[:space:]]/}" "$2"
} Usage: Example: Where This can open both |
For some reason |
Thanks @brad-jones ! mypath="$(wslpath -w $PWD)" && p=$(printf "%s" "$mypath" | hexdump -v -e '/1 "%02x"') && code --folder-uri "vscode-remote://dev-container+${p}/workspaces/$(basename $PWD)" or as an alias with proper escaping: alias devcode='mypath="$(wslpath -w $PWD)" && p=$(printf "%s" "$mypath" | hexdump -v -e "/1 \"%02x\"") && code --folder-uri "vscode-remote://dev-container+${p}/workspaces/$(basename $PWD)"' |
I was able to get it working for Remote SSH and Remote Tunnel, the secret is to add Finding out such information is hard though. I am parsing the output of I put this all together in my code shim: Originally posted by @felipecrs in #5957 (comment) I built my own code --devcontainer ~/repos/myproject It works for when VS Code is running on Linux, when running from within a remote SSH session (in or out of the vscode integrated terminal) and even within a remote tunnel session. https://github.com/felipecrs/dotfiles/blob/master/home/dot_local/bin/executable_code It is immensely useful and timesaving for me, so I expect other people to benefit from it too. Simply install it to your WindowsTerminal_yAF02SU1CR.mp4 |
This issue was closed last time with no resolution: #1084
How do I use the
code
CLI to launch into the devcontainer? I have a folder with a.devcontainer
folder. Runningcode .
prompts me to re-open as a devcontainer but I want to do it in one shot in my Makefile.The text was updated successfully, but these errors were encountered: