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 集群卷组(集群卷) | |
--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+ Swarm 集群卷必须可访问的拓扑结构 | |
--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
驱动程序接受类似于Linuxmount
命令的选项。可以通过多次传递--opt
标志来提供多个选项。某些mount
选项(例如o
选项)可以采用逗号分隔的选项列表。可在此处找到完整的可用挂载选项列表。
例如,以下命令创建一个名为foo
的tmpfs
卷,大小为 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
另一个使用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