docker - 通过SSH挂载远程Docker上下文的卷

标签 docker ssh docker-compose

我正在通过带有远程上下文的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_HOST以从docker-compose访问您的远程docker主机。
从撰写文档中
Compose CLI environment variables

DOCKER_HOST

Sets the URL of the docker daemon. As with the Docker client, defaults to unix:///var/run/docker.sock.


在给定的情况下,docker context use remote将当前上下文设置为remote
仅适用于您的docker命令。 docker-compose仍然使用您的默认(本地)上下文。为了使docker-compose能够检测到它,您必须通过DOCKER_HOST环境变量传递它。
例:
$ export DOCKER_HOST=ssh://user@my.remote.host
$ docker-compose up

关于docker - 通过SSH挂载远程Docker上下文的卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62811751/

相关文章:

networking - 我可以将 Docker 端口仅暴露给另一个 Docker(而不是主机)吗?

docker - Docker-compose:exec用户进程导致 “no such file or directory”但文件存在

docker - 如何使用非 super 用户身份以Cron身份将Alpine图像写入文件的权限修复为可访问卷

Hadoop集群无密码ssh设置

bash - SSH while read line 循环而不使用远程命令

docker - 为docker和compose文件指定端口

git - TFS Build 在推送到远程代表时挂起

postgresql - Postgres, Go Docker compose wait-for-it.sh 没有那个文件或目录

docker - 如何使用状态管理设置 dapr 和 docker compose

docker - 没有docker-machine的docker-compose swarm