docker volume create

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

描述

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

选项

选项默认值描述
--availabilityactiveAPI 1.42+ Swarm 集群卷可用性 (active, pause, drain)
-d, --driverlocal指定卷驱动程序名称
--groupAPI 1.42+ Swarm 集群卷组(集群卷)
--label设置卷的元数据
--limit-bytesAPI 1.42+ Swarm 集群卷的最小大小(以字节为单位)
-o, --opt设置驱动程序特定选项
--required-bytesAPI 1.42+ Swarm 集群卷的最大大小(以字节为单位)
--scopesingleAPI 1.42+ Swarm 集群卷访问范围 (single, multi)
--secretAPI 1.42+ Swarm 集群卷密钥
--sharingnoneAPI 1.42+ Swarm 集群卷访问共享 (none, readonly, onewriter, all)
--topology-preferredAPI 1.42+ Swarm 集群卷首选的拓扑结构
--topology-requiredAPI 1.42+ Swarm 集群卷必须可访问的拓扑结构
--typeblockAPI 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驱动程序接受类似于Linuxmount命令的选项。可以通过多次传递--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

另一个使用nfsrw模式从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