Docker学习笔记(3)-容器数据卷

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker. Volumes have several advantages over bind mounts:

卷是持久化 Docker 容器生成和使用的数据的首选机制。虽然绑定挂载依赖于主机的目录结构和操作系统,但卷完全由 Docker 管理。与绑定安装相比,卷有几个优点:

  • Volumes are easier to back up or migrate than bind mounts.(使用卷比绑定挂载更容易去备份和迁移数据)
  • You can manage volumes using Docker CLI commands or the Docker API.(你可以使拥docker命令和API来很好的管理卷)
  • Volumes work on both Linux and Windows containers.(卷可以用在windows系统和liunx)
  • Volumes can be more safely shared among multiple containers.(卷也可以更安全的使用在多个容器中间)
  • Volume drivers let you store volumes on remote hosts or cloud providers, to encrypt the contents of volumes, or to add other functionality.
    (卷驱动可以让你的卷存储到远程主机或者云提供商上面,可以加密卷的内容,或者添加其他功能)
  • New volumes can have their content pre-populated by a container.(新卷可以通过容器预先补充其他内容)
  • Volumes on Docker Desktop have much higher performance than bind mounts from Mac and Windows hosts.(Docker 桌面版的卷比 Mac 和 Windows 主机上的绑定挂载具有更高的性能)
  • In addition, volumes are often a better choice than persisting data in a container’s writable layer, because a volume does not increase the (此外,与在容器的可写层中持久化数据相比,卷通常是更好的选择,因为卷不会增加)
  • size of the containers using it, and the volume’s contents exist outside the lifecycle of a given container.(使用它的容器的大小,并且卷的内容存在于给定容器的生命周期之外。)
    在这里插入图片描述

volumes on the Docker host

If your container generates non-persistent state data, consider using a tmpfs mount to avoid storing the data anywhere permanently, and to increase the container’s performance by avoiding writing into the container’s writable layer.
如果你的容器产生了非持久性的状态数据,可以考虑使用tmpfs挂载,以避免将数据永久地存储在任何地方,并通过避免向容器的可写层写入来提高容器的性能。

Volumes use rprivate bind propagation, and bind propagation is not configurable for volumes.
卷使用rprivate绑定传播,而卷的绑定传播是不可配置的。

上面是docker官方文档对于卷的描述 以及卷有什么优点

上面那些优点可能暂时看的不太懂,这个暂时不影响我们使用,当我们通过不同的业务场景和需求时,慢慢的就会知道其中使用的好处与优势、提倡边用边学。

还有上面的那张图,说明容器有三种挂载方式,
一种 bind mount 一种volume 还有一种tmpfs mount
不管你选择哪种挂载方式,从容器中看都是一样的。数据在容器的文件系统中被展示为一个目录或者一个单独的文件。

一个简单区分volumes,bind mounts和tmpfs mounts不同点的方法是:思考数据在宿主机上是如何存在的

  • Volumes由Docker管理,存储在宿主机的某个地方(在linux上是/var/lib/docker/volumes/)。非Docker应用程序不能改动这一位置的数据。* Volumes是Docker最好的数据持久化方法。
  • Bind mounts的数据可以存放在宿主机的任何地方。数据甚至可以是重要的系统文件或目录。非Docker应用程序可以改变这些数据。
  • tmpfs mounts的数据只存储在宿主机的内存中,不会写入到宿主机的文件系统。
更详细的Diff
  • Volumes:由Docker创建和管理。你可以通过docker volume create命令显式地创建volume,Docker也可以在创建容器或服务是自己创建volume。

  • 当你创建了一个volume,它会被存放在宿主机的一个目录下。当你将这个volume挂载到某个容器时,这个目录就是挂载到容器的东西。这一点和bind mounts类似,除了volumes是由Docker创建的,和宿主机的核心(core functionality)隔离。

  • 一个volume可以同时被挂载到几个容器中。即使没有正在运行的容器使用这个volume,volume依然存在,不会被自动清除。可以通过docker volume prune清除不再使用的volumes。

  • volumes也支持volume driver,可以将数据存放在另外的机器或者云上。

  • Bind mounts:Docker早期就支持这个特性。与volumes相比,Bind mounts支持的功能有限。使用bind mounts时,宿主机上的一个文件或目录被挂载到容器上。

警告:使用Bind mounts的一个副作用是,容器中运行的程序可以修改宿主机的文件系统,包括创建,修改,删除重要的系统文件或目录。这个功能可能会有安全问题。

  • tmpfs mounts:tmpfs mounts的数据不会落盘。在容器的生命周期内,它可以被用来存储一些不需要持久化的状态或敏感数据。例如,swarm服务通过tmpfs mounts来将secrets挂载到一个服务的容器中去。

  • 适合Volumes的场景

在不同的容器中共享数据。If you don’t explicitly create it, a volume is created the first time it is mounted into a container. When that container stops or is removed, the volume still exists. Multiple containers can mount the same volume simultaneously, either read-write or read-only. Volumes are only removed when you explicitly remove them.
When the Docker host is not guaranteed to have a given directory or file structure. Volumes help you decouple the configuration of the Docker host from the container runtime.
When you want to store your container’s data on a remote host or a cloud provider, rather than locally.
当你需要备份或迁移数据的时候,When you need to be able to back up, restore, or migrate data from one Docker host to another, volumes are a better choice. You can stop containers using the volume, then back up the volume’s directory (such as /var/lib/docker/volumes/).

  • 适合bind mounts的场景

宿主机和容器共享配置文件。Docker提供的DNS解决方案就是如此,将宿主机的/etc/resolv.conf挂载到每个容器中。
开发环境需要在宿主机和容器中共享代码。docker的开发就是如此,毕竟容器中一般是没有编辑器的
When the file or directory structure of the Docker host is guaranteed to be consistent with the bind mounts the containers require.

  • 适合tmpfs mounts的场景

tmpfs mounts主要用在你既不想在容器内,又不想在宿主机文件系统保存数据的时候。这可能是出于安全原因,也可能是你的应用需要写非常多的非持久化数据,tmpfs mounts这时候可以保证容器性能。

了解清楚不同场景下使用不同的挂载方式

我们来使用下volume的基本操作

#交互模式运行一个mysql容器 设置密码为123456
docker run -d -p 3302:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 -v mysql02:/usr/lib/mysql mysql:5.6

看到容器已经跑起来了
在这里插入图片描述
我们可以查看下容器信息

docker inspect contain_id

在这里插入图片描述

此时容器卷已经创建了
我们查看宿主机有没有具名卷
在这里插入图片描述

  • 匿名挂载 不指定名字 随机生成 在这里插入图片描述

  • 具名挂载 会有指定名称

docker run -d -p 3302:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 -v hahah:/usr/lib/mysql mysql:5.6

在这里插入图片描述

  • 指定路径挂载
-v 宿主机路径::容器内路径

小练习:建立三个mysql容器 使其数据共享

docker run -d -p 3300:3306 --name mysql01 -v mysqldata:/var/lib/mysql/ mysql:5.6
docker run -d -p 3301:3306 --name mysql02 -v mysqldata:/var/lib/mysql/ mysql:5.6
docker run -d -p 3302:3306 --name mysql03 -v mysqldata:/var/lib/mysql/ mysql:5.6

此时连接mysql01创建数据库tb
然后断开mysql01 启动mysql02
就会发现数据库tb会自动到mysql02中
因为他们公用一个文件目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员若风+

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值