Volumes是Docker最为推荐的数据持久化方法。

Volumes have several advantages over bind mounts:

  • Volumes are easier to back up or migrate than bind mounts.
  • You can manage volumes using Docker CLI commands or the Docker API.
  • Volumes work on both Linux and Windows containers.
  • Volumes can be more safely shared among multiple containers.
  • Volume drivers allow you to store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
  • A new volume’s contents can be pre-populated by a container.

Choose the -v or --mount flag

  起初, the -v or --volume flag was used for 单机版-Containers and the --mount flag was used for Swarm services. However, starting with Docker 17.06, you can also use --mount with 单机版-Containers. In general, --mount 更清晰 and 更冗长. 最大的不同是 -v 语法将所有的选项组合在一起,--mount语法将他们进行了分离。

Tip: 新永不应该使用--mount语法,有经验的用户更熟悉-v or --volume语法。 但是更鼓励使用--mount,因为调查发现它更容易被使用。

If you need to specify volume driver options, you must use --mount.

  • --mount: 由多个<key>=<value>对组成,使用逗号进行分割。The --mount syntax is more verbose than -v or --volume, but the order of the keys is not significant, and the value of the flag is easier to understand.

    • The type of the mount, which can be bind, volume, or tmpfs. This topic discusses volumes, so the type is always volume.
    • The source of the mount. For named volumes, this is the name of the volume. For anonymous volumes, this field is omitted. May be specified as source or src.
    • The destination takes as its value the path where the file or directory is mounted in the container. May be specified as destination, dst, or target.
    • The readonly option, if present, causes the bind mount to be mounted into the container as read-only.
    • The volume-opt option, which can be specified more than once, takes a key-value pair consisting of the option name and its value.

Create and manage volumes

Unlike a bind mount, you can create and manage volumes outside the scope of any container.

Create a volume:

$ docker volume create my-vol

List volumes:

$ docker volume ls

local               my-vol

Inspect a volume:

$ docker volume inspect my-vol
[
{
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
"Name": "my-vol",
"Options": {},
"Scope": "local"
}
]

Remove a volume:

$ docker volume rm my-vol

Start a container with a volume

If you start a container with a volume that does not yet exist, Docker creates the volume for you. The following example mounts the volume myvol2 into /app/ in the container.

The -v and --mount examples below produce the same result. You can’t run them both unless you remove the devtest container and the myvol2 volume after running the first one.

$ docker run -d \
--name devtest \
--mount source=myvol2,target=/app \
nginx:latest
$ docker run -d \
--name devtest \
-v myvol2:/app \
nginx:latest
  • 可以为多个Container mount同一个volume,以达到数据在多个Container之间共享的目的;
  • 如果 mount target指向的是已有目录,原有数据会被复制到source volume 中。

参考文档: https://docs.docker.com/

 

