11

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?

Gama11
  • 31,714
  • 9
  • 78
  • 100
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

1 Answers1

16

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

enter image description here

Step 2

Connect VSCode to remote SSH server

enter image description here

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.

enter image description here

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

enter image description here

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.

  • Uninstall Docker extension from VS Code. Close all VS Code windows.
  • Start VS Code again. Use `Remote-SSH: Connect to Host..' to connect to remote machine.
  • Open extension marketplace and install Docker extension on the remote machine (see screenshot below)

enter image description here

  • Once the Docker extension is installed remotely, it can also be installed locally.

Step 4

From the containers list select the target container which you want your VSCode to connect to.

enter image description here

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.

enter image description here

Hamid Ali
  • 875
  • 1
  • 8
  • 22
  • Selecting 'Containers' in step3 lists down containers in the local machine and not on the remote machine. How can we list the containers of the remote-machine? – Anoop K. Prabhu Jul 01 '20 at 18:11
  • @AnoopK.Prabhu make sure your connection to remote server is successful – Hamid Ali Jul 01 '20 at 18:48
  • Yes it is. the green symbol is shown too. https://ibb.co/D1HW3Vw is what I see after the connection to remote machine is successful. Else I could find dockers in my local machine – Anoop K. Prabhu Jul 01 '20 at 19:39
  • 1
    @AnoopK.Prabhu so I found out that my docker host was set to remote already, that is the main reason it worked for me. But then I tried to reproduce your use-case by connecting to different remote server. I'm sharing my findings by updating the answer. Please have a look into it. Keenly waiting for the feedback. – Hamid Ali Jul 04 '20 at 02:50
  • The updated answer worked for me. Actually I had to do a bit of change for myself as the remote machine was behind a VPN which has a password and 2-step authentication. This made 'docker context' usage difficult. Port forwarding the docker.sock helped to deal with it. – Anoop K. Prabhu Jul 06 '20 at 17:29
  • 1
    Great!, @AnoopK.Prabhu feel free to accept this answer so that others can follow it too. Thanks. – Hamid Ali Jul 07 '20 at 06:55