我正在通过带有远程上下文的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/