Date Volume 本质上是Dokcer host文件系统中的目录或者文件,能够直接被mount到容器的文件系统中。
Data Volume 有如下特点:
1、Data Volume 是目录或者文件,而非没有格式化的磁盘(块设备)
2、容器可以读写volume中的数据
3、volume数据可以被永久的保存,即使使用他的容器已经销毁
现在有三种方式来存储数据:镜像层、容器层、volume 。考虑如下几个场景:
1、Database 软件 vs Database 数据
2、Web应用 vs 应用产生的日志
3、数据分析软件 vs input/output 数据
4、apache server vs 静态HTML文件
对于以上四种场景,前者是无状态的,放在数据层,属于镜像的一部分。后者放在Data Volume中,这是需要持久化的数据,应该与镜像分开存放。
设置volume容量
因为volume实际上是docker host文件系统的一部分,所以volume的容量取决于文件系统当前未使用的空间,目前还没有办法设置volume的容量
volume挂载方式1 bind mount
将host上已经存在的目录或者文件mount到容器
挂载方式
1、目录 -->> 目录
docker run -v <host dir path>:<container dir path> image_name
2、文件 -->> 文件
docker run -v <host file path>:<container file path> image_name
3、只读方式挂载
docker run -v <host path>:<container path>:ro image_name
4、目录 -->> 不存在目录(会自动在容器中创建这个不存在的目录,内容是docker host上的指定目录)
docker run -v <host dir path>:<container none_dir path> image_name
5、文件 -->> 不存在的文件(会自动在容器中创建这个不存在的文件,内容是dokcer host上的指定文件)
docker run -v <host file path>:<container none_file path> image_name
6、不存在path -->> 存在目录(在docker host上创建一个空目录,挂载到容器中,覆盖容器中的目录)
docker run -v <host none_path>:<container dir> image_name
7、不存在path -->> 存在文件(报错,无法挂载)
docker run -v <host none_path>:<container file> image_name
8、不存在path -->> 不存在path (会在dockerhost和容器中分别创建对应的path,进行挂载)
docker run -v <host path>:<container path>:ro image_name
容器销毁后,之前对docker host 上进行的文件或者目录修改不会丢失,会持久保存
需要注意的是,当容器迁移到新的docker host上,而该host上没有目录可供挂载,那么挂载会失败
1、测试一个正常的httpd容器
root@docker-lab:~/html# docker run -d -p 81:80 httpd
eafed05a921727813c10dad78e3b850e44729b1f9866f511fb5edfbafbfe5256
root@docker-lab:~/html# docker inspect eafe -f '{{.NetworkSettings.IPAddress}}'
172.17.0.12
root@docker-lab:~/html# curl http://172.17.0.12
<html><body><h1>It works!</h1></body></html>
root@docker-lab:~/html# curl http://127.0.0.1:81
<html><body><h1>It works!</h1></body></html>
2、挂载docker host上的HTML目录到httpd容器中,且容器销毁后dockerhost上的文件还在
root@docker-lab:~/html# pwd
/root/html
root@docker-lab:~/html# cat index.html
Docker Volume bind test
root@docker-lab:~/html# docker run -d -p 80:80 -v /root/html:/usr/local/apache2/htdocs httpd
595f3bf54850a93c0b10580efed8f3c7c6ab70c264ad8b3bd108898a3f059295
root@docker-lab:~/html# docker inspect 595f -f '{{.NetworkSettings.IPAddress}}'
172.17.0.11
root@docker-lab:~/html# curl http://172.17.0.11
Docker Volume bind test
root@docker-lab:~/html# curl http://127.0.0.1:80
Docker Volume bind test
root@docker-lab:~/html# docker stop 595f
595f
root@docker-lab:~/html# docker rm 595f
595f
root@docker-lab:~/html# pwd
/root/html
root@docker-lab:~/html# cat index.html
Docker Volume bind test
3、挂载docker host上的文件到容器中,验证效果
root@docker-lab:~/html# docker run -d -p 80:80 -v /root/html/index.html:/usr/local/apache2/htdocs/index.html httpd
0c51a31ee4c1412785864ef1ad820b8624e7e8f5338cb35c1a7d8f7afab3edf4
root@docker-lab:~/html# docker inspect 0c51 -f '{{.NetworkSettings.IPAddress}}'
172.17.0.11
root@docker-lab:~/html# curl http://172.17.0.11
Docker Volume bind test
root@docker-lab:~/html# curl http://127.0.0.1
Docker Volume bind test
4、确认busybox容器默认 /home 目录是空的
root@docker-lab:~/html# docker run -it busybox sh
/ # ls -a /home/
. ..
5、挂载目录到容器中,并验证只读参数
root@docker-lab:~/html# docker run -it -v /root/html:/home:ro busybox sh
/ # ls -a /home/
. .. index.html
/ # echo aaa > /home/testwrite
sh: can't create /home/testwrite: Read-only file system
6、挂载文件到容器中一个不存在的文件
root@docker-lab:~/html# docker run -it -v /root/html/index.html:/home/index.html2 busybox sh
/ # ls -a /home/
. .. index.html2
/ # cat /home/index.html2
Docker Volume bind test
7、挂载一个不存在的path到容器中也不存在的path,会在dockerhost和容器中分别创建对应的path,进行挂载
root@docker-lab:~/html# docker run -it -v /root/html/index.html2:/home/index.html busybox sh
/ # ls -a /home/
. .. index.html
/ # echo aaa > /home/index.html/testfile
/ # cat /home/index.html/testfile
aaa
/ # exit
root@docker-lab:~/html# ls
index.html index.html2
root@docker-lab:~/html# cat index.html2/testfile
aaa
- 第 6 章 存储 - 039 - Data Volume 之 bind mount
Data Volume Data Volume 本质上是 Docker Host 文件系统中的目录或文件,能够直接被 mount 到容器的文件系统中. Data Volume 有以下特点: 1.Dat ...
- Data Volume 之 bind mount - 每天5分钟玩转 Docker 容器技术(39)
storage driver 和 data volume 是容器存放数据的两种方式,上一节我们学习了 storage driver,本节开始讨论 Data Volume. Data Volume 本质 ...
- 37-Data Volume 之 bind mount
storage driver 和 data volume 是容器存放数据的两种方式,上一节我们学习了 storage driver,本节开始讨论 Data Volume. Data Volume 本质 ...
- docker的volume和bind mount究竟有什么区别?
不知道你在使用docker的时候,有没有注意到volume mount和bind mount的使用? 进一步说,他们之间的区别到底是什么? 接下来的内容,我们就为你揭开他们的神秘面纱. 相同之处 首先 ...
- 梦想MxWeb3D协同设计平台 2019.02.28更新
梦想MxWeb3D协同设计平台 2019.02.28更新 SDK开发包下载地址: http://www.mxdraw.com/ndetail_10130.html 在线演示网址: http://www ...
- 将数据挂载到 docker 容器中的3种方式:volume、bind mount、tmpfs
出处:https://deepzz.com/post/the-docker-volumes-basic.html
- Cheatsheet: 2018 11.01 ~ 2019 02.28
Golang FromXToGo micro - A microservice toolkit Other Easy parsing of Excel spreadsheet format with ...
- 2019.02.28 bzoj4199: [Noi2015]品酒大会(sam+线段树)
传送门 题意:给一个串,每个位置有一个权值,当S[s...s+len−1]=S[t...t+len−1]&&S[s...s+len]̸=S[t..t+len]S[s...s+len-1 ...
- 2019.02.28 bzoj3527: [Zjoi2014]力(fft)
传送门 fftfftfft菜题. 题意简述:给一个数列aia_iai,对于i=1→ni=1\rightarrow ni=1→n求出ansi=∑i<jai(i−j)2−∑i>jai(i−j ...
随机推荐
- bzoj4671: 异或图
bzoj4671: 异或图 Description 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 ( ...
- 洛谷AT2046 Namori(思维,基环树,树形DP)
洛谷题目传送门 神仙思维题还是要写点东西才好. 树 每次操作把相邻且同色的点反色,直接这样思考会发现状态有很强的后效性,没办法考虑转移. 因为树是二分图,所以我们转化模型:在树的奇数层的所有点上都有一 ...
- 洛谷SP22343 NORMA2 - Norma(分治,前缀和)
洛谷题目传送门 这题推式子恶心..... 考虑分治,每次统计跨过\(mid\)的所有区间的答案和.\(i\)从\(mid-1\)到\(l\)枚举,统计以\(i\)为左端点的所有区间. 我们先维护好\( ...
- 用户队列服务API
/// <summary> /// 用户队列服务API /// </summary> public interface ICustomerQueueManager : ISer ...
- Freescale 车身控制模块(BCM) 解决方案
中国汽车业已成为全球第一市场,标志着中国汽车产业进入了白热化竞争时代,因此,人们对汽车的操控性,安全性,易用性,舒适性,以及智能化要求也越来越高,更大的空间需求和更多的零部件因而产生了冲突,这就要求汽 ...
- Centos 6.x/7.x yum安装php5.6.X
鉴于Centos 默认yum源的php版本太低了,手动编译安装又有点一些麻烦,那么如何采用Yum安装的方案安装最新版呢.那么,今天我们就来学习下如何用yum安装php最新版. 1.检查当前安装的PHP ...
- Vagrant将下载好的镜像装载到本地中
Vagrant box add centos7 ${path}CentOS-7-x86_64-Vagrant-1803_01.VirtualBox Vagrant init ${名字} Vagrant ...
- spring中的@Bean是否一定要与@Configuration一起用
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/little_newBee/article/details/80383691 在使用sprin ...
- JSF action actionListner 详解
https://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener actionLi ...
- js 表单提交
方式一: 使用input type="submit" 提交 <form action="http://www.w3school.com.cn/tiy/loadtex ...