docker 数据量与挂载的主机目录 [英] docker data volume vs mounted host directory

查看:26
本文介绍了docker 数据量与挂载的主机目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在 docker 中拥有一个数据卷:

$ docker run -v/path/to/data/in/container --name test_container debian$ docker 检查 test_container...坐骑": [{"名称": "fac362...80535","来源": "/var/lib/docker/volumes/fac362...80535/_data","目的地": "/path/to/data/in/container","司机": "本地",模式": "",RW":真}]...

但是如果数据卷存在于/var/lib/docker/volumes/fac362...80535/_data,这与使用挂载的文件夹中的数据有什么不同-v/path/to/data/in/container:/home/user/a_good_place_to_have_data?

解决方案

这与使用 -v/path/to/data/in/container:/home/user/a_good_place_to_have_data 挂载文件夹中的数据有什么不同吗?

这是因为,如将主机目录挂载为数据卷"

<块引用>

主机目录本质上是依赖于主机的.出于这个原因,你不能从 Dockerfile 挂载主机目录,因为构建的图像应该是可移植的.并非所有潜在主机上都可以使用主机目录.

如果你有一些持久化数据想要在容器之间共享,或者想要从非持久化容器中使用,最好创建一个命名的数据卷容器,然后从中挂载数据.

您可以结合两种方法:

 docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf/backup/backup.tar/dbdata

<块引用>

在这里,我们启动了一个新容器并从 dbdata 容器装载了卷.
然后我们将本地主机目录挂载为 /backup.
最后,我们传递了一个命令,该命令使用 tardbdata 卷的内容备份到我们的 backup.tar 文件中>/backup 目录.当命令完成且容器停止时,我们将保留 dbdata 卷的备份.

We can have a data volume in docker:

$ docker run -v /path/to/data/in/container --name test_container debian
$ docker inspect test_container
...
Mounts": [
    {
        "Name": "fac362...80535",
        "Source": "/var/lib/docker/volumes/fac362...80535/_data",
        "Destination": "/path/to/data/in/container",
        "Driver": "local",
        "Mode": "",
        "RW": true
    }
]
...

But if the data volume lives in /var/lib/docker/volumes/fac362...80535/_data, is it any different from having the data in a folder mounted using -v /path/to/data/in/container:/home/user/a_good_place_to_have_data?

解决方案

is it any different from having the data in a folder mounted using -v /path/to/data/in/container:/home/user/a_good_place_to_have_data?

It is because, as mentioned in "Mount a host directory as a data volume"

The host directory is, by its nature, host-dependent. For this reason, you can’t mount a host directory from Dockerfile because built images should be portable. A host directory wouldn’t be available on all potential hosts.

If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it’s best to create a named Data Volume Container, and then to mount the data from it.

You can combine both approaches:

 docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata

Here we’ve launched a new container and mounted the volume from the dbdata container.
We’ve then mounted a local host directory as /backup.
Finally, we’ve passed a command that uses tar to backup the contents of the dbdata volume to a backup.tar file inside our /backup directory. When the command completes and the container stops we’ll be left with a backup of our dbdata volume.

这篇关于docker 数据量与挂载的主机目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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