如何使用Docker的卷 - 从?映射卷路径? [英] How to map volume paths using Docker's --volumes-from?

查看:537
本文介绍了如何使用Docker的卷 - 从?映射卷路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Docker的新人,对于使用 - 卷从功能感到兴奋,但有一些我不了解的东西。



如果我想从中使用和两个仅数据的容器,每个容器都会导出名为 / srv ,如何防止卷路径碰撞?使用 [host-dir]创建绑定挂载时可以映射卷名:[container-dir] ;如何使用 - 卷 - 从



那么我想要的东西看起来像这样:

  docker run --name = DATA1 --volume = / srv busybox true 
docker run --name = DATA2 --volume = / srv busybox true
docker运行-t -i -rm --volumes-from DATA1:/ srv1 --volumes-from DATA2:/ srv2 ubuntu bash


解决方案

可以完成,但在Docker命令行界面中此时不支持。



How-to



查找卷目录

  docker检查DATA1 | grepvfs / dir
#输出如下:
#/ srv:/ var / lib / docker / vfs / dir /< long vol id&

所以,您可以自动执行此操作,并将这些目录安装在您选择的安装位置:


 #在变量中加载目录:
SRV1 = $(docker inspect DATA1 | grepvfs / dir| awk'/ (。*)/ {gsub(//,,$ 2); print $ 2}')
SRV2 = $(docker inspect DATA2 | grepvfs / dir| awk'/\" )/ {gsub(//,,$ 2); print $ 2}')

现在,通过实际目录安装这些卷,而不是--volumes-from:

  docker运行-t -i -v $ SRV1:/ srv1 -v $ SRV2:/ srv2 ubuntu bash 

IMO,功能是一样的,因为这是使用 - volumes-from 时所做的相同的事情。


I'm new to Docker and am excited about using the --volumes-from feature but there's something I'm not understanding.

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

解决方案

It can be done, but it is not supported at this moment in docker commandline interface.

How-to

Find the volumes directories:

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 }')

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, the functionality is identical, because this is the same thing that is done when using --volumes-from.

这篇关于如何使用Docker的卷 - 从?映射卷路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