Mounting a single file from an NFS docker volume i

2020-04-20 08:23发布

问题:

Example (many options omitted for brevity):

version: "3"
volumes:
  traefik:
    driver: local
    driver_opts:
      type: nfs
      o: "addr=192.168.1.100,soft,rw,nfsvers=4,async"
      device: ":/volume/docker/traefik"
services:
  traefik:
    volumes:
      - traefik/traefik.toml:/traefik.toml

This errors out as there is no volume with the name traefik/traefik.toml meaning that the volume name must be the full path to the file (i.e. you can't append a path to the volume name)?

Trying to set device: ":/volume/docker/traefik/traefik.toml" just returns a not a directory error.

Is there a way to take a single file and mount it into a container?

回答1:

You cannot mount a file or sub-directory within a named volume, the source is either the named volume or a host path. NFS itself, along with most filesystems you'd mount in Linux, require you to mount an entire filesystem, not a single file, and when you get down to the inode level, this is often a really good thing.

The options remaining that I can think of are to mount the entire directory somewhere else inside your container, and symlink to the file you want. Or to NFS mount the directory to the host and do a host mount (bind mount) to a specific file.

However considering the example you presented, using a docker config would be my ideal solution, removing the NFS mount entirely, and getting a read only copy of the file that's automatically distributed to whichever node is running the container.

More details on configs: https://docs.docker.com/engine/swarm/configs/



回答2:

I believe I found the issue!

Wrong:

   volumes:
      - traefik/traefik.toml:/traefik.toml

Correct:

   volumes:
      - /traefik/traefik.toml:/traefik.toml      

Start the volume with "/"