码迷,mamicode.com
首页 > 其他好文 > 详细

Docker之数据卷和数据卷容器

时间:2017-08-03 20:08:42      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:docker   数据卷   数据卷容器   

数据卷是一个可供容器使用的特殊目录,可以在容器之间共享和重用,对数据卷的修改会马上生效,卷会一直存在直到没有容器使用,数据卷的使用类似于mount

数据卷容器就是一个普通的容器,可专门提供数据卷供其他容器挂载使用,可以在容器之间共享一些持续更新的数据


1:创建数据卷

利用镜像centos,创建一个名为tomcat的容器,并在后台以守护进程方式工作,且将本地的/b2b/目录挂在在新建立的tomcat容器的/test目录下

root@My-Ubuntu:/home/zxl# docker run -d --name=tomcat -v /b2b:/test -it centos 

3865efc4c698fe74c4975e18ca80f113130e8c76e0d4f175dc53e22f68a9ec15

进入tomcat容器,并启动/bin/bash

root@My-Ubuntu:/home/zxl# docker exec -it tomcat /bin/bash

[root@3865efc4c698 /]# ls /test/

在tomcat容器的/test目录下创建一个叫haha的文件

[root@3865efc4c698 /]# touch /test/haha

[root@3865efc4c698 /]# ls /test/

haha

[root@3865efc4c698 /]# exit

exit

退出tomcat容器后,进入本地的/b2b/目录下

root@My-Ubuntu:/home/zxl# cd /b2b/

可以看到高才在tomcat容器中创建的文件在

root@My-Ubuntu:/b2b# ll

total 8

drwxr-xr-x  2 root root 4096 Aug  3 11:18 ./

drwxr-xr-x 24 root root 4096 Aug  3 11:17 ../

-rw-r--r--  1 root root    0 Aug  3 11:18 haha


2:创建数据卷容器

首先用centos镜像创建一个叫dbdata的数据卷容器,并将本地的/dbdata目录挂载到dbdata数据卷容器的/dbdata目录下,也可以不挂载

root@My-Ubuntu:/b2b# docker run -it -v /dbdata:/dbdata --name dbdata centos

[root@6f23465f05f3 /]# exit

exit

再用centos镜像新创建db1和db2两个容器,并用--volumes-from 参数挂载dbdata数据卷容器上的数据卷

root@My-Ubuntu:/b2b# docker run -it --volumes-from dbdata --name db1 centos

[root@be670cd7fd5b /]# exit

exit

root@My-Ubuntu:/b2b# docker run -it --volumes-from dbdata --name db2 centos

[root@8fe9f2b7fe15 /]# exit 

exit

启动db1容器

root@My-Ubuntu:/b2b# docker start db1

db1

进入到db1容器中的/dbdata目录下,创建一个叫做hehehe的文件

root@My-Ubuntu:/b2b# docker exec -it db1 /bin/bash

[root@be670cd7fd5b /]# 

[root@be670cd7fd5b /]# cd /dbdata/

[root@be670cd7fd5b dbdata]# ll

total 0

[root@be670cd7fd5b dbdata]# touch hehehe

[root@be670cd7fd5b dbdata]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 04:02 hehehe

[root@be670cd7fd5b dbdata]# exit

exit

启动db1容器

root@My-Ubuntu:/b2b# docker start db2

db2

进入到db1容器中的/dbdata目录下,可以看到db1创建的hehehe文件,并创建一个叫做hahaha的文件

root@My-Ubuntu:/b2b# docker exec -it db2 /bin/bash 

[root@8fe9f2b7fe15 /]# cd /dbdata/

[root@8fe9f2b7fe15 dbdata]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 04:02 hehehe

[root@8fe9f2b7fe15 dbdata]# touch hahaha

[root@8fe9f2b7fe15 dbdata]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 04:05 hahaha

-rw-r--r-- 1 root root 0 Aug  3 04:02 hehehe

[root@8fe9f2b7fe15 dbdata]# exit 

exit

启动并进入数据卷容器dbdata

root@My-Ubuntu:/b2b# docker start dbdata 

dbdata

root@My-Ubuntu:/b2b# docker exec -it dbdata /bin/bash 

[root@6f23465f05f3 /]# cd /dbdata/

在/dbdata目录下可以看到db1和db2创建的文件,且创建一个叫aaaa的文件

[root@6f23465f05f3 dbdata]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 04:05 hahaha

-rw-r--r-- 1 root root 0 Aug  3 04:02 hehehe


[root@6f23465f05f3 dbdata]# touch aaaaa

[root@6f23465f05f3 dbdata]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 07:24 aaaaa

-rw-r--r-- 1 root root 0 Aug  3 04:05 hahaha

-rw-r--r-- 1 root root 0 Aug  3 04:02 hehehe

将hehehe文件删除

[root@6f23465f05f3 /]# rm /dbdata/hehehe 

rm: remove regular empty file ‘/dbdata/hehehe‘? y

[root@6f23465f05f3 /]# cd /dbdata/

[root@6f23465f05f3 dbdata]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 07:24 aaaaa

-rw-r--r-- 1 root root 0 Aug  3 04:05 hahaha

[root@6f23465f05f3 dbdata]# exit

exit

进入db1容器,可以看到aaaa且hehehe被删除了

root@My-Ubuntu:/b2b# docker exec -it db1 /bin/bash

[root@be670cd7fd5b /]# cd /dbdata/

[root@be670cd7fd5b dbdata]# ll

total 0

-rw-r--r-- 1 root root 0 Aug  3 07:24 aaaaa

-rw-r--r-- 1 root root 0 Aug  3 04:05 hahaha


在本地的/dbdata目录下叶可以看到文件,因为在创建数据卷容器dbdata时,将本地的/dbdata目录挂在到了数据卷容器的/dbdata目录下

root@My-Ubuntu:/b2b# cd /dbdata/

root@My-Ubuntu:/dbdata# ll

total 8

drwxr-xr-x  2 root root 4096 Aug  3 15:25 ./

drwxr-xr-x 25 root root 4096 Aug  3 11:41 ../

-rw-r--r--  1 root root    0 Aug  3 15:24 aaaaa

-rw-r--r--  1 root root    0 Aug  3 12:05 hahaha


本文出自 “11097124” 博客,请务必保留此出处http://11107124.blog.51cto.com/11097124/1953361

Docker之数据卷和数据卷容器

标签:docker   数据卷   数据卷容器   

原文地址:http://11107124.blog.51cto.com/11097124/1953361

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!