docker 数据量与挂载的主机目录 [英] docker data volume vs mounted host directory
问题描述
我们可以在 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
.
最后,我们传递了一个命令,该命令使用 tar
将 dbdata
卷的内容备份到我们的 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 usestar
to backup the contents of thedbdata
volume to abackup.tar
file inside our/backup
directory. When the command completes and the container stops we’ll be left with a backup of ourdbdata
volume.
这篇关于docker 数据量与挂载的主机目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!