Docker容器数据卷volumes-from

定义4个终端:

  1. 终端host
  2. 终端container dc01
  3. 终端container dc02
  4. 终端container dc03

各个容器之间的关系:

1、启动一个父容器dc01

启动一个父容器dc01,并在 dataVolumeContainer2 新增内容。

命令格式:docker run -it --name dc01 zzyy/centos

操作步骤如下:

终端container dc01:

创建并启动dc01容器,并在 dataVolumeContainer2 目录中添加 dc01.txt

[root@localhost mydocker]# docker run -it --name dc01 zzyy/centos
[root@3caa375ebfd9 /]# ll
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   2 root root     6 Nov 27 03:02 dataVolumeContainer1
drwxr-xr-x.   2 root root     6 Nov 27 03:02 dataVolumeContainer2
drwxr-xr-x.   5 root root   360 Nov 27 03:02 dev
drwxr-xr-x.   1 root root    66 Nov 27 03:02 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 264 root root     0 Nov 27 03:02 proc
dr-xr-x---.   2 root root   114 Oct  6 19:15 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 27 02:53 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var
[root@3caa375ebfd9 /]# cd dataVolumeContainer2/
[root@3caa375ebfd9 dataVolumeContainer2]# ls
[root@3caa375ebfd9 dataVolumeContainer2]# echo "container dc01 add"> dc01.txt
[root@3caa375ebfd9 dataVolumeContainer2]# ls
dc01.txt
[root@3caa375ebfd9 dataVolumeContainer2]# cat dc01.txt 
container dc01 add
[root@3caa375ebfd9 dataVolumeContainer2]# 

终端host:
验证容器启动情况

[root@localhost _data]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3caa375ebfd9        zzyy/centos         "/bin/sh -c /bin/bash"   22 minutes ago      Up 22 minutes                           dc01
[root@localhost _data]# 

2、dc02/dc03继承自dc01

–volumes-form
dc02/dc03分别在dataVolumeContainer2各自新增内容

命令格式

dc02的 格式:

docker run -it --name dc02 --volumes-from dc01 zzyy/centos

dc03的格式:

docker run -it --name dc03 --volumes-from dc01 zzyy/centos

操作步骤如下:

终端container dc02:

创建并启动dc02容器,并在 dataVolumeContainer2 目录中添加 dc02.txt

[root@localhost java]# docker run -it --name dc02 --volumes-from dc01 zzyy/centos
[root@7dbdb68f3e73 /]# ll
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   2 root root     6 Nov 27 03:02 dataVolumeContainer1
drwxr-xr-x.   2 root root    22 Nov 27 03:09 dataVolumeContainer2
drwxr-xr-x.   5 root root   360 Nov 27 03:17 dev
drwxr-xr-x.   1 root root    66 Nov 27 03:17 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 270 root root     0 Nov 27 03:17 proc
dr-xr-x---.   2 root root   114 Oct  6 19:15 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 27 02:53 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var
[root@7dbdb68f3e73 /]# cd  d
dataVolumeContainer1/ dataVolumeContainer2/ dev/                  
[root@7dbdb68f3e73 /]# cd  cd dataVolumeContainer2/
bash: cd: cd: No such file or directory
[root@7dbdb68f3e73 /]# cd  dataVolumeContainer2/
[root@7dbdb68f3e73 dataVolumeContainer2]# ls
dc01.txt
[root@7dbdb68f3e73 dataVolumeContainer2]# cat dc01.txt 
container dc01 add
[root@7dbdb68f3e73 dataVolumeContainer2]# 
[root@7dbdb68f3e73 dataVolumeContainer2]# echo "container dc02" > dc02.txt
[root@7dbdb68f3e73 dataVolumeContainer2]# ls
dc01.txt  dc02.txt
[root@7dbdb68f3e73 dataVolumeContainer2]# 

终端container dc03:

创建并启动dc03容器,并在 dataVolumeContainer2 目录中添加 dc03.txt