Docker 数据管理-Volumes的更多相关文章

  1. Docker数据管理

    用户在使用Docker的过程中,往往需要能查看容器内应用产生的数据,或者需要把容器内的数据进行备份,甚至多个容器之间进行数据共享,这必然涉及到Docker的数据管理. 容器中管理数据主要有两种方式: ...

  2. Docker数据管理(四)

    Docker数据管理 Docker数据分为两种: 数据卷 -v /data -v src:dst 数据卷容器 --volumes-from 数据卷 案例1:我们创建一个容器,起名叫nginx-volu ...

  3. Docker 数据管理(Volumes)

    Docker 容器产生的数据在可写层,如果不通过 docker commit 生成新的镜像,使得数据成为镜像的一部分保存下来,那么当容器删除后,数据自然也就没有了. Docker 提供了三种数据 Mo ...

  4. Docker数据管理-数据卷 data volumes和数据卷容器data volumes containers的使用详解

    此文来源于:https://yq.aliyun.com/ziliao/43471 参考原文件之外,做了些修改. Volume数据卷是Docker的一个重要概念.数据卷是可供一个或多个容器使用的特殊目录 ...

  5. Docker系统七:Docker数据管理

    Docker的数据管理 I. 基本概念 Docker容器一旦删除,其相关的rootf文件系统就会被删除,其容器内的数据将一并删除,为了保存相关数据,Docker提出了数据卷的概念. II. 数据卷 D ...

  6. Docker数据管理(数据卷&数据卷容器)

    生产环境中使用Docker的过程中,往往需要对数据进行持久化,或者需要在多个容器之间进行数据共享,这必然涉及容器的数据管理操作. 容器中管理数据主要有两种方式: 数据卷(Data Volumes):容 ...

  7. 【Docker】第五篇 Docker 数据管理

    一.基本介绍 数据管理的原因:Docker中的容器一旦删除,容器本身的rootfs文件系统就会被删除,容器中的所有数据就会被删除.为了对一些需要持久化的数据,不随容器删除而删除,所以我们可以通过多个容 ...

  8. 六、【Docker笔记】Docker数据管理

    前几节我们介绍了Docker的基本使用和三大核心概念,那么我们在使用Docker的过程中,Docker中必然产生了大量的数据,对于这些数据我们需要查看或者对这些数据进行一个备份,也有可能容器之间的数据 ...

  9. Docker数据管理(五)

    一.什么是数据卷 生成环境中使用docker的过程中,往往需要对数据进行持久化,或者需要多个容器之间进行数据共享,这个就涉及到了容器数据管理 容器中管理数据主要有两种方式: 数据卷:容器内数据之间映射 ...

随机推荐

  1. Eclipse 经常使用快捷键

    一.File 二.Edit Ctrl + 1  有益写错,让编辑器提醒改动 三.Refactor 抽取为全局变量 Refactor - Convert Local Variable to Field ...

  2. linux下 apache启动、停止、重启命令

    假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令:推荐/usr/local/apache2/bin/apachectl start apa ...

  3. springboot整合docker部署(两种构建Docker镜像方式)

    项目结构 package hello; import org.springframework.boot.SpringApplication; import org.springframework.bo ...

  4. foxmail 客户端 LOGIN Login error password error

    显示这个错误是我在更换电脑时,将E:\Foxmail 7.2\Storage\15167136106@163.com 账户 移动到新的电脑上,并在新电脑上创建用户,总是报:账户或密码错误 我输入的密码 ...

  5. MyBatis_传入参数的问题

    一.单个参数 1.基本数据类型 (1)直接使用 List<ChargeRuleDO> tests(long id); <select id="tests" res ...

  6. tcp 状态转移图详解

    首先看一张图片: 虚线表示服务端的状态转移,实现表示客户端的状态转移. 初始的close状态并不是真是的状态,只是为了方便描述开始和终止状态而构造出来的. 从服务端的状态转移开始说: 服务端打开后处于 ...

  7. Android自定义action与permission!!! (转)

    原文地址:http://blog.csdn.net/android_tutor/article/details/6310418#reply 大家好,今天给大家简单分享一下Android中自定义acti ...

  8. PMD:Java源代码扫描器

    PMD是一个开源代码分析器.可以查找常见编程缺陷,比如未使用的变量.空catch代码块.不必要的对象创建等.支持Java.JavaScript.PLSQL.Apache Velocity.XML.XS ...

  9. 本地虚拟机LNMP环境安装

      首先上传源码包到linux中(本人上传到根目录中),随意上传能找到即可 一.配置YUM源(如果已经配好就不许要重新配置) 挂载光驱要挂载到/mnt下  Mount /dev/cdrom /mnt ...

  10. org.apache.poi3.1.7 Excle并发批量导入导出

    org.apache.poi3.1.7 升级,需要修改设置方式: 1.org.apache.poi3.1.4 的设置单元格: XSSFCellStyle cellStyle = wb.createCe ...