命名的容器挂载数据卷,其它容器通过挂载这个(父容器)实现数据共享,挂载数据卷的容器,称之为数据卷容器。
看图说文:
(1) 上图中dc1容器挂载了数据卷
(2) dc2和dc3使用“--volumes-from”选项直接继承自dc1容器。因此,也能够访问数据卷,dc1/dc2/dc3 三个容器之间可以通过数据卷共享数据。
① 如果删除dc1容器,则dc2和dc3还是可以通过数据卷共享数据。
② 如果删除dc3容器,则dc2还是可以通过数据卷共享数据。
(3) 新建dc4容器,该容器继承dc2容器,dc4还是能够访问数据卷。
① 如果删除dc2容器,则新创建的dc4容器还是能够访问数据卷。
实例:先启动一个父容器dc1;dc2/dc3容器继承自dc1(使用--volumes-from命令);
(一)首先,使用“docker run -it --name dc1 -v /myDataVolume:/dataVolume centos”命令启动父容器dc1。如下:
[root@localhost ~]# docker run -it --name dc1 -v /myDataVolume:/dataVolume centos [root@0c90abf2767d /]# ll /dataVolume/ total 0
上面成功创建了dc1容器。然后使用“docker ps”查看新创建的容器,如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0c90abf2767d centos "/bin/bash" About a minute ago Up About a minute dc1
(二)使用“--volumes-from”选项启动dc2/dc3容器,从dc1继承数据卷。启动dc2,命令为“docker run -it --names dc2 --volumes-from dc1 centos”。如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0c90abf2767d centos "/bin/bash" 3 minutes ago Up 3 minutes dc1 [root@localhost ~]# docker run -it --name dc2 --volumes-from dc1 centos [root@e74c92ee0f01 /]# ll /dataVolume/ total 0 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e74c92ee0f01 centos "/bin/bash" 53 seconds ago Up 52 seconds dc2 0c90abf2767d centos "/bin/bash" 5 minutes ago Up 5 minutes dc1
启动dc3如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e74c92ee0f01 centos "/bin/bash" About a minute ago Up About a minute dc2 0c90abf2767d centos "/bin/bash" 5 minutes ago Up 5 minutes dc1 [root@localhost ~]# docker run -it --name dc3 --volumes-from dc1 centos [root@946ad8b067aa /]# ll /dataVolume/ total 0 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" About a minute ago Up 59 seconds dc3 e74c92ee0f01 centos "/bin/bash" 3 minutes ago Up 3 minutes dc2 0c90abf2767d centos "/bin/bash" 7 minutes ago Up 7 minutes dc1
(三)我们创建数据到dc2/dc3的/dataVolume目录,然后进入到dc1的/dataVolume目录查看文件。如下:
a、向dc2的/dataVolume目录写入数据:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 3 minutes ago Up 3 minutes dc3 e74c92ee0f01 centos "/bin/bash" 5 minutes ago Up 5 minutes dc2 0c90abf2767d centos "/bin/bash" 9 minutes ago Up 9 minutes dc1 [root@localhost ~]# docker attach e74c92ee0f01 [root@e74c92ee0f01 /]# cd dataVolume/ [root@e74c92ee0f01 dataVolume]# touch dc2.txt [root@e74c92ee0f01 dataVolume]# ll total 0 -rw-r--r--. 1 root root 0 Jun 30 10:07 dc2.txt
b、向dc3的/dataVolume目录写入数据:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 5 minutes ago Up 5 minutes dc3 e74c92ee0f01 centos "/bin/bash" 8 minutes ago Up 8 minutes dc2 0c90abf2767d centos "/bin/bash" 12 minutes ago Up 12 minutes dc1 [root@localhost ~]# docker attach 946ad8b067aa [root@946ad8b067aa /]# ll /dataVolume/ total 0 -rw-r--r--. 1 root root 0 Jun 30 10:07 dc2.txt [root@946ad8b067aa /]# touch /dataVolume/dc3.txt [root@946ad8b067aa /]# ll /dataVolume/ total 0 -rw-r--r--. 1 root root 0 Jun 30 10:07 dc2.txt
从上面最终结果可知,我们在dc3中能够看见dc2刚刚创建的dc2.txt文件。说明:dc2和dc3之间数据是共享的,可以相互访问。
(1) 向dc2/dc3写入数据,进入dc1验证是否能够查看dc2/dc3新添加的数据?能查看
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 8 minutes ago Up 8 minutes dc3 e74c92ee0f01 centos "/bin/bash" 10 minutes ago Up 10 minutes dc2 0c90abf2767d centos "/bin/bash" 14 minutes ago Up 14 minutes dc1 [root@localhost ~]# docker attach 0c90abf2767d [root@0c90abf2767d /]# ll /dataVolume/ total 0 -rw-r--r--. 1 root root 0 Jun 30 10:07 dc2.txt -rw-r--r--. 1 root root 0 Jun 30 10:12 dc3.txt
(2) 删除dc1,验证dc2修改后dc3可否访问?能访问
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 11 minutes ago Up 11 minutes dc3 e74c92ee0f01 centos "/bin/bash" 13 minutes ago Up 13 minutes dc2 0c90abf2767d centos "/bin/bash" 17 minutes ago Up 17 minutes dc1 [root@localhost ~]# docker stop 0c90abf2767d 0c90abf2767d [root@localhost ~]# docker rm 0c90abf2767d 0c90abf2767d
上面,先使用“docker stop”停止dc1容器,然后使用“docker rm”删除dc1容器。查看dc2/dc3的数据。如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 12 minutes ago Up 12 minutes dc3 e74c92ee0f01 centos "/bin/bash" 15 minutes ago Up 15 minutes dc2 [root@localhost ~]# [root@localhost ~]# docker exec -t e74c92ee0f01 ls /dataVolume dc2.txt dc3.txt [root@localhost ~]# docker exec -t 946ad8b067aa ls /dataVolume dc2.txt dc3.txt
(3) 删除dc2后,dc3能够访问?能访问
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 14 minutes ago Up 14 minutes dc3 e74c92ee0f01 centos "/bin/bash" 16 minutes ago Up 16 minutes dc2 [root@localhost ~]# docker stop e74c92ee0f01 e74c92ee0f01 [root@localhost ~]# docker rm e74c92ee0f01 e74c92ee0f01 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 15 minutes ago Up 14 minutes dc3 [root@localhost ~]# docker exec -t 946ad8b067aa ls /dataVolume dc2.txt dc3.txt
上面,先使用“docker stop”停止dc2容器,“docker rm”删除dc2容器;最后使用“docker exec -t”命令查看dc3容器的/dataVolume目录,任然是可以访问的。
(4) 新建dc4继承dc3,再删除dc3,查看dc4的/dataVolume目录是否能够访问?能访问
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 946ad8b067aa centos "/bin/bash" 16 minutes ago Up 16 minutes dc3 [root@localhost ~]# docker run -it --name dc4 --volumes-from dc3 centos [root@b0daef3a3145 /]# ll /dataVolume/ total 0 -rw-r--r--. 1 root root 0 Jun 30 10:07 dc2.txt -rw-r--r--. 1 root root 0 Jun 30 10:12 dc3.txt
上面新运行dc4容器,继承dc3容器。使用docker ps查看如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b0daef3a3145 centos "/bin/bash" 54 seconds ago Up 52 seconds dc4 946ad8b067aa centos "/bin/bash" 17 minutes ago Up 17 minutes dc3
删除dc3容器,查看dc4的/dataVolume目录。如下:
[root@localhost ~]# docker stop 946ad8b067aa 946ad8b067aa [root@localhost ~]# docker rm 946ad8b067aa 946ad8b067aa [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b0daef3a3145 centos "/bin/bash" 2 minutes ago Up 2 minutes dc4
上面成功删除了dc3容器。下面将使用“docker attach”进入到dc4容器,如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b0daef3a3145 centos "/bin/bash" 2 minutes ago Up 2 minutes dc4 [root@localhost ~]# docker attach b0daef3a3145 [root@b0daef3a3145 /]# ll /dataVolume/ total 0 -rw-r--r--. 1 root root 0 Jun 30 10:07 dc2.txt -rw-r--r--. 1 root root 0 Jun 30 10:12 dc3.txt
总结:容器之间配置信息的传递,数据卷的生命周期一直持续到没有容器使用它为止。