docker volume create

描述创建一个卷
用法docker volume create [OPTIONS] [VOLUME]

描述

创建一个新的卷,容器可以使用它来存储数据。如果未指定名称,Docker 将生成一个随机名称。

选项

选项默认值描述
--availabilityactiveAPI 1.42+ Swarm 集群卷可用性(activepausedrain
-d, --driverlocal指定卷驱动程序名称
--groupAPI 1.42+ Swarm 集群卷组(集群卷)
--label为卷设置元数据
--limit-bytesAPI 1.42+ Swarm 集群卷的最小大小(字节)
-o, --opt设置驱动程序特定选项
--required-bytesAPI 1.42+ Swarm 集群卷的最大大小(字节)
--scopesingleAPI 1.42+ Swarm 集群卷访问范围(singlemulti
--secretAPI 1.42+ Swarm 集群卷密钥
--sharingnoneAPI 1.42+ Swarm 集群卷访问共享(nonereadonlyonewriterall
--topology-preferredAPI 1.42+ Swarm 集群卷首选拓扑
--topology-requiredAPI 1.42+ Swarm 集群卷必须可访问的拓扑
--typeblockAPI 1.42+ Swarm 集群卷访问类型(mountblock

示例

创建卷并配置容器使用它

$ 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 选项)可以接受逗号分隔的选项列表。有关可用挂载选项的完整列表,请参阅此处

例如,以下命令创建一个名为 footmpfs 卷,大小为 100 兆字节,uid 为 1000。

$ 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

另一个使用 nfs192.168.1.1rw 模式挂载 /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
© . This site is unofficial and not affiliated with Kubernetes or Docker Inc.