039、Data Volume 之 bind mount (2019-02-28 周四)

参考https://www.cnblogs.com/CloudMan6/p/7142150.html

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

原文地址:https://www.cnblogs.com/www1707/p/10448567.html

时间: 2024-08-01 01:37:58

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

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

storage driver 和 data volume 是容器存放数据的两种方式,上一节我们学习了 storage driver,本节开始讨论 Data Volume. Data Volume 本质上是 Docker Host 文件系统中的目录或文件,能够直接被 mount 到容器的文件系统中.Data Volume 有以下特点: Data Volume 是目录或文件,而非没有格式化的磁盘(块设备). 容器可以读写 volume 中的数据. volume 数据可以被永久的保存,即使使用它的容器

前端面试题2019.02.28

1.用 js 写出几种去除 string 空格的方法. (1).使用js循环   //供使用者调用 function trim(s){ return trimRight(trimLeft(s)); } //去掉左边的空白 function trimLeft(s){ if(s == null) { return ""; } var whitespace = new String(" \t\n\r"); var str = new String(s); if (whit

Docker的数据管理(volume/bind mount/tmpfs)

Docker提供了三种不同的方式用于将宿主的数据挂载到容器中:volumes,bind mounts,tmpfs volumes.当你不知道该选择哪种方式时,记住,volumes总是正确的选择. volumes是Docker数据持久化机制.bind mounts依赖主机目录结构,volumes完全由Docker管理.Volumes有以下优点: Volumes更容易备份和移植. 可以通过Docker CLI或API进行管理 Volumes可以无区别的工作中Windows和Linux下. 多个容器共

Docker Bind Mount 与 Volume

为了防止docker里序列化数据的丢失,docker采用了挂载的方式 volume docker -v volumeName:containerPath 或者 docker -v containerPath 这种方式,linux中,docker会在/var/lib/docker/volumes下生成volumeName目录,将containerPath的目录挂载在/var/lib/docker/volumes/$volumeName/_data里 如果不指定volumeName,将随机生成一个文

Docker-3:Data Volume

Add a volume You can also use the VOLUME instruction in a Dockerfile to add one or more new volumes to any container created from that image. docker run -d -P --name web -v /webapp training/webapp python app.py #add a volume /webapp for container web

docker 使用Data Volume 共享文件

Adding a data volume You can add a data volume to a container using the -v flag with the docker run command. You can use the -v multiple times in a single docker run to mount multiple data volumes. Let's mount a single volume now in our web applicati

MSc Data Mining and Machine Learning (2019)

MSc Data Mining and Machine Learning (2019)Lab 4 – Neural NetworksProblemThe challenge is to implement the Error Back-Propagation (EBP) training algorithm for a multilayerperceptron (MLP) 4-2-4 encoder [1] using MatLab (or your language of choice). I

【谜客帝国】第147届月思主擂谜会(2019.02.15)

 [谜客帝国]第147届月思主擂谜会(2019.02.15) 主持计分:东东 1.“人在中天日月间”(9笔字)春/月思 [注:面出陈孚<开平即事二首>,“势超大地山河上,-.”] 2. 玉漏声中烟气袅(3字法国奢侈品牌)YSL/月思 3. 双双相念初相爱(2字著名动漫人物)菜菜/月思 4.“数点燕云州外.雪霜威”(足球用语二,4+3)4132.451/月思 [注:面出余文<相见欢>,“登高望断龙旗,未曾归.几度中原北定,梦依稀.朔风乱,胡尘漫,掩斜晖.-.”] 5.“十载同心如一人

Docker 持久化存储, Data Volume

docker容器, 再启动之后 我们可以对其进行 修改删除等等.如果是一个数据库的容器, 里面的数据 不想随着这个容器的消失, 而消失.  就需要持久化数据存储. Data Volume   这是 docker hub 上面  mysql 的Dockerfile 这里的 VOLUME 意思就是, 将产生的数据 写入到当前主机的 /var/lib/mysql 里面. [[email protected] ~]$ docker images REPOSITORY TAG IMAGE ID CREAT