windows docker 数据卷(四)

docker 官方关于数据卷的解释

https://docs.docker.com/storage/volumes/

 

数据卷解决的问题

  共享主机与容器,容器与容器之间的公用数据。在容器内对卷的修改会立即在主机上生效

 

使用环境

  系统:windows10

  命令控制台:windows 的powershell docker 不建议使用 powershell ISE

特点

  • 与绑定安装相比,卷更易于备份或迁移。
  • 您可以使用Docker CLI命令或Docker API管理卷。
  • 卷在Linux和Windows容器上均可工作。
  • 可以在多个容器之间更安全地共享卷。
  • 卷驱动程序使您可以将卷存储在远程主机或云提供程序上,以加密卷内容或添加其他功能。
  • 可以通过容器预先填充新卷的内容。

卷的声明周期独立于容器的声明周期

使用

 两种方式可以挂载数据卷 -v(--volume) 或 --mount ,docker 官方推荐使用--mount 

  优点:更明确的语意,虽然冗长,但是更好理解。 使用key=value 的方式,明确每个字段的含义

  绑定的主机文件目录若不存在则--mount 不会自动创建,而-v 会自动创建出一个目录

挂载一个数据卷的过程

  列出docker 镜像:   docker iamges

 

  绑定并运行docker:

  docker run -d -it --name mymount --mount type=bind,source="${pwd}",target=/data mytest 

  此时windows 会弹出一个windows share it的确认框 点击share it

 

 

 

 

   若docker 容器中没有 data 文件夹则创建一个文件夹

 

 红框标记的地方是没有点击shrae it 爆出来的错误

  命令解析

  -d  : detached 以分离模式运行,即后台模式 dtached 默认等于true 若设置为detached=false 则等同于没有过-d  ,即前台模式运行

  -it : i 是 打印信息 -t 分配一个伪tty(终端)

  --name :给数据卷命名

  type: 分为三种  bind ,volume, tmpfs

  source:宿主主机的文件路径:我这里是 F:\docker\mountdir  

  target:docker 容器内的文件路径

  mytest :某个镜像

 

docker inspect mymount(这是数据卷名) :查看数据卷的信息

  

 

尝试修改容器内的数据, 查看绑定的文件夹内容是否同步变化

  

 

 

 可以看到在容器中创建一个文件立马在主机映射的目录下也创建一个文件

 

 

 在主机上直接创建一个文件但是在容器内却看不到,有待验证是否有其他方式查看注解内容,不然怎么在容器之间做资源共享。

 

 

 

 删除容器内的文件并不能删除主机上的文件这说明:数据卷主机的内容并不会随着容器的数据销毁而销毁。

 

  

 

 

 停止和删除数据卷

 

 

 

 

 

删除之后再在容器内创建文件并不会映射到主机的目录下

 

其他特性

支持多个数据卷同时绑定

支持设置主机数据卷的读写权限

支持多种隔离级别 

shared Sub-mounts of the original mount are exposed to replica mounts, and sub-mounts of replica mounts are also propagated to the original mount.
slave similar to a shared mount, but only in one direction. If the original mount exposes a sub-mount, the replica mount can see it. However, if the replica mount exposes a sub-mount, the original mount cannot see it.
private The mount is private. Sub-mounts within it are not exposed to replica mounts, and sub-mounts of replica mounts are not exposed to the original mount.
rshared The same as shared, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rslave The same as slave, but the propagation also extends to and from mount points nested within any of the original or replica mount points.
rprivate The default. The same as private, meaning that no mount points anywhere within the original or replica mount points propagate in either direction.

 

多种持久性方式

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

  

 

posted @ 2020-08-08 11:46  zero_and_one  阅读(1891)  评论(0编辑  收藏  举报