docker 6:容器之间共享磁盘

创建三个容器,然后共享磁盘,实现文件在三个容器之间传递同步修改。

创建容器docker01,然后创建docker02和docker03继承docker01,即可实现文件的共享。

[root@localhost ~]# docker images 
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
volume1/centos        latest    04b2be41fc6c   3 hours ago    209MB
1. 创建并运行docker01:
[root@localhost ~]# docker run -it --name docker01 volume1/centos
[root@cc5648abda23 /]# ls -l
total 0
lrwxrwxrwx.   1 root root   7 Nov  3 15:22 bin -> usr/bin
drwxr-xr-x.   2 root root   6 Apr  1 10:08 dataVolume1
drwxr-xr-x.   2 root root   6 Apr  1 10:08 dataVolume2

  进入共享目录,创建1.txt [root@cc5648abda23 /]# cd /dataVolume1 [root@cc5648abda23 dataVolume1]# touch 1.txt [root@cc5648abda23 dataVolume1]# ls 1.txt [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cc5648abda23 volume1/centos "/bin/sh -c /bin/bash" 22 seconds ago Up 19 seconds docker01
2.创建并运行docker02:
[root@localhost ~]# docker run -it --name docker02 --volumes-from docker01 volume1/centos [root@d3029b48260e /]# ls -l total 0 lrwxrwxrwx. 1 root root 7 Nov 3 15:22 bin -> usr/bin drwxr-xr-x. 2 root root 19 Apr 1 10:09 dataVolume1 drwxr-xr-x. 2 root root 6 Apr 1 10:08 dataVolume2
  可以看到1.txt,创建2.txt [root@d3029b48260e /]# cd dataVolume1 [root@d3029b48260e dataVolume1]# ls -l total 0 -rw-r--r--. 1 root root 0 Apr 1 10:09 1.txt [root@d3029b48260e dataVolume1]# touch 2.txt [root@d3029b48260e dataVolume1]# ls -l total 0 -rw-r--r--. 1 root root 0 Apr 1 10:09 1.txt -rw-r--r--. 1 root root 0 Apr 1 10:11 2.txt
3.创建并运行docker03: [root@localhost ~]# docker run -it --name docker03 --volumes-from docker01 volume1/centos [root@70c8a7164cda /]# ls -l total 0 lrwxrwxrwx. 1 root root 7 Nov 3 15:22 bin -> usr/bin drwxr-xr-x. 2 root root 32 Apr 1 10:11 dataVolume1 drwxr-xr-x. 2 root root 6 Apr 1 10:08 dataVolume2
  可以看到1.txt和2.txt,创建3.txt [root@70c8a7164cda /]# cd dataVolume1 [root@70c8a7164cda dataVolume1]# ls -l total 0 -rw-r--r--. 1 root root 0 Apr 1 10:09 1.txt -rw-r--r--. 1 root root 0 Apr 1 10:11 2.txt [root@70c8a7164cda dataVolume1]# touch 3.txt [root@70c8a7164cda dataVolume1]# ls -l total 0 -rw-r--r--. 1 root root 0 Apr 1 10:09 1.txt -rw-r--r--. 1 root root 0 Apr 1 10:11 2.txt -rw-r--r--. 1 root root 0 Apr 1 10:12 3.txt [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 70c8a7164cda volume1/centos "/bin/sh -c /bin/bash" 40 seconds ago Up 38 seconds docker03 d3029b48260e volume1/centos "/bin/sh -c /bin/bash" About a minute ago Up About a minute docker02 cc5648abda23 volume1/centos "/bin/sh -c /bin/bash" 3 minutes ago Up 3 minutes docker01








其实这个时候我们去看三个容器的挂载情况,会发现他们的挂载是一样的,都是对应的宿主机的相同文件夹:

[root@localhost ~]# docker inspect d3029b48260e "Mounts": [ { "Type": "volume", "Name": "3d78455dc61c42b02ed985c140fa5df799b722db481535c33a7af20c9ef777f4", "Source": "/var/lib/docker/volumes/3d78455dc61c42b02ed985c140fa5df799b722db481535c33a7af20c9ef777f4/_data", "Destination": "/dataVolume1", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "7d63ce25e13f6d382ac748acd1fcdeab7af505dc98ce95e54e4ab4e87a4c83d9", "Source": "/var/lib/docker/volumes/7d63ce25e13f6d382ac748acd1fcdeab7af505dc98ce95e54e4ab4e87a4c83d9/_data", "Destination": "/dataVolume2", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], [root@localhost ~]# docker inspect 70c8a7164cda Mounts "Mounts": [ { "Type": "volume", "Name": "3d78455dc61c42b02ed985c140fa5df799b722db481535c33a7af20c9ef777f4", "Source": "/var/lib/docker/volumes/3d78455dc61c42b02ed985c140fa5df799b722db481535c33a7af20c9ef777f4/_data", "Destination": "/dataVolume1", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "7d63ce25e13f6d382ac748acd1fcdeab7af505dc98ce95e54e4ab4e87a4c83d9", "Source": "/var/lib/docker/volumes/7d63ce25e13f6d382ac748acd1fcdeab7af505dc98ce95e54e4ab4e87a4c83d9/_data", "Destination": "/dataVolume2", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], [root@localhost ~]# docker inspect cc5648abda23 "Mounts": [ { "Type": "volume", "Name": "3d78455dc61c42b02ed985c140fa5df799b722db481535c33a7af20c9ef777f4", "Source": "/var/lib/docker/volumes/3d78455dc61c42b02ed985c140fa5df799b722db481535c33a7af20c9ef777f4/_data", "Destination": "/dataVolume1", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "7d63ce25e13f6d382ac748acd1fcdeab7af505dc98ce95e54e4ab4e87a4c83d9", "Source": "/var/lib/docker/volumes/7d63ce25e13f6d382ac748acd1fcdeab7af505dc98ce95e54e4ab4e87a4c83d9/_data", "Destination": "/dataVolume2", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ],

  

posted on 2021-04-01 19:00  torotoise512  阅读(1030)  评论(0编辑  收藏  举报