简体   繁体   中英

The "code" command does not work when connecting to a Docker container remotely with VSCode

I am using VSCode in my local PC and connecting to a Docker container in a remote server with VSCode's Extensions of Remote - SSH and Remote - containers . However, when I type

$ code <file name>

on the VSCode's terminal (Bash), I get an error messages saying that

bash: code: command not found

and I can't edit the file on the VSCode's editor.

If I click on the file from VSCode's Explorer (Ctrl+Shift+E), the edit screen will appear, but isn't it possible to call it with the code command?

Also, I call the command palette (Ctrl+Shift+P) and then search for Shell Command: Install 'code' command in PATH but no matching commands are found.

The execution environment is as follows:.

  • Local PC: Windows 10 Pro
  • Remote host PC: Ubuntu 18.04.3 LTS
  • Docker container in the remote host PC: Ubuntu 18.04.3 LTS

Thank you very much for your answer.

I'll answer myself as I was able to call VSCode using the code command in the remote's container.

When I look closely underneath home, I see that there was a code at the following directory.

$HOME/.vscode-server/bin/<directory with a hash-like name>/bin/

So I passed the PATH through it and it worked.

By the way, <directory with a hash-like name> is a directory with a hash-like name, which is randomly assigned when you connect to the container remotely. It's different every time, so please refer to it by yourself.

The way to register the path is as follows.

export PATH="$PATH:$HOME/.vscode-server/bin/<directory with a hash-like name>/bin/"

Thank you very much for your support.

They posted an easy solution in response to the issue created by @davetapley here .

Go to the VSCode settings, search for "terminal integrated inherit env" and enable the option. The code command will be available the next time you open a terminal.

(This should have been a comment but I don't have that privilege yet!)

It sounds like you are confusing which place you are writing the code command. Your installation of Visual Studio Code is local on your machine and not inside the docker container. When you open a terminal inside the docker container this is as if it was a different machine altogether. Here is a link to vscode documentation that is both interesting and useful.

Here is what worked for a similar problem where the code command was not working as expected on my Linux system, connected to my windows PC via VSCode's Extensions of Remote - SSH : adding VS Code to PATH by editing the ~/.bashrc file in my linux remote system .

I used the path variable from akki's answer , and the procedure detailed in this answer by oadams . To edit this file in my system, I use nano:

nano ~/.bashrc

at the end the file, add the export path statement akki mentioned, just without the quotation marks:

export PATH=$PATH:$HOME/.vscode-server/bin/<directory with a hash-like name>/bin/

However, my hashtag-like-name of the code mentioned in akki's answer does not change when I remote SSH to my Raspberry Pi, so I am not sure how to fix that part of the problem.

My execution environment is as follows:

  • Local PC: Windows 10 on Dell Latitude PC.
  • Remote host PC: Raspbian GNU/Linux 10 (buster) on Raspberry Pi 3B.

Derived from @akki's answer, I noticed that the hash is stored in several environment variables. So I added this to my .zshrc which simply finds the path to the bin and then makes an alias.

VSCODE_SSH_BIN=$(echo "$BROWSER" | sed -e 's/\/helpers\/browser.sh//g')
alias code='$VSCODE_SSH_BIN/remote-cli/code'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM