'Rootless Docker (remote system) and VS Code - Attach VS Code to container failing

I am using the following setup

  • Remote system

    • Running "rootless Docker"
    • Docker context named "rootless" being active
    • VS Code Docker extension being installed
  • VS Code - Connecting via SSH to the remote machine using "Remote Extension"

    • Building and runing the Docker container using rootless Docker
    • Checking that the "rootless" Docker context is selected
    • Trying to use "right-click" option on container "Attach Visual Studio Code", which will fail with the following error message:
    Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
    

VS Code tries to refer to the "default" Docker context, altouhgh the "rootless" Docker context is selected, which in my case is:

rootless *    Rootless mode    unix:///run/user/1001/docker.sock

Also when the "docker.host" is set to the one of the rootless Docker or if the "docker.context" is set to the "rootless", using "Attach Visual Studio Code" will fail with the same error message.

What did work out is the following:

  • On local machine defining Docker context for remote docker context create <some_name> --docker host="ssh://<user>@<ip address>
  • VS Code - connected to local machine
    • Selecting the Docker context of the remote machine
    • Using "Attach Visual Studio Code" to attach to remote container

Does someone know how to fix the issue, so that it is possible to use "Attach Visual Studio Code" directly from the VS Code window being connected via ssh to the remote system?



Solution 1:[1]

My SSH config looks like this

Host remote
    HostName 1.2.3.4
    User ubuntu
    IdentityFile ~/.ssh/remote
    SetEnv DOCKER_HOST=unix:///run/user/1001/docker.sock
    SetEnv PATH=/home/ubuntu/bin:$PATH

I have created SSH key pair called remote, and the pair is under my local ~/.ssh/. For SetEnv you should have that information once you installed docker rootless.

In VS Code, I have one line in my user settings.json

"docker.host": "ssh://remote",

where "remote" is the name that matches my SSH config. After these steps you should be able to attach to your running remote container.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user10229