Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose named mounted volume

In order to keep track of the volumes used by docker-compose, I'd like to use named volumes. This works great for 'normal' volumes like

version: 2 services:    example-app:     volume:       -named_vol:/dir/in/container/volume volumes:   named_vol: 

But I can't figure out how to make it work when mounting the local host. I'm looking for something like:

version: 2 services:    example-app:     volume:       -named_homedir:/dir/in/container/volume volumes:   named_homedir: /c/Users/ 

or

version: 2 services:    example-app:     volume:       -/c/Users/:/home/dir/in/container/ --name named_homedir 

is this in any way possible or am I stuck with anonymous volumes for mounted ones?

like image 993
pvgoddijn Avatar asked Mar 07 '16 10:03

pvgoddijn


People also ask

What is named volume in Docker?

Named volumes Named volumes can persist data after we restart or remove a container. Also, it's accessible by other containers. These volumes are created inside /var/lib/docker/volume local host directory.

Where are Docker named volumes stored?

Volumes are stored in a part of the host filesystem which is managed by Docker ( /var/lib/docker/volumes/ on Linux).

What does Docker-compose volumes do?

When you execute a docker-compose command, the volumes directive in docker-compose. yml file mounts source directories or volumes from your computer at target paths inside the container. If a matching target path exists already as part of the container image, it will be overwritten by the mounted path.

Does Docker-compose create volumes?

We can also create a volume and then use it with a container using the -v flag. Docker-compose allows us to use volumes that are either existing or new.


1 Answers

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.

like image 192
kaiser Avatar answered Sep 21 '22 21:09

kaiser