Docker的数据卷是可以绕过文件系统的,而且数据卷是可以共享,可重用的
查看数据卷:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@docker ~]
# docker inspect -f ``.`Volumes` volume-test1
map[
/data
:
/var/lib/docker/volumes/9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5/_data
]
[root@docker ~]
# cd /var/lib/docker/
[root@docker docker]
# ls
containers devicemapper graph init linkgraph.db repositories-devicemapper tmp trust volumes
[root@docker docker]
# cd volumes/
[root@docker volumes]
# ls -lrt
total 8
drwxr-xr-x 3 root root 4096 Feb 24 09:29 63ccf5d524567c835c6d542cfc74d1a28d28fdf48a0bca0c517cb8dedf46b632
drwxr-xr-x 3 root root 4096 Feb 24 09:30 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5
[root@docker volumes]
# cd 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5/
[root@docker 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5]
#
[root@docker ~]
# docker run -it --name volume-test1 -h nginx -v /data centos
[root@nginx /]
# ls /data
|
测试:
1、系统随机生成的目录,进行挂载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
容器的宿主机:
[root@docker 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5]
# ls
_data
[root@docker 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5]
# cd _data/
[root@docker _data]
# ls
passwd
[root@docker _data]
# dd if=/dev/zero of=test.iso bs=1M count=10
10+0 records
in
10+0 records out
10485760 bytes (10 MB) copied, 0.00956288 s, 1.1 GB
/s
[root@docker _data]
# ls
passwd
test
.iso
容器内:
[root@nginx /]
# cd /data/
[root@nginx data]
# ls
[root@nginx data]
# cp /etc/passwd .
[root@nginx data]
# ls
passwd
[root@nginx data]
# ls
passwd
test
.iso
说明:容器的宿主机的目录已共享给容器使用(删除容器后,原有的数据卷目录中的文件仍旧是存在的)
|
2、系统指定的目录,进行挂载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
宿主机的操作:
[root@docker ~]
# mkdir -p /volume-test2-data
[root@docker ~]
# docker run -it -d --name volume-test2 -h centos7 --restart=always -v /volume-test2-data:/centos-data centos
29e9a41e563d514c8b9f14d14a0426af955651b28109c4851cfec40a186244d0
[root@docker ~]
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29e9a41e563d centos
"/bin/bash"
7 seconds ago Up 5 seconds volume-test2
[root@docker ~]
# cp -r /usr/share/doc/abrt-2.0.8/ /volume-test2-data/
[root@docker ~]
# ls -l /volume-test2-data/
total 4
drwxr-xr-x 2 root root 4096 Feb 24 09:58 abrt-2.0.8
容器内的操作:
[root@docker ~]
# ./in.sh 29e9a41e563d
[root@centos7 /]
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/docker-8
:3-8388750-29e9a41e563d514c8b9f14d14a0426af955651b28109c4851cfec40a186244d0 9.8G 230M 9.0G 3% /
tmpfs 1.9G 0 1.9G 0%
/dev
shm 64M 0 64M 0%
/dev/shm
/dev/sda3
193G 11G 173G 6%
/etc/hosts
[root@centos7 /]
# cd /centos-data/
[root@centos7 centos-data]
# ls
abrt-2.0.8
[root@centos7 centos-data]
# cp /etc/passwd .
[root@centos7 centos-data]
# ls
abrt-2.0.8
passwd
[root@docker _data]
# ls /volume-test2-data/
abrt-2.0.8
passwd
|
3、系统指定的目录,进行挂载(可挂载多个,并设置权限,默认为rw权限)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
宿主机的操作:
先删除原有的容器
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29e9a41e563d centos
"/bin/bash"
11 minutes ago Up 11 minutes volume-test2
8884fc392971 nginx "nginx -g 'daemon of 13 hours ago Up 51 minutes 80
/tcp
, 443
/tcp
nginxdocker
14caa9ab03a9 dockerui
/dockerui
"/dockerui"
46 hours ago Up 51 minutes 0.0.0.0:9000->9000
/tcp
dockerui_qinwen
e278cabec91e centos
"/bin/bash"
47 hours ago Up 51 minutes centos_aways
删除正在允许的容器时,会有报错信息:
[root@docker ~]
# docker rm 29e9a41e563d -f
Error response from daemon: Cannot destroy container 29e9a41e563d: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -f
Error response from daemon: no such
id
: -f
Error: failed to remove containers: [29e9a41e563d -f]
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29e9a41e563d centos
"/bin/bash"
11 minutes ago Up 11 minutes volume-test2
8884fc392971 nginx "nginx -g 'daemon of 13 hours ago Up 52 minutes 80
/tcp
, 443
/tcp
nginxdocker
14caa9ab03a9 dockerui
/dockerui
"/dockerui"
46 hours ago Up 52 minutes 0.0.0.0:9000->9000
/tcp
dockerui_qinwen
e278cabec91e centos
"/bin/bash"
47 hours ago Up 52 minutes centos_aways
先停止容器,然后再进行删除:
[root@docker ~]
# docker stop 29e9a41e563d
29e9a41e563d
[root@docker ~]
# docker rm 29e9a41e563d
29e9a41e563d
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8884fc392971 nginx "nginx -g 'daemon of 13 hours ago Up 52 minutes 80
/tcp
, 443
/tcp
nginxdocker
14caa9ab03a9 dockerui
/dockerui
"/dockerui"
46 hours ago Up 52 minutes 0.0.0.0:9000->9000
/tcp
dockerui_qinwen
e278cabec91e centos
"/bin/bash"
47 hours ago Up 52 minutes centos_aways
运行一个基于centos镜像的容器,容器名称为volume-test2,并设置容器中的主机名为centos7(仅对容器有效),挂载本地的
/volume-test2-data
以及
/opt
目录,并且指定
/opt
目录对容器是只读的
[root@docker ~]
# docker run -it -d --name volume-test2 -h centos7 --restart=always -v /volume-test2-data:/centos-data -v /opt:/opt:ro centos
37652d4601948fe8421c0371278e9bee8c5cbb35f16c7bf755764d56ddafa304
[root@docker ~]
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37652d460194 centos
"/bin/bash"
16 seconds ago Up 15 seconds volume-test2
[root@docker ~]
# ./in.sh 37652d460194
容器内的操作:
[root@centos7 /]
# cd /opt/
[root@centos7 opt]
# ls
rh
[root@centos7 opt]
# ls
opt.iso rh
[root@centos7 opt]
# touch 123.txt
touch
: cannot
touch
‘123.txt’: Read-only
file
system
--volume-from:
继承容器数据卷的操作:
[root@docker ~]
# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
[root@docker ~]
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos
"/bin/bash"
6 seconds ago Up 4 seconds volume-test04
[root@docker ~]
# ./in.sh 94ea720a2c41
[root@94ea720a2c41 /]
# ls /
anaconda-post.log centos-data etc lib lost+found mnt proc run srv tmp var
bin dev home lib64 media opt root sbin sys usr
[root@94ea720a2c41 /]
# cd /opt/
[root@94ea720a2c41 opt]
# ls
opt.iso rh
[root@94ea720a2c41 opt]
# cd /centos-data/
[root@94ea720a2c41 centos-data]
# ls
abrt-2.0.8
passwd
[root@94ea720a2c41 centos-data]
# touch 12345.txt
[root@94ea720a2c41 centos-data]
# cd /opt
[root@94ea720a2c41 opt]
# ls
opt.iso rh
[root@94ea720a2c41 opt]
# touch 111
touch
: cannot
touch
‘111’: Read-only
file
system
拥有volume-test2的容器的操作:
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos
"/bin/bash"
45 seconds ago Up 43 seconds volume-test04
37652d460194 centos
"/bin/bash"
About an hour ago Up About an hour volume-test2
8884fc392971 nginx "nginx -g 'daemon of 14 hours ago Up About an hour 80
/tcp
, 443
/tcp
nginxdocker
14caa9ab03a9 dockerui
/dockerui
"/dockerui"
47 hours ago Up About an hour 0.0.0.0:9000->9000
/tcp
dockerui_qinwen
e278cabec91e centos
"/bin/bash"
2 days ago Up About an hour centos_aways
[root@docker ~]
# ./in.sh 37652d460194
[root@centos7 /]
# ls
anaconda-post.log centos-data etc lib lost+found mnt proc run srv tmp var
bin dev home lib64 media opt root sbin sys usr
[root@centos7 /]
# cd /centos-data/
[root@centos7 centos-data]
# ls
abrt-2.0.8
passwd
[root@centos7 centos-data]
# ls
12345.txt abrt-2.0.8
passwd
|
进一步进行测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
[root@docker ~]
# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
宿主机上的操作:
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos
"/bin/bash"
14 minutes ago Up 14 minutes volume-test04
37652d460194 centos
"/bin/bash"
About an hour ago Up About an hour volume-test2
8884fc392971 nginx "nginx -g 'daemon of 14 hours ago Up 2 hours 80
/tcp
, 443
/tcp
nginxdocker
14caa9ab03a9 dockerui
/dockerui
"/dockerui"
47 hours ago Up 2 hours 0.0.0.0:9000->9000
/tcp
dockerui_qinwen
e278cabec91e centos
"/bin/bash"
2 days ago Up 2 hours
[root@docker ~]
# docker stop 37652d460194
37652d460194
[root@docker ~]
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos
"/bin/bash"
14 minutes ago Up 14 minutes volume-test04
8884fc392971 nginx "nginx -g 'daemon of 14 hours ago Up 2 hours 80
/tcp
, 443
/tcp
nginxdocker
14caa9ab03a9 dockerui
/dockerui
"/dockerui"
47 hours ago Up 2 hours 0.0.0.0:9000->9000
/tcp
dockerui_qinwen
e278cabec91e centos
"/bin/bash"
2 days ago Up 2 hours centos_aways
[root@docker _data]
# cd /volume-test2-data
[root@docker volume-test2-data]
# ls
12345.txt abrt-2.0.8
passwd
[root@docker volume-test2-data]
# ls -lrt
total 8
drwxr-xr-x 2 root root 4096 Feb 24 09:58 abrt-2.0.8
-rw-r--r-- 1 root root 692 Feb 24 10:01
passwd
-rw-r--r-- 1 root root 0 Feb 24 11:16 12345.txt
容器内的操作:
[root@94ea720a2c41 opt]
# cd /centos-data/
[root@94ea720a2c41 centos-data]
# ls -lrt
total 8
drwxr-xr-x 2 root root 4096 Feb 24 01:58 abrt-2.0.8
-rw-r--r-- 1 root root 692 Feb 24 02:01
passwd
-rw-r--r-- 1 root root 0 Feb 24 03:16 12345.txt
[root@94ea720a2c41 centos-data]
#
|
说明:
1、验证后可以发现,ro权限是正常的
2、这种映射在Dockerfile里面是没法使用的
3、这里我们不管volume-test2 所属的容器是否正在允许,在新主机中均能够继承
本文转自 冰冻vs西瓜 51CTO博客,原文链接:http://blog.51cto.com/molewan/1744570,如需转载请自行联系原作者