数据卷是一个可供容器使用的特殊目录,能够在容器之间共享和重用,对数据卷的修改会立刻生效,卷会一直存在直到没有容器使用,数据卷的使用相似于mount
docker
数据卷容器就是一个普通的容器,可专门提供数据卷供其余容器挂载使用,能够在容器之间共享一些持续更新的数据centos
1:建立数据卷tomcat
利用镜像centos,建立一个名为tomcat的容器,并在后台以守护进程方式工做,且将本地的/b2b/目录挂在在新创建的tomcat容器的/test目录下bash
root@My-Ubuntu:/home/zxl# docker run -d --name=tomcat -v /b2b:/test -it centos ide
3865efc4c698fe74c4975e18ca80f113130e8c76e0d4f175dc53e22f68a9ec15spa
进入tomcat容器,并启动/bin/bash
进程
root@My-Ubuntu:/home/zxl# docker exec -it tomcat /bin/bashrem
[root@3865efc4c698 /]# ls /test/it
在tomcat容器的/test目录下建立一个叫haha的文件class
[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