docker 卷创建
描述 | 创建卷 |
---|---|
用法 | docker volume create [OPTIONS] [VOLUME] |
描述
创建一个新卷,容器可以在其中使用和存储数据。如果未指定名称,Docker 会生成一个随机名称。
选项
选项 | 默认 | 描述 |
---|---|---|
--availability | active | API 1.42+
Swarm
集群卷可用性 ( active , pause , drain ) |
-d, --driver | local | 指定卷驱动程序名称 |
--group | API 1.42+ Swarm Cluster 卷组(集群卷) | |
--label | 设置卷的元数据 | |
--limit-bytes | API 1.42+ Swarm 集群卷的最小大小(以字节为单位) | |
-o, --opt | 设置驱动程序特定选项 | |
--required-bytes | API 1.42+ Swarm 集群卷的最大大小(以字节为单位) | |
--scope | single | API 1.42+
Swarm
Cluster Volume 访问范围 ( single , multi ) |
--secret | API 1.42+ Swarm Cluster Volume 秘密 | |
--sharing | none | API 1.42+
Swarm
Cluster 卷访问共享 ( none , readonly , onewriter , all ) |
--topology-preferred | API 1.42+ Swarm 首选集群卷的拓扑 | |
--topology-required | API 1.42+ Swarm 必须可访问集群卷的拓扑 | |
--type | block | API 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
卷。foo
uid
$ 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
inrw
模式的
示例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