Docker容器資料卷volumes-from
阿新 • • 發佈:2018-11-28
定義4個終端:
- 終端host
- 終端container dc01
- 終端container dc02
- 終端container dc03
各個容器之間的關係:
1、啟動一個父容器dc01
啟動一個父容器dc01,並在 dataVolumeContainer2 新增內容。
命令格式:docker run -it --name dc01 zzyy/centos
操作步驟如下:
終端container dc01:
建立並啟動dc01容器,並在 dataVolumeContainer2 目錄中新增 dc01.txt
[[email protected] mydocker]# docker run -it --name dc01 zzyy/centos
[[email protected] /]# 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
[ [email protected] /]# cd dataVolumeContainer2/
[[email protected] dataVolumeContainer2]# ls
[[email protected] dataVolumeContainer2]# echo "container dc01 add"> dc01.txt
[[email protected] dataVolumeContainer2]# ls
dc01.txt
[[email protected] dataVolumeContainer2]# cat dc01.txt
container dc01 add
[ [email protected] dataVolumeContainer2]#
終端host:
驗證容器啟動情況
[[email protected] _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
[[email protected] _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
[[email protected] java]# docker run -it --name dc02 --volumes-from dc01 zzyy/centos
[[email protected] /]# 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
[[email protected] /]# cd d
dataVolumeContainer1/ dataVolumeContainer2/ dev/
[[email protected] /]# cd cd dataVolumeContainer2/
bash: cd: cd: No such file or directory
[[email protected] /]# cd dataVolumeContainer2/
[[email protected] dataVolumeContainer2]# ls
dc01.txt
[[email protected] dataVolumeContainer2]# cat dc01.txt
container dc01 add
[[email protected] dataVolumeContainer2]#
[[email protected] dataVolumeContainer2]# echo "container dc02" > dc02.txt
[[email protected] dataVolumeContainer2]# ls
dc01.txt dc02.txt
[[email protected] dataVolumeContainer2]#
終端container dc03:
建立並啟動dc03容器,並在 dataVolumeContainer2 目錄中新增 dc03.txt
[[email protected] java]# docker run -it --name dc03 --volumes-from dc01 zzyy/centos
[[email protected] /]# ls
anaconda-post.log bin dataVolumeContainer1 dataVolumeContainer2 dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[[email protected] /]# 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
[[email protected] /]# cd dataVolumeContainer2/
[[email protected] dataVolumeContainer2]# ls
dc01.txt
[[email protected] dataVolumeContainer2]# ls
dc01.txt dc02.txt
[[email protected] dataVolumeContainer2]# echo "container dc03 addd" > dc03.txt
[[email protected] dataVolumeContainer2]# ls
dc01.txt dc02.txt dc03.txt
[[email protected] dataVolumeContainer2]#
終端host:
檢視容器執行情況:
[[email protected] _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
[[email protected] _data]#
dc01、dc02、dc03執行正常。
3、回到dc01可以看到02/03各自新增的都能共享
終端container dc01:
[[email protected] dataVolumeContainer2]# ls
dc01.txt dc02.txt dc03.txt
[[email protected] dataVolumeContainer2]#
4、刪除dc01,dc02修改後,dc03可否訪問
終端host:
刪除dc01
[[email protected] _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
[[email protected] _data]# docker rm -f 3caa375ebfd9
3caa375ebfd9
[[email protected] _data]#
終端container dc02:
在dc02中新增新檔案,並且可以看見dc01雖然被刪除了,但是dc01的建立的檔案還在。
[[email protected] dataVolumeContainer2]# ls
dc01.txt dc02.txt dc03.txt
[[email protected] dataVolumeContainer2]# echo "container dc02 add 22" > dc02_update.txt
[[email protected] dataVolumeContainer2]# ls
dc01.txt dc02.txt dc02_update.txt dc03.txt
[[email protected] dataVolumeContainer2]#
終端container dc03:
dc01.txt 還在,dc01.txt中的內容也能獲取。
新建立的 dc02_update.txt 也在。
[[email protected] dataVolumeContainer2]# ls
dc01.txt dc02.txt dc02_update.txt dc03.txt
[[email protected] dataVolumeContainer2]# cat dc01.txt
container dc01 add
[[email protected] dataVolumeContainer2]#
5、刪除dc02後,dc03可否訪問
6、新建dc04,繼承dc03後,再刪除dc03
結論:容器之間配置資訊的傳遞,資料卷的生命週期一直持續到沒有容器使用它為止。