docker volume create
| 描述 | 创建卷 |
|---|---|
| 用法 | docker volume create [OPTIONS] [VOLUME] |
描述
创建一个可供容器使用并存储数据的新卷。如果未指定名称,Docker 会生成一个随机名称。
选项
| 选项 | 默认 | 描述 |
|---|---|---|
--availability | active | API 1.42+
Swarm
集群卷可用性 (active, pause, drain) |
-d, --driver | local | 指定卷驱动名称 |
--group | API 1.42+ Swarm 集群卷组 (cluster volumes) | |
--label | 设置卷的元数据 | |
--limit-bytes | API 1.42+ Swarm 集群卷的最小大小(字节) | |
-o, --opt | 设置驱动特定选项 | |
--required-bytes | API 1.42+ Swarm 集群卷的最大大小(字节) | |
--scope | single | API 1.42+
Swarm
集群卷访问范围 (single, multi) |
--secret | API 1.42+ Swarm 集群卷密钥 | |
--sharing | none | API 1.42+
Swarm集群卷访问共享 (none, readonly, onewriter, all) |
--topology-preferred | API 1.42+ Swarm 集群卷首选的拓扑结构 | |
--topology-required | API 1.42+ 集群 集群卷必须可访问的拓扑 | |
--type | block | API 1.42+
Swarm
集群卷访问类型 (mount, block) |
示例
创建一个卷,然后配置容器以使用它:
$ docker volume create hello
hello
$ docker run -d -v hello:/world busybox ls /world
挂载点创建在容器的 /world 目录中。Docker 不支持容器内挂载点的相对路径。
多个容器可以使用同一个卷。如果两个容器需要访问共享数据,这将非常有用。例如,如果一个容器写入数据,而另一个容器读取数据。
卷名在驱动程序中必须是唯一的。这意味着您不能在两个不同的驱动程序中使用相同的卷名。尝试创建两个同名的卷会导致错误:
A volume named "hello" already exists with the "some-other" driver. Choose a different volume name.
如果您指定了当前驱动程序上已在使用的卷名称,Docker 会假定您想要重用现有的卷,并且不会返回错误。
特定于驱动程序的选项 (-o, --opt)
某些卷驱动程序可能需要选项来自定义卷的创建。使用
-o 或 --opt 标志来传递驱动程序选项:
$ docker volume create --driver fake \
--opt tardis=blue \
--opt timey=wimey \
foo
这些选项直接传递给卷驱动程序。不同卷驱动程序的选项可能会产生不同的效果(或者根本不起作用)。
内置的 local 驱动在 Windows 上不接受任何选项。在 Linux 和 Docker Desktop 上,local 驱动接受的选项类似于 Linux mount 命令。您可以通过多次传递 --opt 标志来提供多个选项。某些 mount 选项(例如 o 选项)可以接受逗号分隔的选项列表。可用挂载选项的完整列表可以在 此处 找到。
例如,以下命令创建了一个名为 foo 的 tmpfs 卷,大小为 100 兆字节,以及一个大小为 1000 的 uid。
$ docker volume create --driver local \
--opt type=tmpfs \
--opt device=tmpfs \
--opt o=size=100m,uid=1000 \
foo
另一个使用 btrfs 的示例:
$ docker volume create --driver local \
--opt type=btrfs \
--opt device=/dev/sda2 \
foo
另一个示例,使用 nfs 以 rw 模式从 192.168.1.1 挂载 /path/to/dir:
$ docker volume create --driver local \
--opt type=nfs \
--opt o=addr=192.168.1.1,rw \
--opt device=:/path/to/dir \
foo