当前位置: 首页 > 编程文章 >

Docker数据卷挂载命令volume(-v)与mount的使用总结

Docker数据卷挂载命令volume(-v)与mount的使用总结

作者:陆言时间:2026-09-04 17:53:46浏览:834

Docker提供--volume/-v和--mount两个选项用于创建带有数据卷的容器,两者在功能范围和语法结构上存在微妙差异。--volume仅支持bind mount,而--mount默认挂载volume并支持bind mount和tmpfs。本文梳理了两种命令的具体用法、参数格式及适用场景,帮助开发者根据需求选择更合适的挂载方式。

本文目录展开
目录
  • 前言
  • 命令用法
    • --volume(-v)
    • --mount
  • 差异总结
  • 官方文档

前言

在Docker中,用户可通过docker run命令的--volume/-v或--mount选项来创建包含数据卷的容器。尽管这两个选项都能实现数据持久化,但在底层机制和使用细节上存在一些微妙差异,以下进行详细梳理。

命令用法

--volume(-v)

参数--volume(或其简写形式-v)的功能较为单一,仅能用于创建bind mount类型的挂载点。具体使用示例如下:

docker run --name $CONTAINER_NAME -it \
-v $PWD/$CONTAINER_NAME/app:/app:rw \
-v $PWD/$CONTAINER_NAME/data:/data:ro \
avocado-cloud:latest /bin/bash

注意事项:

  • 命令基本格式为[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]
  • 若指定了HOST-DIR,则该路径必须是绝对路径;如果该路径在宿主机上不存在,Docker会自动创建它。
  • 在实例中,rw表示读写权限,ro表示只读权限。

--mount

参数--mount在默认情况下用于挂载volume,这是一种更为灵活的数据管理方式,可以通过docker volume命令集进行独立管理。此外,--mount也支持创建bind mount和tmpfs。若不显式指定type选项,则默认为挂载volume。具体使用示例如下:

docker run --name $CONTAINER_NAME -it \
--mount type=bind,source=$PWD/$CONTAINER_NAME/app,destination=/app \
--mount source=${CONTAINER_NAME}-data,destination=/data,readonly \
avocado-cloud:latest /bin/bash

注意事项:

  • 挂载volume的命令格式为:[type=volume,]source=my-volume,destination=/path/in/container[,...]
  • 创建bind mount的命令格式为:type=bind,source=/path/on/host,destination=/path/in/container[,...]
  • 当使用--mount创建bind mount时,若指定了source,则该路径必须是绝对路径且必须已经存在于宿主机上。
  • 在示例中,readonly参数表示只读挂载。

差异总结

创建bind mount和挂载volume的比较

对比项bind mountvolume
Source位置用户指定/var/lib/docker/volumes/
Source为空覆盖dest为空保留dest内容
Source非空覆盖dest内容覆盖dest内容
Source种类文件或目录只能是目录
可移植性一般(自行维护)强(docker托管)
宿主直接访问容易(仅需chown)受限(需登陆root用户)*

*注释:Docker无法简单地通过sudo chown someuser: -R /var/lib/docker/volumes/somevolume来将volume的内容开放给主机上的普通用户访问,如果开放更多权限则有安全风险。而这点上Podman的设计就要理想得多,volume存放在$HOME/.local/share/containers/storage/volumes/路径下,即提供了便捷性,又保障了安全性。无需root权限即可运行容器,这正是Podman的优势之一,实际使用过程中的确受益良多。

创建bind mount时使用--volume和--mount的比较

对比项--volume 或 -v--mount type=bind
如果主机路径不存在自动创建命令报错

官方文档

DOCKER(1)                          JUNE 2014                         DOCKER(1)

NAME
       docker-run - Run a command in a new container

SYNOPSIS
       docker run
       [--mount[=[MOUNT]]]
       [-v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]]
       IMAGE

