Docker-compose named mounted volume

2020-02-02 06:16发布

In order to keep track of the volumes used by docker-compose i 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 cant 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?

5条回答
姐就是有狂的资本
2楼-- · 2020-02-02 06:48

I was looking for an answer to the same question recently and stumbled on this plugin: https://github.com/CWSpear/local-persist Looks like it allows just what topic started wants to do.

Haven't tried it myself yet, but thought it might be useful for somebody.

查看更多
贪生不怕死
3楼-- · 2020-02-02 06:49

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…

  • …@visslav, you can use ${PWD} for relative paths.
  • …@mikeyjk, you might need to delete preexisting volumes:

    docker volume rm $(docker volume ls)
    
  • …@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.

查看更多
放荡不羁爱自由
4楼-- · 2020-02-02 06:52

OP appears to be using full paths already, but if like most people you're interested in mounting a project folder inside the container this might help.

This is how to do it with driver_opts like @kaiser said and @linuxbandit exemplified. But you can try to use the usually available environment variable $PWD to avoid specifying full paths for directories in the docker-compose context:

logs-directory:
  driver_opts:
    type: none
    device: ${PWD}/logs
    o: bind
查看更多
神经病院院长
5楼-- · 2020-02-02 06:55

Host volumes are different from named volumes or anonymous volumes. Their "name" is the path on the host.

There is no way to use the volumes section for host volumes.

查看更多
Anthone
6楼-- · 2020-02-02 07:08

I've been trying the (almost) same thing and it seems to work with something like :

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

Seems to work for me (I didn't dig into it, just tested it).

查看更多
登录 后发表回答