Docker bind Mounts和volume及tmpfs对比

本文总结了 bind Mounts 和 volume 及 tmpfs 三种 Docker 管理容器数据方式的区别

bind mounts

根据官方文档的介绍:

bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine. By contrast, when you use a volume, a new directory is created within Docker’s storage directory on the host machine, and Docker manages that directory’s contents.

bind mounts相比volume功能较为有限,创建一个 bind mount 之后,Host 上的一个文件/文件夹就相当于被挂载到容器里了,该文件/文件夹就被容器通过绝对路径引用了,意味着这个文件/文件夹必须实现存在于 Host 上

而使用使用volume时,会在 Host 主机上新建一个Docker 存储文件夹,文件夹里的内容由 Docker 管理。

The file 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 very performant, but they rely on the host machine’s filesystem having a specific directory structure available. If you are developing new Docker applications, consider using named volumes instead.

volume映射的文件/文件夹不需要存在于 Host 上,它们是按需生成的,即可以被自动创建,不需要实现在 Host 上创建。

虽然bind mounts很高效,但是它依赖 Host 上存在特定的目录结构,这对于部署到新主机上显然不是一件好事,所以推荐新应用使用volume

volume

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.

volumebind mounts的区别在于后者依赖于 Host 上有特定的目录结构,而前者不需要,使用 volume 时文件的管理由 Docker 主导而不是 Host。

文档还提到了volume相比于bind mounts的优点:

  • 更易于备份/转移
  • 同时可用于 Linux/Windows 容器
  • 在多个容器间共享更安全
  • volume驱动能够让你在远程主机或云服务器上存储volume,还能实现数据加密等功能
  • 新容器可以提前填充内容到volume
  • 在 Mac 和 Windows 主机里volume性能比bind mounts好的多

若容器产生的数据不需要持久化,可以考虑使用tmpfs来避免持久化数据和写入容器层(writable layer),从而提高性能

tmpfs

tmpfs是将文件写到内存中,可以避免写数据到容器层增加容器大小。

tmpfs有两个缺点:

  • 只能在 Linux Host 上使用
  • 不能在多个容器间共享

下图解释了三者的关系: 

猜你喜欢

转载自blog.csdn.net/smilejiasmile/article/details/125096530