与主机共享目录或卷与容器 [英] Share directory or volume with container from host
问题描述
我有一个目录(可能是以后的卷),我想与我的所有交互式容器共享。我知道,原生Docker卷存储在 / var / lib / docker / volumes
和 docker run -v
最简单的方法,但我认为数据卷容器
是一个更加标准化的方式。我不知道如何从目录或现有的其他卷创建此卷容器。可能是错误的方法?
I have a directory(maybe later volume), that I would like to share with all my interactive containers. I know, that native Docker volumes are stored under /var/lib/docker/volumes
and docker run -v
seems the easiest way, but I think Data Volume Container
is a much more standardized way. I don't know, how to create this volume container from a directory or an existing another volume. Maybe is it wrong method?
推荐答案
有两种创建和共享卷的方法:1.使用 Dockerfile
中的VOLUME 指令。 2在容器运行期间指定 -v< volume_name>
选项,然后使用 - volumes-from =< container>
每个随后的容器需要共享数据。这是一个ex与以后:
There are two ways to create and share volumes: 1. using the VOLUME
instruction on the Dockerfile
. 2 Specifying the -v <volume_name>
option during container runtime and later using --volumes-from=<container>
with every subsequent container which need to share the data. Here is an ex with the later:
- 启动您的第一个容器与
-v
,然后在共享卷的目录下添加一个测试文件。
- Start your first container with
-v
, then add a test file under the directory of the shared volume.
docker run -it -v /test-volume --name=testimage1 ubuntu:14.04 /bin/bash
root@ca30f0f99401:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test-volume ===> test-volume dir got created here
root@ca30f0f99401:/# touch test-volume/1
root@ca30f0f99401:/# cat > test-volume/1
Test Message!
- 从主机操作系统,您可以通过检查您的容器来获取卷的详细信息:
docker inspect ca30f0f99401 | grep -i -color -E'^ | Vol'
"Mounts":
{
"Name": "025835b8b47d282ec5f27c53b3165aee83ecdb626dc36b3b18b2e128595d9134",
"Source": "/var/lib/docker/volumes/025835b8b47d282ec5f27c53b3165aee83ecdb626dc36b3b18b2e128595d9134/_data",
"Destination": "/test-volume",
"Driver": "local",
"Mode": "",
"RW": true
"Image": "ubuntu:14.04",
"Volumes": {
"/test-volume": {} }
- 使用共享卷启动另一个容器,并检查共享文件夹/文件是否存在。
$ docker run -it --name=testimage2 --volumes-from=testimage1 ubuntu:14.04 /bin/bash
root@60ff1dcebc44:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys test-volume tmp usr var
root@60ff1dcebc44:/# cat test-volume/1
Test Message!
- Goto第3步与新集装箱共享卷。
这篇关于与主机共享目录或卷与容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!