stefanzan分享 http://blog.sciencenet.cn/u/stefanzan

博文

什么是Docker Volume?

已有 2134 次阅读 2017-6-7 18:03 |系统分类:博客资讯| Docker

摘要Docker Volume,通常翻译为数据卷,用于保存持久化数据。当我们将数据库例如MySQL运行在Docker容器中时,一般将数据通过Docker Volume保存在主机上,这样即使删除MySQL容器,数据依然保存在主机上,有效保证了数据的安全性。这篇博客将通过简单的实践帮助大家理解什么是Docker Volume



本文所有命令都是在play-with-docker的在线Docker实例上执行,Docker版本为17.05.0-ce

1. 指定Docker Volume

使用docker run命令,可以运行一个Docker容器

docker run -itd --volume /tmp/data1:/tmp/data2 --name test ubuntu bash
  • 基于ubuntu镜像创建了一个Docker容器。

  • 容器的名称为test,由–name选项指定。

  • Docker Volume–volume选项指定,主机的/tmp/data1目录与容器中的/tmp/data2目录一一对应。

2. 查看Docker Volume

使用docker inspect命令,可以查看Docker容器的详细信息:

docker inspect --format='{{json .Mounts}}'test | python -m json.tool
[
   {
"Destination": "/tmp/data2",
"Mode": "",
"Propagation": "",
"RW": true,
"Source": "/tmp/data1",
"Type": "bind"
   }
]
  • 使用–format选项,可以选择性查看需要的容器信息。.Mount为容器的Docker Volume信息。

  • python -m json.tool可以将输出的json字符串格式化显示。

  • Source表示主机上的目录,即/tmp/data1

  • Destination为容器中的目录,即/tmp/data2

3. 本机文件可以同步到容器在本机/tmp/data1目录中新建hello.txt文件

touch /tmp/data1/hello.txt
ls /tmp/data1/
hello.txt

hello.txt文件在容器/tmp/data2/目录中可见

使用docker exec命令,可以在容器中执行命令。

docker exec test ls /tmp/data2/
hello.txt

可知,在本机目录/tmp/data1/的修改,可以同步到容器目录/tmp/data2/中。

4. 容器文件可以同步到主机在容器/tmp/data2目录中新建world.txt文件

docker exectest touch /tmp/data2/world.txt
docker exectest ls /tmp/data2/
hello.txt
world.txt

world.txt文件在主机/tmp/data1/目录中可见

ls /tmp/data1/
hello.txt  world.txt

可知,在容器目录/tmp/data2/的修改,可以同步到主机目录/tmp/data1/中。

5. 结论

Docker Volume本质上是容器与主机之间共享的目录或者文件,这样Docker Volume中的数据可以在主机和容器中实时同步。使用Virtualbox创建虚拟机时,也可以配置共享目录,这与Docker Volume非常相似。

欢迎加入我们FundebugDocker技术交流群: 305097057




版权声明:

转载时请注明作者Fundebug以及本文地址:

https://blog.fundebug.com/2017/06/07/what-is-docker-volume/




https://blog.sciencenet.cn/blog-811611-1059561.html

上一篇:价值1.35亿美元的BUG
下一篇:10个最佳Node.js企业应用案例:从Uber到LinkedIn
收藏 IP: 58.23.11.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 17:12

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部