Rootless Docker(远程系统)和 VS Code - 将 VS Code 附加到容器失败

发布于 2025-01-11 10:41:20 字数 2143 浏览 0 评论 0 原文

我正在使用以下设置

  • 远程系统

    • 运行“无根 Docker”
    • 名为“rootless”的 Docker 上下文处于活动状态
    • 正在安装 VS Code Docker 扩展
  • VS Code - 使用“远程扩展”通过 SSH 连接到远程计算机

    • 使用 rootless Docker 构建和运行 Docker 容器
    • 检查是否选择了“rootless”Docker 上下文
    • 尝试在容器“附加 Visual Studio Code”上使用“右键单击”选项,该选项将失败并显示以下错误消息:
    无法连接到位于 unix:///var/run/docker.sock 的 Docker 守护进程。 docker 守护进程是否正在运行?
    

    “尝试附加

VS Code 尝试引用“默认”Docker 上下文,尽管“选择“rootless”Docker 上下文,在我的情况下是:

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

此外,当“docker.host”设置为 rootless Docker 之一时,或者如果“docker.context”设置为“rootless”,使用“Attach Visual”工作室代码”将失败并出现相同的错误消息。


更新 2023-03-06:

运行 docker context use rootless 时,我得到以下输出:

docker context use rootless

VSCode Docker 扩展显示以下内容: VSCode Docker扩展 - Contexts

但是当我运行然后在命令行中运行 docker context ls 时,我得到以下输出:

docker context ls

但是DOCKER_HOST的值被设置为无根上下文。

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

运行 docker ps 时,它会列出在无根上下文中运行的容器。

这是它没有按预期工作的原因吗?


解决方法 - 解决方法如下:

  • 在本地计算机上为远程定义 Docker 上下文 docker context create ; --docker host="ssh://@
  • VS Code - 连接到本地计算机
    • 选择远程计算机的 Docker 上下文
    • 使用“附加 Visual Studio Code”附加到远程容器

有人知道如何解决该问题,以便可以直接从连接的 VS Code 窗口使用“附加 Visual Studio Code”通过 ssh 到远程系统?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花心好男孩 2025-01-18 10:41:20

我在运行无根docker时遇到了类似的问题。似乎它没有遵守 ~/.bashrc 中的环境变量(如 DOCKER_HOST),

在日志中我发现它正在加载 /bin/sh >,而不是 /bin/bash。在本例中,我使用的托管登录用户未出现在本地 /etc/passwd 中。将适用的环境变量移至 ~/.profile 解决了我的问题。

# container logs from vscode
[13689 ms] userEnvProbe: loginInteractiveShell (default)
[13689 ms] userEnvProbe: not found in cache
[13690 ms] userEnvProbe shell: /bin/sh

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
江南月 2025-01-18 10:41:20

我的 SSH 配置如下所示

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

我创建了名为“远程”的 SSH 密钥对,该密钥对位于我的本地 ~/.ssh/ 下。对于 SetEnv,一旦安装了 docker rootless,您就应该拥有该信息。

在 VS Code 中,我的用户 settings.json 中有一行

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

,其中“remote”是与我的 SSH 配置匹配的名称。完成这些步骤后,您应该能够附加到正在运行的远程容器。

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.

不如归去 2025-01-18 10:41:20

这个问题可以通过应用另一个问题的答案来解决:
“ssh 登录时 .bashrc 的答案”

我按照说明操作,并将以下内容添加到 ~/.bash_profile< /code>:

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

确保添加到 ~/.bashrc 文件中的 DOCKER_HOSTexport 不包含$UID,而是 $(id -u $USER),因为 $UIDsh 中不起作用。

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

现在,附加 Visual Studio Code 在 VSCode 窗口中工作,并按预期通过 SSH 连接到远程系统。

  1. 使用 VSCode 远程资源管理器连接到远程
  2. 启动容器
  3. 使用 Visual Studio Code Attach 从 Docker 扩展的上下文菜单或使用 CMD/CTRL+SHIFT+P 连接到正在运行的容器选择>Dev Containers:附加到正在运行的容器

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
涙—继续流 2025-01-18 10:41:20

就我而言,我使用 rootless 和 podman。
podman 是由 sudo 用户安装的。

所以我需要的是更改 VSCode 中的 docker 主机路径。

对于图形方式,您需要打开扩展菜单并找到已安装的扩展。点击 Docker 扩展旁边的小齿轮图标,然后选择扩展设置。接下来,您需要找到以下条目。

image1

Docker: Host 值更改为 unix :///run/user/1000/podman/podman.sock,而 1000 代表您的用户 ID。如果您不确定 1000 是否正确,请在终端中运行 id 并检查 uid= 的值。

image2

Docker: Docker Path 值更改为 /也推荐使用 usr/bin/podman

或者,按 Ctrl+Shift+P 并搜索设置。打开首选项:打开用户设置 (JSON) 条目并将以下行添加到您的配置中。

{
    "docker.host": "unix:///run/user/1000/podman/podman.sock",
    "docker.dockerPath": "/usr/bin/podman"
}

In my case, I use the rootless and podman.
The podman was installed by sudo user.

So the thing I need is change the docker host path in VSCode.

For the graphical way, you need to open the extensions menu and find the installed extension. Hit the little cogwheel icon next to the Docker Extension and select Extension Settings. Next, you need to find the following entries.

image1

Change the Docker: Host the value to unix:///run/user/1000/podman/podman.sock, whereas the 1000 represents your user id. If you are unsure if 1000 is correct, run id in a terminal and check the value for uid=.

image2

Changing the Docker: Docker Path value to /usr/bin/podman is recommended, too.

Alternatively, hit Ctrl+Shift+P and search for Settings. Open the Preferences: Open User Settings (JSON) entry and add the below lines to your configuration.

{
    "docker.host": "unix:///run/user/1000/podman/podman.sock",
    "docker.dockerPath": "/usr/bin/podman"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文