docker 卷类型 - 绑定与卷
docker volume type - bind vs volume
TLDR
与docker-compose
有什么区别
volumes:
- type: volume
source: mydata
target: /data
和
volumes:
- type: bind
source: mydata
target: /data
?
长题:
当您在 docker-compose
文件中指定 volumes
选项时,您可以使用 long-syntax style
根据文档,type
选项接受 3 个不同的值:volume
、bind
和 tmpfs
:
我了解 tmpfs
选项 - it means that the volume will not be saved after the container is down.。
但是我在文档中找不到任何关于其他两个选项之间区别的参考资料:bind
和 volume
,有人可以启发我吗?
当绑定装载是来自主机的文件时,卷更像是 docker 的 nas。
- 绑定装载是从主机(运行 docker 守护进程的主机)装载到容器中的文件。
- 卷就像完全由 docker 管理的存储空间。
在文献中,您会发现两种类型的卷:
- 命名卷(您提供它的名称)
- 匿名卷(来自 docker 的常用 UUID 名称,就像您可以在容器或未标记的图像上找到它们一样)
这些卷有自己的一套 docker commands;您也可以通过
查阅此列表
docker volume --help
您可以通过
查看您现有的卷
docker volume ls
您可以通过
创建命名卷
docker volume create my_named_volume
但您也可以通过 docker-compose
文件创建卷
version: "3.3"
services:
mysql:
image: mysql
volumes:
- type: volume
source: db-data
target: /var/lib/mysql/data
volumes:
db-data:
这是说 请 docker 的部分,在容器目录 [=67] 顶部安装名为 db-data 的卷=]
- type: volume
source: db-data
target: /var/lib/mysql/data
这是对 docker 请创建一个名为 db-data[=22= 的卷的部分]
volumes:
db-data:
Docker 关于三种安装类型的文档:
如果我没理解错的话,换句话说,你是在问:Volumes 和 bind mounts 有什么区别?
主机管理和隔离的区别
绑定装载 存在于主机文件系统上并由主机维护者管理。
Docker 之外的应用程序/进程也可以修改它。
Volumes 也可以在主机上实现,但是 Docker 会为我们管理它们并且它们不能在 Docker 之外访问。
卷是更广泛的解决方案
虽然这两种解决方案都帮助我们将数据生命周期与容器分离,
通过使用 Volumes,您的系统将获得更多的功能和灵活性。
使用 Volumes 我们可以有效地设计我们的数据并通过将其存储在专用的远程位置(例如云)并将其与系统集成来将其与主机和系统的其他部分分离备份、监控、加密和硬件管理等外部服务。
与绑定挂载相比更多卷优势:
- 没有主机问题。
- 可以使用 Docker CLI 进行管理。
- 卷可以为您节省一些 uid/gid 问题相关权限,这些问题在容器用户的 uid 与主机不匹配时发生
gid
。
- 新卷的内容可以由容器预填充。
例子
让我们来看两个场景。
案例一:Web服务器。
我们想为我们的 Web 服务器提供一个可能经常更改的配置文件。
例如:根据当前环境暴露端口。
我们可以每次使用相关设置重建图像,或者为每个环境创建 2 个不同的图像。这两种解决方案都不是很有效。
With Bind mounts Docker 将给定的源目录挂载到容器内的某个位置。
(联合文件系统内部只读层中的原始目录/文件将被简单地覆盖)。
例如 - 将动态端口绑定到 nginx:
version: "3.7"
services:
web:
image: nginx:alpine
volumes:
- type: bind #<-----Notice the type
source: ./mysite.template
target: /etc/nginx/conf.d/mysite.template
ports:
- "9090:8080"
environment:
- PORT=8080
command: /bin/sh -c "envsubst < /etc/nginx/conf.d/mysite.template >
/etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
(*) 请注意,此示例也可以使用卷来解决。
案例 2:数据库。
Docker 容器不存储持久数据:一旦容器停止,任何将写入容器联合文件系统中可写层的数据都将丢失 运行。
但是如果我们在容器上有一个数据库 运行 并且容器停止了怎么办 - 这意味着所有数据都将丢失?
卷 来救援。
这些被命名为文件系统树,由 Docker.
为我们管理
例如 - 持久化 Postgres SQL 数据:
services:
db:
image: postgres:latest
volumes:
- "dbdata:/var/lib/postgresql/data"
volumes:
- type: volume #<-----Notice the type
source: dbdata
target: /var/lib/postgresql/data
volumes:
dbdata:
请注意,在这种情况下,对于命名卷,源是卷的名称
(对于匿名卷,省略此字段)。
Feature
Bind
Volume
Internal soul
Bind mounts attach a user-specified location on host filesystem to a specific point in a container file tree.
Volume attach with disk storage on the host filesystem or cloud storage.
command
--mount type=bind,src="",dst=""
Docker CLI
docker volume
command
Dependency
dependent on location on to the host filesystem.
Container-independent data management
Separation of concerns
No
Yes
Conflict with other containers
Yes Example: multiple instances of Cassandra that all use the same host location as a bind mount for data storage. In that case, each of the instances would compete for the same set of files. Without other tools such as file locks, that would likely result in corruption of the database.
No. By default, Docker creates volumes by using the local
volume plugin.
When to choose
1- Bind mounts are useful when the host provides a file or directory that is needed by a program running in a container, or when that containerized program produces a file or log that is processed by users or programs running outside containers.
2- appropriate tools for workstations, machines with specialized concerns
3- systems with more traditional configuration management tooling.
Working with Persistent storage
1. Databases
2. Cloud storage
When not to choose
Better to avoid these kinds of specific bindings in generalized platforms or hardware pools.
To be written
TLDR
与docker-compose
有什么区别
volumes:
- type: volume
source: mydata
target: /data
和
volumes:
- type: bind
source: mydata
target: /data
?
长题:
当您在 docker-compose
文件中指定 volumes
选项时,您可以使用 long-syntax style
根据文档,type
选项接受 3 个不同的值:volume
、bind
和 tmpfs
:
我了解 tmpfs
选项 - it means that the volume will not be saved after the container is down.。
但是我在文档中找不到任何关于其他两个选项之间区别的参考资料:bind
和 volume
,有人可以启发我吗?
当绑定装载是来自主机的文件时,卷更像是 docker 的 nas。
- 绑定装载是从主机(运行 docker 守护进程的主机)装载到容器中的文件。
- 卷就像完全由 docker 管理的存储空间。
在文献中,您会发现两种类型的卷:- 命名卷(您提供它的名称)
- 匿名卷(来自 docker 的常用 UUID 名称,就像您可以在容器或未标记的图像上找到它们一样)
这些卷有自己的一套 docker commands;您也可以通过
查阅此列表docker volume --help
您可以通过
查看您现有的卷docker volume ls
您可以通过
创建命名卷docker volume create my_named_volume
但您也可以通过 docker-compose
文件创建卷
version: "3.3"
services:
mysql:
image: mysql
volumes:
- type: volume
source: db-data
target: /var/lib/mysql/data
volumes:
db-data:
这是说 请 docker 的部分,在容器目录 [=67] 顶部安装名为 db-data 的卷=]
- type: volume
source: db-data
target: /var/lib/mysql/data
这是对 docker 请创建一个名为 db-data[=22= 的卷的部分]
volumes:
db-data:
Docker 关于三种安装类型的文档:
如果我没理解错的话,换句话说,你是在问:Volumes 和 bind mounts 有什么区别?
主机管理和隔离的区别
绑定装载 存在于主机文件系统上并由主机维护者管理。
Docker 之外的应用程序/进程也可以修改它。
Volumes 也可以在主机上实现,但是 Docker 会为我们管理它们并且它们不能在 Docker 之外访问。
卷是更广泛的解决方案
虽然这两种解决方案都帮助我们将数据生命周期与容器分离, 通过使用 Volumes,您的系统将获得更多的功能和灵活性。
使用 Volumes 我们可以有效地设计我们的数据并通过将其存储在专用的远程位置(例如云)并将其与系统集成来将其与主机和系统的其他部分分离备份、监控、加密和硬件管理等外部服务。
与绑定挂载相比更多卷优势:
- 没有主机问题。
- 可以使用 Docker CLI 进行管理。
- 卷可以为您节省一些 uid/gid 问题相关权限,这些问题在容器用户的 uid 与主机不匹配时发生
gid
。 - 新卷的内容可以由容器预填充。
例子
让我们来看两个场景。
案例一:Web服务器。
我们想为我们的 Web 服务器提供一个可能经常更改的配置文件。
例如:根据当前环境暴露端口。
我们可以每次使用相关设置重建图像,或者为每个环境创建 2 个不同的图像。这两种解决方案都不是很有效。
With Bind mounts Docker 将给定的源目录挂载到容器内的某个位置。
(联合文件系统内部只读层中的原始目录/文件将被简单地覆盖)。
例如 - 将动态端口绑定到 nginx:
version: "3.7"
services:
web:
image: nginx:alpine
volumes:
- type: bind #<-----Notice the type
source: ./mysite.template
target: /etc/nginx/conf.d/mysite.template
ports:
- "9090:8080"
environment:
- PORT=8080
command: /bin/sh -c "envsubst < /etc/nginx/conf.d/mysite.template >
/etc/nginx/conf.d/default.conf && exec nginx -g 'daemon off;'"
(*) 请注意,此示例也可以使用卷来解决。
案例 2:数据库。
Docker 容器不存储持久数据:一旦容器停止,任何将写入容器联合文件系统中可写层的数据都将丢失 运行。
但是如果我们在容器上有一个数据库 运行 并且容器停止了怎么办 - 这意味着所有数据都将丢失?
卷 来救援。
这些被命名为文件系统树,由 Docker.
例如 - 持久化 Postgres SQL 数据:
services:
db:
image: postgres:latest
volumes:
- "dbdata:/var/lib/postgresql/data"
volumes:
- type: volume #<-----Notice the type
source: dbdata
target: /var/lib/postgresql/data
volumes:
dbdata:
请注意,在这种情况下,对于命名卷,源是卷的名称 (对于匿名卷,省略此字段)。
Feature |
Bind |
Volume |
---|---|---|
Internal soul | Bind mounts attach a user-specified location on host filesystem to a specific point in a container file tree. | Volume attach with disk storage on the host filesystem or cloud storage. |
command | --mount type=bind,src="",dst="" |
Docker CLI docker volume command |
Dependency | dependent on location on to the host filesystem. | Container-independent data management |
Separation of concerns | No | Yes |
Conflict with other containers | Yes Example: multiple instances of Cassandra that all use the same host location as a bind mount for data storage. In that case, each of the instances would compete for the same set of files. Without other tools such as file locks, that would likely result in corruption of the database. | No. By default, Docker creates volumes by using the local volume plugin. |
When to choose | 1- Bind mounts are useful when the host provides a file or directory that is needed by a program running in a container, or when that containerized program produces a file or log that is processed by users or programs running outside containers. 2- appropriate tools for workstations, machines with specialized concerns 3- systems with more traditional configuration management tooling. |
Working with Persistent storage 1. Databases 2. Cloud storage |
When not to choose | Better to avoid these kinds of specific bindings in generalized platforms or hardware pools. | To be written |