gpt4 book ai didi

docker 数据量 vs 挂载的主机目录

转载 作者:IT老高 更新时间:2023-10-28 12:34:49 26 4
gpt4 key购买 nike

我们可以在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
}
]
...

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

最佳答案

虽然使用卷和绑定(bind)挂载的感觉相同(唯一的变化是目录的位置),但在行为上存在差异。

卷与绑定(bind)安装

  • 使用绑定(bind)挂载,主机上的文件或目录被挂载到容器中。文件或目录由其在主机上的完整路径或相对路径引用。
  • 使用 Volume,在主机上在 Docker 的存储目录中创建一个新目录,Docker 管理该目录的内容。

相对于绑定(bind)安装的卷优势:

  • 卷比绑定(bind)挂载更容易备份或迁移。
  • 您可以使用 Docker CLI 命令或 Docker API 管理卷。
  • 卷适用于 Linux 和 Windows 容器。
  • 可以在多个容器之间更安全地共享卷。
  • 卷驱动程序允许您将卷存储在远程主机或云提供商上、加密卷的内容或添加其他功能。
  • 容器可以预先填充新卷的内容。

编辑(9.9.2019):
根据@Sebi2020 评论,绑定(bind)挂载更容易备份。 Docker 不提供任何命令来备份卷。您必须使用带有绑定(bind)挂载的临时容器来创建备份。

音量

Created and managed by Docker. You can create a volume explicitlyusing the docker volume create command, or Docker can create a volumeduring container or service creation.

When you create a volume, it is stored within a directory on theDocker host. When you mount the volume into a container, thisdirectory is what is mounted into the container. This is similar tothe way that bind mounts work, except that volumes are managed byDocker and are isolated from the core functionality of the hostmachine.

A given volume can be mounted into multiple containers simultaneously.When no running container is using a volume, the volume is stillavailable to Docker and is not removed automatically. You can removeunused volumes using docker volume prune.

When you mount a volume, it may be named or anonymous. Anonymousvolumes are not given an explicit name when they are first mountedinto a container, so Docker gives them a random name that isguaranteed to be unique within a given Docker host. Besides the name,named and anonymous volumes behave in the same ways.

Volumes also support the use of volume drivers, which allow you tostore your data on remote hosts or cloud providers, among otherpossibilities.

enter image description here

绑定(bind)坐骑

Available since the early days of Docker. Bind mounts have limitedfunctionality compared to volumes. When you use a bind mount, a fileor directory on the host machine is mounted into a container. The fileor directory is referenced by its full path on the host machine. Thefile or directory does not need to exist on the Docker host already.It is created on demand if it does not yet exist. Bind mounts are veryperformant, but they rely on the host machine’s filesystem having aspecific directory structure available. If you are developing newDocker applications, consider using named volumes instead. You can’tuse Docker CLI commands to directly manage bind mounts.

enter image description here

还有tmpfs mounts
tmpfs 挂载

A tmpfs mount is not persisted on disk, either on the Docker host orwithin a container. It can be used by a container during the lifetimeof the container, to store non-persistent state or sensitiveinformation. For instance, internally, swarm services use tmpfs mountsto mount secrets into a service’s containers.
enter image description here

引用:
https://docs.docker.com/storage/

关于docker 数据量 vs 挂载的主机目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34357252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com