2

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?
    

    Error message when Trying Attach Visual Studio Code

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

$ echo $DOCKER_HOST
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.


Update 2023-03-06:

When running docker context use rootless I get the following output:

docker context use rootless

And the VSCode Docker extension shows the following: VSCode Docker extension - Contexts

But when I run then run docker context ls in the command line I get the following output:

docker context ls

But the value of DOCKER_HOST is set to the rootless context.

$ echo DOCKER_HOST
unix:///run/user/1001/docker.sock

When running docker ps it lists the container running in the rootless context.

Is this the reason it does not work as expected?


Workaround - 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?

ai2ys
  • 1,151
  • 10
  • 21

3 Answers3

1

I had a similar issue running rootless docker. It seemed it was not honoring environment variables in ~/.bashrc (like DOCKER_HOST)

In the logs I found that it was loading /bin/sh, rather than /bin/bash. In this case I am using a managed login user that does not appear in local /etc/passwd. Moving applicable environment variables into ~/.profile fixed my issues.

# container logs from vscode
[13689 ms] userEnvProbe: loginInteractiveShell (default)
[13689 ms] userEnvProbe: not found in cache
[13690 ms] userEnvProbe shell: /bin/sh
firien
  • 1,548
  • 16
  • 23
  • Thanks for your answer. I tried it, but it did not work out, perhaps I made a mistake. But it helped me to find a solution that works on my system: https://stackoverflow.com/a/75749619/11687201 – ai2ys Mar 15 '23 at 20:12
  • Now I know what went wrong. `$UID` won't work in `sh`. Therefore the export for `DOCKER_HOST` to be added dumped from the rootless installation won't work `export DOCKER_HOST=unix:///run/user/$UID/docker.sock` . If this is changed to `export DOCKER_HOST=unix:///run/user/$(id -u $USER)/docker.sock` instead when adding it to the `~/.profile` it works as well. – ai2ys Mar 15 '23 at 21:05
0

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.

  • It did not work out on my machine, I am still getting the same error. I was already using a ssh-key-pair. A drawback with editing "docker.host" in the "settings.json" is, that the local docker context on my machine will be invisble and cannot be selected anymore. – ai2ys Jun 13 '22 at 08:54
  • Are you using this for using "Attach Visual Studio Code" or only for "Attach Shell"? – ai2ys Jun 13 '22 at 08:56
  • I can use VS Code "Run and Debug" functionality. So I think that's "Attach Visual Studio Code". – user10229 Jun 20 '22 at 21:39
0

The problem can be solved by applying the solution from the answer to another question: "answer for .bashrc at ssh login"

I followed the instructions and added the following to ~/.bash_profile:

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

Be sure that the export for DOCKER_HOST being added to the ~/.bashrc file does not contain $UID, but $(id -u $USER) instead, because $UID will not work in sh.

# inside the ~/.bashrc
export DOCKER_HOST=unix:///run/user/$(id -u $USER)/docker.sock

Now Attach Visual Studio Code works within the VSCode window being connected via SSH to the remote system as expected.

  1. Connect to remote using VSCode remote explorer
  2. Start container
  3. Attach to running container using Visual Studio Code Attach either from the context menu of the Docker extension or using CMD/CTRL+SHIFT+P selecting >Dev Containers: Attach to running containers
ai2ys
  • 1,151
  • 10
  • 21