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

 更新时间:2022年08月18日 10:54:16   作者:Charles_Shih  
本文主要介绍了Docker数据卷挂载命令volume(-v)与mount的使用总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

用户可以通过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则必须是绝对路径,如果路径不存在则会自动创建
  • 实例中的rw为读写,ro为只读

--mount

参数--mount默认情况下用来挂载volume,但也可以用来创建bind mount和tmpfs。如果不指定type选项,则默认为挂载volume,volume是一种更为灵活的数据管理方式,volume可以通过docker 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[,...]
  • 如果创建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 <source-dir> to figure out the source mount and then use findmnt
       -o TARGET,PROPAGATION <source-mount-dir> 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制作镜像的完整过程

    本文主要介绍了Docker制作镜像的完整过程,以制作CentOS镜像为例,讲述对镜像自定义,打包以及推送的远程仓库的过程,感兴趣的可以了解一下
    2021-11-11
  • Docker之苹果Mac安装Docker的两种方式小结

    Docker之苹果Mac安装Docker的两种方式小结

    这篇文章主要介绍了Docker之苹果Mac安装Docker的两种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Kubernetes(k8s)基础介绍

    Kubernetes(k8s)基础介绍

    今天小编就为大家分享一篇关于Kubernetes(k8s)基础介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • Docker部署Microsoft Sql Server详细步骤

    Docker部署Microsoft Sql Server详细步骤

    大家好,本篇文章主要讲的是Docker部署Microsoft Sql Server详细步骤,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • 2023年windows DockerDeskTop最新款4.18.0 全程保姆级安装图文教程

    2023年windows DockerDeskTop最新款4.18.0 全程保姆级安装图文教程

    这篇文章主要介绍了2023年windows DockerDeskTop最新款4.18.0 全程保姆级安装图文教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • Docker创建tomcat容器实例后无法访问(HTTP状态404)

    Docker创建tomcat容器实例后无法访问(HTTP状态404)

    本文主要介绍了Docker创建tomcat容器实例后无法访问,HTTP状态显示404,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

    ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

    本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker在Linux服务器上安装Elasticsearch、Kibana和IK分词器,并验证安装,感兴趣的朋友一起看看吧
    2025-02-02
  • 在Docker中安装Oracle数据库超详细步骤

    在Docker中安装Oracle数据库超详细步骤

    oracle作为全球最强大的关系型数据库,应用在各行各业,下面这篇文章主要给大家介绍了关于在Docker中安装Oracle数据库的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • Docker部署SpringBoot项目的实现步骤

    Docker部署SpringBoot项目的实现步骤

    本文主要介绍了Docker部署SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2007-02-02
  • 使用Docker运行Microsoft SQL Server 2017的方法

    使用Docker运行Microsoft SQL Server 2017的方法

    本篇文章主要介绍了使用Docker运行Microsoft SQL Server 2017的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11

最新评论