Docker组成的命名挂载卷 [英] Docker-compose named mounted volume
问题描述
为了跟踪docker-compose使用的卷,我想使用命名卷。这对于普通卷非常有用,例如
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/
或
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?
推荐答案
您可以在GitHub问题中阅读,安装命名卷 现在是一个东西……从1.11或1.12开始。) 特定于驱动程序的选项已记录在案。 GitHub线程上的一些注释:
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.
选项将按字面值传递给mount syscall。我们可能为某些类型添加特殊情况,因为它们使用起来很笨拙……就像nfs示例[上面引用]。
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
这是因为cpuguy83在链接的github线程中写道,选项(在后台)直接传递给 mount
命令。
This is because as cpuguy83 wrote in the github thread linked, the options are (under the hood) passed directly to the mount
command.
编辑:评论者:…
- …@ villasv,您可以使用
$ {PWD}
作为相对路径。 -
…@ mikeyjk,您可能需要删除先前存在的卷:
- …@villasv, you can use
${PWD}
for relative paths. …@mikeyjk, you might need to delete preexisting volumes:
docker volume rm $(docker volume ls)
... @ Camron Hudson,如果您没有这样的文件或目录
错误出现,您可能要阅读此SO问题/答案,因为Docker不遵循符号链接,并且本地文件系统可能存在权限问题。
…@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.
这篇关于Docker组成的命名挂载卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!