OPTIONS
       --mount type=TYPE,TYPE-SPECIFIC-OPTION[,...]
          Attach a filesystem mount to the container

       Current supported mount TYPES are bind, volume, and tmpfs.

       e.g.

       type=bind,source=/path/on/host,destination=/path/in/container

       type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round"

       type=tmpfs,tmpfs-size=512M,destination=/path/in/container

       Common Options:

              · src, source: mount source spec for bind and volume. Mandatory
                for bind.

              · dst, destination, target: mount destination spec.

              · ro, readonly: true or false (default).

       Note: setting readonly for a bind mount does not make its submounts
          read-only on the current Linux implementation. See also
       bind-nonrecursive.

       Options specific to bind:

              · bind-propagation: shared, slave, private, rshared, rslave, or
                rprivate(default). See also mount(2).

              · consistency: consistent(default), cached, or delegated.
                Currently, only effective for Docker for Mac.

              · bind-nonrecursive: true or false (default). If set to true,
                submounts are not recursively bind-mounted. This option is
                useful for readonly bind mount.

       Options specific to volume:

              · volume-driver: Name of the volume-driver plugin.

              · volume-label: Custom metadata.

              · volume-nocopy: true(default) or false. If set to false, the
                Engine copies existing files and directories under the
                mount-path into the volume, allowing the host to access them.

              · volume-opt: specific to a given volume driver.
              
       Options specific to tmpfs:

              · tmpfs-size: Size of the tmpfs mount in bytes. Unlimited by
                default in Linux.

              · tmpfs-mode: File mode of the tmpfs in octal. (e.g. 700 or
                0700.) Defaults to 1777 in Linux.

       -v|--volume[=[[HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]
          Create a bind mount. If you specify, -v /HOST-DIR:/CONTAINER-DIR,
       Docker
          bind mounts /HOST-DIR in the host to /CONTAINER-DIR in the Docker
          container. If 'HOST-DIR' is omitted,  Docker automatically creates
       the new
          volume on the host.  The OPTIONS are a comma delimited list and can
       be:

              · [rw|ro]

              · [z|Z]

              · [[r]shared|[r]slave|[r]private]

              · [delegated|cached|consistent]

              · [nocopy]

       The CONTAINER-DIR must be an absolute path such as /src/docs. The
       HOST-DIR can be an absolute path or a name value. A name value must
       start with an alphanumeric character, followed by a-z0-9, _
       (underscore), . (period) or - (hyphen). An absolute path starts with a
       / (forward slash).

       If you supply a HOST-DIR that is an absolute path,  Docker bind-mounts
       to the path you specify. If you supply a name, Docker creates a named
       volume by that name. For example, you can specify either /foo or foo
       for a HOST-DIR value. If you supply the /foo value, Docker creates a
       bind mount. If you supply the foo specification, Docker creates a named
       volume.

       You can specify multiple  -v options to mount one or more mounts to a
       container. To use these same mounts in other containers, specify the
       --volumes-from option also.

       You can supply additional options for each bind mount following an
       additional colon.  A :ro or :rw suffix mounts a volume in read-only or
       read-write mode, respectively. By default, volumes are mounted in
       read-write mode.  You can also specify the consistency requirement for
       the mount, either :consistent (the default), :cached, or :delegated.
       Multiple options are separated by commas, e.g. :ro,cached.

       Labeling systems like SELinux require that proper labels are placed on
       volume content mounted into a container. Without a label, the security
       system might prevent the processes running inside the container from
       using the content. By default, Docker does not change the labels set by
       the OS.

       To change a label in the container context, you can add either of two
       suffixes :z or :Z to the volume mount. These suffixes tell Docker to
       relabel file objects on the shared volumes. The z option tells Docker
       that two containers share the volume content. As a result, Docker
       labels the content with a shared content label. Shared volume labels
       allow all containers to read/write content.  The Z option tells Docker
       to label the content with a private unshared label.  Only the current
       container can use a private volume.

       By default bind mounted volumes are private. That means any mounts done
       inside container will not be visible on host and vice-a-versa. One can
       change this behavior by specifying a volume mount propagation property.
       Making a volume shared mounts done under that volume inside container
       will be visible on host and vice-a-versa. Making a volume slave enables
       only one way mount propagation and that is mounts done on host under
       that volume will be visible inside container but not the other way
       around.

       To control mount propagation property of volume one can use :[r]shared,
       :[r]slave or :[r]private propagation flag. Propagation property can be
       specified only for bind mounted volumes and not for internal volumes or
       named volumes. For mount propagation to work source mount point (mount
       point where source dir is mounted on) has to have right propagation
       properties. For shared volumes, source mount point has to be shared.
       And for slave volumes, source mount has to be either shared or slave.

       Use df  to figure out the source mount and then use findmnt
       -o TARGET,PROPAGATION  to figure out propagation
       properties of source mount. If findmnt utility is not available, then
       one can look at mount entry for source mount point in
       /proc/self/mountinfo. Look at optional fields and see if any
       propagation properties are specified.  shared:X means mount is shared,
       master:X means mount is slave and if nothing is there that means mount
       is private.

       To change propagation properties of a mount point use mount command.
       For example, if one wants to bind mount source directory /foo one can
       do mount --bind /foo /foo and mount --make-private --make-shared /foo.
       This will convert /foo into a shared mount point. Alternatively one can
       directly change propagation properties of source mount. Say / is source
       mount for /foo, then use mount --make-shared / to convert / into a
       shared mount.

              Note: When using systemd to manage the Docker daemon's start and
              stop, in the systemd unit file there is an option to control
              mount propagation for the Docker daemon itself, called
              MountFlags. The value of this setting may cause Docker to not
              see mount propagation changes made on the mount point. For
              example, if this value is slave, you may not be able to use the
              shared or rshared propagation on a volume.

       To disable automatic copying of data from the container path to the
       volume, use the nocopy flag. The nocopy flag can be set on bind mounts
       and named volumes.

       See also --mount, which is the successor of --tmpfs and --volume.  Even
       though there is no plan to deprecate --volume, usage of --mount is
       recommended.

Docker Community              Docker User Manuals                    DOCKER(1)

到此这篇关于Docker数据卷挂载命令volume(-v)与mount的使用总结的文章就介绍到这了,更多相关Docker volume(-v)与mount内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!

上一篇
Docker端口映射后无法访问的排查与解决

当Docker容器端口映射正常但外部无法访问时,通常由服务监听地址限制或宿主机内核参数未开启引起。需检查配置文件将监听地址从127.0.0.1改为0.0.0.0,并确认sysctl.net.ipv4.ip_forward值为1。通过重启服务和验证网络连接可解决此问题。

下一篇
PHP结合AJAX实现批量删除功能详解

本文介绍如何使用PHP与AJAX技术实现前端页面的批量数据删除功能。通过jQuery监听全选复选框状态,动态获取选中项的ID列表,并以POST方式异步提交至后端脚本。后端接收参数后执行SQL DELETE语句清除记录,并返回JSON格式的状态结果供前端提示。该方法避免了页面刷新,提升了用户体验。

推荐文章

更多
PHP时间戳与日期字符串相互转换方法详解

本文介绍PHP中时间数值(Unix时间戳)与日期字符串之间的相互转换方法。通过strtotime和date函数,实现将当前时间、指定日期转换为时间戳,以及将时间戳格式化为可读的日期时间字符串,并提供相关在线计算工具推荐。

2025.11.30 1,031
PHP日期转时间戳时区偏差怎么解决

在 PHP 中将日期字符串转换为时间戳时,若未显式指定时区,默认使用脚本配置的 date.timezone 或系统时区。当服务器时区与业务逻辑预期(如 UTC )不一致时,会产生偏差。解决核心是确保输入字符串携带明确时区信息,或在转换前统一设置时区上下文。

2026.10.24 1,029
PHP跨域配置:怎么安全地允许指定域名而非使用通配符

在 PHP 中安全地允许指定域名跨域,核心是解析请求头中的 Origin ,验证其是否在白名单内,然后动态设置 Access-Control-Allow-Origin 响应头。不要使用通配符 * ,也不要硬编码单一域名。

2025.03.29 1,017
JavaScript中利用replace结合正则实现replaceAll效果

JavaScript原生仅支持replace()进行单次或全局替换,缺乏类似Java的replaceAll()便捷方法。本文总结四种在JS中实现全量替换的方案:使用带g标志的正则、带gm标志的正则、扩展String原型以及split后join。通过对比代码示例,展示各方法的实现细节与适用场景,帮助开发者根据需求选择最优解。

2025.04.29 1,014
OpenAI的Jalapeno芯片未落地,教你分辨300兆瓦算力消息

OpenAI计划在SB Energy租赁的数据中心部署约300兆瓦自研Jalapeno推理芯片,但目前双方仍处于洽谈阶段,交易细节与上线时间表均未公布。本文带你按报道原文核实项目所处阶段,厘清芯片发布、电力功率与实际推理算力的边界,并将融资意向与签约、开工、投运节点分开追踪,避免误将规划容量计入现有算力。

2026.12.02 1,011
PHP实现基础文件上传:HTML表单与$_FILES处理流程

在 HTML 表单中设置 enctype="multipart/form-data" ,并在 PHP 后端通过 $_FILES 数组接收文件,调用 move_uploaded_file() 将临时文件移动到目标目录即可完成上传。

2026.09.07 1,011

专题推荐

更多
PHP 类与对象定义及继承

先看类声明、对象实例与属性访问。理解对象的创建和销毁后,再读继承、方法重载和关键字对照。已有代码遇到继承行为差异时,可进一步查看 final 方法与反射实例。

PHP 定时任务配置与运行

先确定任务运行间隔以及是否允许重叠。固定时间触发先看 Cron 案例,框架内和常驻服务再读对应示例。

Yii2 队列配置与常驻运行

先用一个最小任务跑通入队和消费,再检查 Web 与 Console 是否加载了相同的队列配置。确认命令能手动运行后再配置 Supervisor,最后验证进程重启时的处理情况。

PHP Session 过期时间设置方法

浏览器关闭、服务端回收和业务规定的闲置超时,可能在不同时间发生。对照配置项与应用层时间戳的做法,检查会话何时仍可读、何时应要求重新登录。

PHP URL 编码和解码方法

URL 路径片段和查询参数对空格的处理方式不同。先比较 urlencode 与 rawurlencode,再沿解码过程核对中文、加号和百分号,检查地址是否被重复编码或提前解码。

在线工具箱

更多
键码查询
键码查询

按下键盘按键时查看浏览器记录的键名和事件信息。

.env 配置检查
.env 配置检查

本地检查 .env 键值语法、引号、重复键及空值,预览解析结果,辅助排查 PHP 和 Laravel 环境配置。

进制转换
进制转换

转换二进制、十进制、十六进制等数值表示,辅助程序调试与进制学习。

HTML 标签安全清理
HTML 标签安全清理

清理 HTML 片段中的标签或危险内容,辅助整理待展示的文本。

文件 Base64 转换
文件 Base64 转换

把文件转为 Base64 文本,也可将编码内容还原为文件,方便接口传输与调试。