Docker数据管理
Docker的数据卷是可以绕过文件系统的,而且数据卷是可以共享,可重用的查看数据卷:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# docker inspect -f {{.Volumes}} volume-test1
map
# cd /var/lib/docker/
# ls
containersdevicemappergraphinitlinkgraph.dbrepositories-devicemappertmptrustvolumes
# cd 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
# cd 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5/
#
# docker run -it --name volume-test1 -h nginx -v /data centos
# ls /data
测试:
1、系统随机生成的目录,进行挂载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
容器的宿主机:
# ls
_data
# cd _data/
# ls
passwd
# 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
# ls
passwdtest.iso
容器内:
# cd /data/
# ls
# cp /etc/passwd .
# ls
passwd
# ls
passwdtest.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
宿主机的操作:
# mkdir -p /volume-test2-data
# docker run -it -d --name volume-test2 -h centos7 --restart=always -v /volume-test2-data:/centos-data centos
29e9a41e563d514c8b9f14d14a0426af955651b28109c4851cfec40a186244d0
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29e9a41e563d centos "/bin/bash" 7 seconds ago Up 5 seconds volume-test2
# cp -r /usr/share/doc/abrt-2.0.8/ /volume-test2-data/
# ls -l /volume-test2-data/
total 4
drwxr-xr-x 2 root root 4096 Feb 24 09:58 abrt-2.0.8
容器内的操作:
# ./in.sh 29e9a41e563d
# df -h
Filesystem SizeUsed Avail Use% Mounted on
/dev/mapper/docker-8:3-8388750-29e9a41e563d514c8b9f14d14a0426af955651b28109c4851cfec40a186244d09.8G230M9.0G 3% /
tmpfs 1.9G 01.9G 0% /dev
shm 64M 0 64M 0% /dev/shm
/dev/sda3 193G 11G173G 6% /etc/hosts
# cd /centos-data/
# ls
abrt-2.0.8
# cp /etc/passwd .
# ls
abrt-2.0.8passwd
# ls /volume-test2-data/
abrt-2.0.8passwd
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
宿主机的操作:
先删除原有的容器
# 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
删除正在允许的容器时,会有报错信息:
# 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:
# 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
先停止容器,然后再进行删除:
# docker stop 29e9a41e563d
29e9a41e563d
# docker rm 29e9a41e563d
29e9a41e563d
# 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目录对容器是只读的
# docker run -it -d --name volume-test2 -h centos7 --restart=always -v /volume-test2-data:/centos-data -v /opt:/opt:ro centos
37652d4601948fe8421c0371278e9bee8c5cbb35f16c7bf755764d56ddafa304
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37652d460194 centos "/bin/bash" 16 seconds ago Up 15 seconds volume-test2
# ./in.sh 37652d460194
容器内的操作:
# cd /opt/
# ls
rh
# ls
opt.isorh
# touch 123.txt
touch: cannot touch ‘123.txt’: Read-only file system
--volume-from:
继承容器数据卷的操作:
# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94ea720a2c41 centos "/bin/bash" 6 seconds ago Up 4 seconds volume-test04
# ./in.sh 94ea720a2c41
# ls /
anaconda-post.logcentos-dataetc lib lost+foundmntprocrun srvtmpvar
bin dev homelib64media optrootsbinsysusr
# cd /opt/
# ls
opt.isorh
# cd /centos-data/
# ls
abrt-2.0.8passwd
# touch 12345.txt
# cd /opt
# ls
opt.isorh
# touch 111
touch: cannot touch ‘111’: Read-only file system
拥有volume-test2的容器的操作:
# 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
# ./in.sh 37652d460194
# ls
anaconda-post.logcentos-dataetc lib lost+foundmntprocrun srvtmpvar
bin dev homelib64media optrootsbinsysusr
# cd /centos-data/
# ls
abrt-2.0.8passwd
# ls
12345.txtabrt-2.0.8passwd
进一步进行测试:
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
# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
宿主机上的操作:
# 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
# docker stop 37652d460194
37652d460194
# 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
# cd /volume-test2-data
# ls
12345.txtabrt-2.0.8passwd
# ls -lrt
total 8
drwxr-xr-x 2 root root 4096 Feb 24 09:58 abrt-2.0.8
-rw-r--r-- 1 root root692 Feb 24 10:01 passwd
-rw-r--r-- 1 root root 0 Feb 24 11:16 12345.txt
容器内的操作:
# cd /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 root692 Feb 24 02:01 passwd
-rw-r--r-- 1 root root 0 Feb 24 03:16 12345.txt
#
说明:
1、验证后可以发现,ro权限是正常的
2、这种映射在Dockerfile里面是没法使用的
3、这里我们不管volume-test2 所属的容器是否正在允许,在新主机中均能够继承
页:
[1]