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
 
 
 

039、Data Volume 之 bind mount (2019-02-28 周四)的更多相关文章

  1. 第 6 章 存储 - 039 - Data Volume 之 bind mount

    Data Volume Data Volume 本质上是 Docker Host 文件系统中的目录或文件,能够直接被 mount 到容器的文件系统中. Data Volume 有以下特点: 1.Dat ...

  2. Data Volume 之 bind mount - 每天5分钟玩转 Docker 容器技术(39)

    storage driver 和 data volume 是容器存放数据的两种方式,上一节我们学习了 storage driver,本节开始讨论 Data Volume. Data Volume 本质 ...

  3. 37-Data Volume 之 bind mount

    storage driver 和 data volume 是容器存放数据的两种方式,上一节我们学习了 storage driver,本节开始讨论 Data Volume. Data Volume 本质 ...

  4. docker的volume和bind mount究竟有什么区别?

    不知道你在使用docker的时候,有没有注意到volume mount和bind mount的使用? 进一步说,他们之间的区别到底是什么? 接下来的内容,我们就为你揭开他们的神秘面纱. 相同之处 首先 ...

  5. 梦想MxWeb3D协同设计平台 2019.02.28更新

    梦想MxWeb3D协同设计平台 2019.02.28更新 SDK开发包下载地址: http://www.mxdraw.com/ndetail_10130.html 在线演示网址: http://www ...

  6. 将数据挂载到 docker 容器中的3种方式:volume、bind mount、tmpfs

    出处:https://deepzz.com/post/the-docker-volumes-basic.html

  7. Cheatsheet: 2018 11.01 ~ 2019 02.28

    Golang FromXToGo micro - A microservice toolkit Other Easy parsing of Excel spreadsheet format with ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. bzoj2212[Poi2011]Tree Rotations [线段树合并]

    题面 bzoj ans = 两子树ans + min(左子在前逆序对数, 右子在前逆序对数) 线段树合并 #include <cstdio> #include <cstdlib> ...

  2. Educational Codeforces Round 54 [Rated for Div. 2] (CF1076)

    第一次在宿舍打CF 把同宿舍的妹子吵得不行... 特此抱歉QAQ A 题意:给定一个字符串, 最多删掉一个字符,使得剩余字符串字典序最小 n<=2e5 当然"最多"是假的 删 ...

  3. 关于360插件化Replugin Activity动态修改父类的字节码操作

    近期在接入360插件化方案Replugin时,发现出现崩溃情况. 大概崩溃内容如下: aused by: java.lang.ClassNotFoundException: Didn't find c ...

  4. Android 可展开列表组件 ExpandableListView

    ExpandableListView 是ListView的子类,它把应用中的列表项分为几组,每组里可包含多个列表项. 所显示的列表项应该由ExpandableListAdapter 接口提供,实现Ex ...

  5. [luogu5003]跳舞的线【动态规划】

    题目描述 线现在在一个地图上,它正在(1,1)上(左上角),最终要去到(M,N)上.它不但只能往下或往右走,还只能在整数格子上移动. Imakf有的时候想要炫技,又有时想偷懒,所以他会告诉你这张地图的 ...

  6. 「SCOI2015」国旗计划 解题报告

    「SCOI2015」国旗计划 蛮有趣的一个题 注意到区间互不交错,那么如果我们已经钦定了一个区间,它选择的下一个区间是唯一的,就是和它有交且右端点在最右边的,这个可以单调队列预处理一下 然后往后面跳拿 ...

  7. 浅析 @PathVariable 和 @RequestParam(转发,非原创)

    首先 上两个地址:地址①http://localhost:8989/SSSP/emps?pageNo=2地址②http://localhost:8989/SSSP/emp/7如果想获取地址①中的 pa ...

  8. JAVA中循环删除list中元素

    文章来源: https://www.cnblogs.com/pcheng/p/5336903.html JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常说的foreach循环) ...

  9. JavaScript深入系列(一)--原型和原型链详解

    构造函数创建对象 首先我们先使用构造函数创建一个对象: function Person(){} var person = new Person(); person.name = 'tom'; cons ...

  10. T4模版 mysql

    MysqlDbhelper.ttinclude <#@ assembly name="System.Core"#> <#@ assembly name=" ...