Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code: connect a docker container in a remote server

I want to work in a container in a remote server. But it doesn't work.

Environment:

Local: Windows 10

Local Terminal for ssh: WSL in Windows 10

Server: Ubuntu 18.04

I checked these two articles.

https://code.visualstudio.com/docs/remote/containers-advanced https://code.visualstudio.com/docs/containers/ssh

I followed these steps.

  1. I installed [Remote Development] extension in VS Code.
  2. Remote-SSH: Connect to host. It works fine.
  3. I Installed [Docker] extension on the remoter server.
  4. Now I can see my containers and images in a docker tab.
  5. I clicked one container and clicked [Attach Visual Studio Code] and it says There are no running containers to attach to.

enter image description here

like image 857
Hakjin Lee Avatar asked Feb 27 '20 01:02

Hakjin Lee


People also ask

How do I connect to VS Code remote container?

To attach to a Docker container, either select Remote-Containers: Attach to Running Container... from the Command Palette (F1) or use the Remote Explorer in the Activity Bar and from the Containers view, select the Attach to Container inline action on the container you want to connect to.

Can Docker desktop connect to remote host?

To use the remote host as your Docker host instead of your local machine, set the DOCKER_HOST environment variable to point to the remote host. This variable will instruct the Docker CLI client to connect to the remote server. Now any Docker command you run will be run on the Droplet.


1 Answers

I resolved this problem by switching to the remote server's Docker context on my local machine:

docker context create some-context-label --docker "host=ssh://user@remote_server_ip"  docker context use some-context-label  docker ps # A list of remote containers on my local machine! It works! 

After that:

  1. Connect via Remote-SSH to the container server
  2. Right click relevant container -> the "Attach Visual Studio Code"

That works for me.

(Note: One would think that I should be able to just use my local VSCode (skip step 1) to connect to said remote container after switching my local context, but VSCode complains Failed to connect. Is docker running? in the Docker control pane.)

like image 130
Daniel Avatar answered Sep 24 '22 13:09

Daniel