【问题标题】:Mount volume for remote Docker context via SSH通过 SSH 为远程 Docker 上下文挂载卷
【发布时间】:2020-10-14 19:30:20
【问题描述】:

我正在通过docker-compose 使用远程上下文部署一些 Docker 服务。我将其配置为使用 SSH:

docker context create remote --docker "host=ssh://user@my.remote.host"
docker context use remote

在远程主机上,我有多个要挂载到 Docker 的配置文件。当我尝试使用docker CLI 时,它工作正常:

docker run -v /home/user/run:/test -it alpine:3.11

# ls -la /test
-> shows remote files correctly here

但是当我使用带有配置文件的docker-compose 启动它时:

version: "3.3"
services:
  nginx:
    image: nginx:1.17.10-alpine
    container_name: nginx
    restart: unless-stopped
    volumes:
      - ${HOME}/run/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - "80:80"
      - "443:443"

由于某种原因,它试图挂载本地文件而不是远程文件,但失败并出现错误:

ERROR: for nginx  Cannot start service nginx: OCI runtime create failed: container_linux.go:296: starting container process caused "process_linux.go:398: container init caused \"rootfs_linux.go:58: mounting \\\"/home/local-user/run/nginx.conf\\\" to rootfs \\\"/hdd/docker/overlay2/c869ef9f2c983d33245fe1b4360eb602d718786ba7d0245d36c40385f7afde65/merged\\\" at \\\"/hdd/docker/overlay2/c869ef9f2c983d33245fe1b4360eb602d718786ba7d0245d36c40385f7afde65/merged/etc/nginx/nginx.conf\\\" caused \\\"not a directory\\\"\"": unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

是否可以通过类似于标准 Docker CLI 的docker-compose 挂载远程资源?

【问题讨论】:

  • 如果您在远程系统上使用正确的绝对路径,我希望这会起作用;我希望在尝试联系 Docker 之前,会在本地扩展像 $HOME 这样的环境变量。将docker-compose.yml 复制到远程计算机并远程运行整个docker-compose up 命令可能会更健壮一些。

标签: docker ssh docker-compose


【解决方案1】:

您需要显式设置 DOCKER_HOST 以从 docker-compose 访问您的远程 docker 主机。

来自撰写文档

Compose CLI environment variables

DOCKER_HOST

设置 docker 守护进程的 URL。与 Docker 客户端一样,默认值 到 unix:///var/run/docker.sock。

在您给定的情况下,docker context use remote 将当前上下文设置为远程 仅适用于您的 docker 命令。 docker-compose 仍然使用您的默认(本地)上下文。为了让 docker-compose 检测到它,您必须通过 DOCKER_HOST 环境变量传递它。

例子:

$ export DOCKER_HOST=ssh://user@my.remote.host
$ docker-compose up

【讨论】:

    最近更新 更多