docker 卷创建

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

描述

创建一个新卷,容器可以在其中使用和存储数据。如果未指定名称,Docker 会生成一个随机名称。

选项

选项默认描述
--availabilityactiveAPI 1.42+ Swarm 集群卷可用性 ( active, pause, drain)
-d, --driverlocal指定卷驱动程序名称
--groupAPI 1.42+ Swarm Cluster 卷组(集群卷)
--label设置卷的元数据
--limit-bytesAPI 1.42+ Swarm 集群卷的最小大小(以字节为单位)
-o, --opt设置驱动程序特定选项
--required-bytesAPI 1.42+ Swarm 集群卷的最大大小(以字节为单位)
--scopesingleAPI 1.42+ Swarm Cluster Volume 访问范围 ( single, multi)
--secretAPI 1.42+ Swarm Cluster Volume 秘密
--sharingnoneAPI 1.42+ Swarm Cluster 卷访问共享 ( none, readonly, onewriter, all)
--topology-preferredAPI 1.42+ Swarm 首选集群卷的拓扑
--topology-requiredAPI 1.42+ Swarm 必须可访问集群卷的拓扑
--typeblockAPI 1.42+ Swarm Cluster 卷访问类型 ( 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选项)可以采用逗号分隔的选项列表。可以在此处找到可用安装选项的完整列表 。

例如,以下命令创建一个名为100 MB、大小为 1000 的tmpfs卷。foouid

$ 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另一个使用以下方式挂载/path/to/dirinrw模式的 示例192.168.1.1

$ docker volume create --driver local \
    --opt type=nfs \
    --opt o=addr=192.168.1.1,rw \
    --opt device=:/path/to/dir \
    foo