[root@localhost java]# docker run -it --name dc03 --volumes-from dc01 zzyy/centos
[root@4dfdba337ef2 /]# ls
anaconda-post.log  bin  dataVolumeContainer1  dataVolumeContainer2  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@4dfdba337ef2 /]# ll
total 12
-rw-r--r--.   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x.   2 root root     6 Nov 27 03:02 dataVolumeContainer1
drwxr-xr-x.   2 root root    22 Nov 27 03:09 dataVolumeContainer2
drwxr-xr-x.   5 root root   360 Nov 27 03:33 dev
drwxr-xr-x.   1 root root    66 Nov 27 03:33 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 272 root root     0 Nov 27 03:33 proc
dr-xr-x---.   2 root root   114 Oct  6 19:15 root
drwxr-xr-x.  10 root root   130 Oct  6 19:15 run
lrwxrwxrwx.   1 root root     8 Oct  6 19:14 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Nov 27 02:53 sys
drwxrwxrwt.   7 root root   132 Oct  6 19:15 tmp
drwxr-xr-x.  13 root root   155 Oct  6 19:14 usr
drwxr-xr-x.  18 root root   238 Oct  6 19:14 var
[root@4dfdba337ef2 /]# cd dataVolumeContainer2/
[root@4dfdba337ef2 dataVolumeContainer2]# ls
dc01.txt
[root@4dfdba337ef2 dataVolumeContainer2]# ls
dc01.txt  dc02.txt
[root@4dfdba337ef2 dataVolumeContainer2]# echo "container dc03 addd" > dc03.txt
[root@4dfdba337ef2 dataVolumeContainer2]# ls
dc01.txt  dc02.txt  dc03.txt
[root@4dfdba337ef2 dataVolumeContainer2]# 

终端host:
查看容器运行情况:

[root@localhost _data]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4dfdba337ef2        zzyy/centos         "/bin/sh -c /bin/bash"   3 minutes ago       Up 3 minutes                            dc03
7dbdb68f3e73        zzyy/centos         "/bin/sh -c /bin/bash"   19 minutes ago      Up 19 minutes                           dc02
3caa375ebfd9        zzyy/centos         "/bin/sh -c /bin/bash"   34 minutes ago      Up 34 minutes                           dc01
[root@localhost _data]# 

dc01、dc02、dc03运行正常。

3、回到dc01可以看到02/03各自添加的都能共享

终端container dc01:

[root@3caa375ebfd9 dataVolumeContainer2]# ls
dc01.txt  dc02.txt  dc03.txt
[root@3caa375ebfd9 dataVolumeContainer2]# 

4、删除dc01,dc02修改后,dc03可否访问

终端host:
删除dc01

[root@localhost _data]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4dfdba337ef2        zzyy/centos         "/bin/sh -c /bin/bash"   2 hours ago         Up 2 hours                              dc03
7dbdb68f3e73        zzyy/centos         "/bin/sh -c /bin/bash"   3 hours ago         Up 3 hours                              dc02
3caa375ebfd9        zzyy/centos         "/bin/sh -c /bin/bash"   3 hours ago         Up 2 hours                              dc01
[root@localhost _data]# docker rm -f 3caa375ebfd9
3caa375ebfd9
[root@localhost _data]# 

终端container dc02:
在dc02中添加新文件,并且可以看见dc01虽然被删除了,但是dc01的创建的文件还在。

[root@7dbdb68f3e73 dataVolumeContainer2]# ls
dc01.txt  dc02.txt  dc03.txt
[root@7dbdb68f3e73 dataVolumeContainer2]# echo "container dc02 add 22" > dc02_update.txt
[root@7dbdb68f3e73 dataVolumeContainer2]# ls
dc01.txt  dc02.txt  dc02_update.txt  dc03.txt
[root@7dbdb68f3e73 dataVolumeContainer2]# 

终端container dc03:
dc01.txt 还在,dc01.txt中的内容也能获取。
新创建的 dc02_update.txt 也在。

[root@4dfdba337ef2 dataVolumeContainer2]# ls
dc01.txt  dc02.txt  dc02_update.txt  dc03.txt
[root@4dfdba337ef2 dataVolumeContainer2]# cat dc01.txt 
container dc01 add
[root@4dfdba337ef2 dataVolumeContainer2]# 

5、删除dc02后,dc03可否访问

6、新建dc04,继承dc03后,再删除dc03

结论:容器之间配置信息的传递,数据卷的生命周期一直持续到没有容器使用它为止。

你可能感兴趣的:(docker)