docker 卷创建
描述 | 创建卷 |
---|---|
用法 | 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
driver 在 Windows 上不接受任何选项。在 Linux 上,使用
Docker Desktop 中,local
driver 接受类似于 Linux 的选项mount
命令。您可以通过传递--opt
标记多个
次。一些mount
选项(例如o
选项)可以采用逗号分隔的
选项列表。可在此处找到可用挂载选项的完整列表。
例如,以下代码会创建一个tmpfs
卷调用foo
大小为
100 MB 和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
挂载/path/to/dir
在rw
mode 从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