Mount volume for remote Docker context via SSH

Viewed 2596

I'm deploying few Docker services via docker-compose with remote context. I configured it to use SSH:

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

On the remote host I have multiple configuration files which I want to mount into the Docker. It's working fine when I'm trying with docker CLI:

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

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

But when I'm starting it using docker-compose with config file:

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"

It's trying to mount local files instead of remote for some reason and fails with error:

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

Is it possible to mount remote resources via docker-compose similar to standard Docker CLI?

1 Answers

You need to explicitly set DOCKER_HOST to access your remote docker host from docker-compose.

From the compose documentation

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.

In your given case, docker context use remote sets current context to remote only for your docker command. docker-compose still uses your default (local) context. In order for docker-compose to detect it, you must pass it via the DOCKER_HOST environment variable.

Example:

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