Using the Remote Development extension pack, Visual Studio Code is able to open a Docker container running on the current system, and to open a folder on a remote host connected using SSH.
If there is a Docker container running on a remote SSH host, how can VSCode open a file/folder in this container?
Connect to a remote host Verify you can connect to the SSH host by running the following command from a terminal / PowerShell window replacing user@hostname as appropriate. In VS Code, select Remote-SSH: Connect to Host... from the Command Palette (F1, Ctrl+Shift+P) and use the same user@hostname as in step 1.
VS Code Remote SSHYou can connect over SSH into another machine from Visual Studio Code and interact with files and folders anywhere on that remote filesystem. If you have an app located on a different computer, you could use SSH to connect to it and access your app, view its files, and even modify, run, and debug it.
Use the Command Palette (Ctrl+Shift+P) to issue the Docker Context: Use command to activate the Docker context pointing to the remote machine. This command causes both VS Code and Docker CLI to use the remote machine context.
So you want to open a folder inside a container in a remote SSH server. Following are the steps through which I achieved it.
Step 1
install Remote Development extension in VSCode and open its control panel
Step 2
Connect VSCode to remote SSH server
Step 3
Upon connecting you'll notice a green tick sign, means you are connected to remote server, now select Containers from Remote Explorer dropdown. You'll see a list of containers. If you don't see the list of container or if you see list of local containers then follow along, otherwise move to Step 4.
Step 3.1
Add new context to your docker by executing following commands in terminal.
$ docker context create my-remote-docker-machine --docker "host=ssh://username@host:port"
$ docker context ls
$ docker context use my-remote-docker-machine
Step 3.2
$ docker ps -a
You'll notice that above command lists all the containers from remote server now (its because of the context which you just added)
Lets come back to VScode now, click on Docker
from side bar menu, You'll notice a warning Failed to connect. Is Docker Running? know more about this bug
Troubleshooting
Verify that Docker extension is installed on the remote machine. As of February 2020 there is a bug in VS Code that prevents the Docker extension to be installed remotely if it is already installed locally. This bug is scheduled to be fixed in VS Code 1.43 release. A workaround to get the extension installed remotely is described here.
Step 4
From the containers list select the target container which you want your VSCode to connect to.
Step 5
Vala! VSCode is connected to the container inside remote server, you can click on Explorer and select any folder/directory inside container and start working in that directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With