How to Attach to a Running Container in a Remote Host from Windows

Hezi Hershkovitz
4 min readJan 26, 2021

--

Container on the move.. completely unrelated to the post, but nice image nevertheless (source: Unsplash)

I spent a few hours yesterday until I finally made it work, so I figured out it worth spending time documenting how to get it done for others who might face the same issues that I encountered, and also for myself, since I will probably want to redo it on other containers in the future.

For me, the benefit of being able to attach to a running container on a remote host, was using the remote container as a development environment, while still using vscode from my computer as usual.

This is the official documentation from vscode that I was trying to follow: Connect to remote Docker over SSH. However, few things didn’t work as expected, and I tested out several things until I find the solution that actually worked for me.

WSL was not used with the final solution. Although I have WSL in my computer, and currently my local docker containers are inside WSL, the docker CLI that I used for the solution is the “external” (non-WSL) docker service from the host machine (that is not not actually running):

> docker ps -a
Error response from daemon: open \\.\pipe\docker_engine_linux: The system cannot find the file specified.

The reason it works is because I am using docker context, and for that only docker CLI is needed.

The first steps are similar to the official document. First, have a key or create one, then configure the ssh-agent using Powershell (OpenSSH) method. I used different commands to run the ssh-agent:

#copy private key to `c:\Users\%USERNAME%\.ssh` > Start-Service ssh-agent
> start-ssh-agent
#To check the status:
> Get-Service ssh-agent

ssh-add -l didn’t work for me (but the solution still worked):

> ssh-add -l
Error connecting to agent: No such file or directory

To check that it is working, you can try to ssh to the remote host without the key:

ssh root@123.456.789.123

Next, you need to create a docker context. Like I said before, for this step, it is not ciritical if docker service is not running in the host, since all that is required is docker CLI in the command line. I used Powershell, but I believe that from here simple cmd shell is enough:

docker context create my_context_name --docker "host=ssh://root@123.456.789.123"docker context use my_context_name

At this stage you should be able to do docker ps -a from command line and see the containers in the remote host!

Now, open vscode. I used vscode insiders (1.53.0), but later checked also with normal vscode, and it worked as well. Make sure that you have the following extensions installed:

  • Docker
  • Remote-SSH
  • Remote-Development
  • Remote-Containers

On the docker tab, in the CONTEXTS section, click on the context that you created for the remote host.

Here started the weird stuff… Even after switching to the new context, I was not able to see the containers in the vscode UI (Error: Request time out). In a moment of dispair I opened the vscode devtools, and saw some weird errors. See image below:

I can’t see the containers in the UI (#1), while they exists in the terminal (#2), using the same context in the terminal and in vscode (#3,#5). The dev tools error with pageant is in #4.

The error in devtools was because it looking for pageant inside an extension folder, and pageant wasn’t there. I manually copied pageant to that folder (C:\Users\%USERNAME%\.vscode-insiders\extensions\ms-azuretools.vscode-docker-1.9.1\util\pagent.exe), but it still didn’t work.

However, this is how it does work:
For some reason you CAN see the containers when clicking on the ‘remote-explorer -> containers’ in vscode:

And voila!
Click on Attach to Container and should connect to the remote container so that you can use vscode as usual while inside the file system of the container in the remote host.

I hope that the brief explanation helps you. I would be happy to know if it did, or if you find an easier way to connect to a remote container. If you know of such a way, please do share in the comments!

--

--

Hezi Hershkovitz
Hezi Hershkovitz

Written by Hezi Hershkovitz

Programmer by day, blogger and business owner by night https://successhowto.com/mh

No responses yet