Docker-compose named mounted volume

As you can read in this GitHub issue, mounting named volumes now is a thing … since 1.11 or 1.12.). Driver specific options are documented. Some notes from the GitHub thread:

docker volume create --opt type=none --opt device=<host path> --opt o=bind

If the host path does not exist, it will not be created.

Options are passed in literally to the mount syscall. We may add special cases for certain “types” because they are awkward to use… like the nfs example [referenced above].

– @cpuguy83

To address your specific question about how to use that in compose, you write under your volumes section:

my-named-volume:
     driver_opts:
           type: none
           device: /home/full/path #NOTE needs full path (~ doesn't work)
           o: bind

This is because as cpuguy83 wrote in the github thread linked, the options are (under the hood) passed directly to the mount command.

EDIT: As commented by…

  • …@villasv, you can use ${PWD} for relative paths.

  • …@mikeyjk, you might need to delete preexisting volumes:

     docker volume rm $(docker volume ls -q)
     OR
     docker volume prune
    
  • …@Camron Hudson, in case you have no such file or directory errors showing up, you might want to read this SO question/ answer as Docker does not follow symlinks and there might be permission issues with your local file system.

Leave a Comment