问题描述
我是 Docker 的新手,对使用 --volumes-from
功能感到很兴奋,但有些地方我不明白.
I'm new to Docker and am excited about using the --volumes-from
feature but there's something I'm not understanding.
如果我想将 --volumes-from
与两个纯数据容器一起使用,每个容器都导出名为 /srv
的卷,如何防止卷路径从碰撞?我可以在使用 [host-dir]:[container-dir]
创建绑定安装时映射卷名;我如何使用 --volumes-from
做到这一点?
If I want to use --volumes-from
with two data-only containers, each of which exports volumes named /srv
, how to I prevent the volume paths from colliding? I can map volume names when creating a bind mount using [host-dir]:[container-dir]
; how do I do that with --volumes-from
?
所以我想要的看起来像这样:
So what I want would look something like this:
docker run --name=DATA1 --volume=/srv busybox true
docker run --name=DATA2 --volume=/srv busybox true
docker run -t -i -rm --volumes-from DATA1:/srv1 --volumes-from DATA2:/srv2 ubuntu bash
推荐答案
可以做到,但是目前docker命令行界面不支持.
It can be done, but it is not supported at this moment in docker commandline interface.
查找卷目录:
docker inspect DATA1 | grep "vfs/dir"
# output something like:
# "/srv": "/var/lib/docker/vfs/dir/<long vol id>"
因此,您可以自动执行此操作,并在您选择的挂载点挂载这些目录:
So, you can automate this, and mount these directories at mount points of your choice:
# load directories in variables:
SRV1=$(docker inspect DATA1 | grep "vfs/dir" | awk '/"(.*)"/ { gsub(/"/,"",$2); print $2 }')
SRV2=$(docker inspect DATA2 | grep "vfs/dir" | awk '/"(.*)"/ { gsub(/"/,"",$2); print $2 }')
现在,通过真实目录而不是 --volumes-from 挂载这些卷:
now, mount these volumes by real directories instead of the --volumes-from:
docker run -t -i -v $SRV1:/srv1 -v $SRV2:/srv2 ubuntu bash
IMO,功能是相同的,因为这与使用 --volumes-from
时所做的相同.
IMO, the functionality is identical, because this is the same thing that is done when using --volumes-from
.
这篇关于如何使用 Docker 的 --volumes-from 映射卷路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!