设为首页 收藏本站
查看: 739|回复: 0

[经验分享] Docker数据管理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-2-25 09:25:30 | 显示全部楼层 |阅读模式
Docker的数据卷是可以绕过文件系统的,而且数据卷是可以共享,可重用的
查看数据卷:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[iyunv@docker ~]# docker inspect -f {{.Volumes}} volume-test1
map[/data:/var/lib/docker/volumes/9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5/_data]
[iyunv@docker ~]# cd /var/lib/docker/
[iyunv@docker docker]# ls
containers  devicemapper  graph  init  linkgraph.db  repositories-devicemapper  tmp  trust  volumes
[iyunv@docker docker]# cd volumes/
[iyunv@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
[iyunv@docker volumes]# cd 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5/
[iyunv@docker 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5]#
[iyunv@docker ~]# docker run -it --name volume-test1 -h nginx -v /data centos
[iyunv@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
容器的宿主机:
[iyunv@docker 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5]# ls
_data
[iyunv@docker 9f36c1a9b7728974566268a2beebb0b917226952280c96107388469659bfcfe5]# cd _data/
[iyunv@docker _data]# ls
passwd
[iyunv@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
[iyunv@docker _data]# ls
passwd  test.iso
容器内:
[iyunv@nginx /]# cd /data/
[iyunv@nginx data]# ls
[iyunv@nginx data]# cp /etc/passwd .
[iyunv@nginx data]# ls
passwd
[iyunv@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
宿主机的操作:
[iyunv@docker ~]# mkdir -p /volume-test2-data
[iyunv@docker ~]# docker run -it -d --name volume-test2 -h centos7 --restart=always -v /volume-test2-data:/centos-data centos
29e9a41e563d514c8b9f14d14a0426af955651b28109c4851cfec40a186244d0
[iyunv@docker ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
29e9a41e563d        centos              "/bin/bash"         7 seconds ago       Up 5 seconds                            volume-test2        
[iyunv@docker ~]# cp -r /usr/share/doc/abrt-2.0.8/ /volume-test2-data/
[iyunv@docker ~]# ls -l /volume-test2-data/
total 4
drwxr-xr-x 2 root root 4096 Feb 24 09:58 abrt-2.0.8
容器内的操作:
[iyunv@docker ~]# ./in.sh 29e9a41e563d
[iyunv@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
[iyunv@centos7 /]# cd /centos-data/
[iyunv@centos7 centos-data]# ls
abrt-2.0.8
[iyunv@centos7 centos-data]# cp /etc/passwd .
[iyunv@centos7 centos-data]# ls
abrt-2.0.8  passwd
[iyunv@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
宿主机的操作:
先删除原有的容器
[iyunv@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     
删除正在允许的容器时,会有报错信息:
   
[iyunv@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]
[iyunv@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      
先停止容器,然后再进行删除:  
[iyunv@docker ~]# docker stop 29e9a41e563d
29e9a41e563d
[iyunv@docker ~]# docker rm 29e9a41e563d
29e9a41e563d
[iyunv@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目录对容器是只读的
[iyunv@docker ~]# docker run -it -d --name volume-test2 -h centos7 --restart=always -v /volume-test2-data:/centos-data -v /opt:/opt:ro centos
37652d4601948fe8421c0371278e9bee8c5cbb35f16c7bf755764d56ddafa304
[iyunv@docker ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
37652d460194        centos              "/bin/bash"         16 seconds ago      Up 15 seconds                           volume-test2        
[iyunv@docker ~]# ./in.sh 37652d460194
容器内的操作:
[iyunv@centos7 /]# cd /opt/
[iyunv@centos7 opt]# ls
rh
[iyunv@centos7 opt]# ls
opt.iso  rh
[iyunv@centos7 opt]# touch 123.txt
touch: cannot touch ‘123.txt’: Read-only file system
--volume-from:
继承容器数据卷的操作:
[iyunv@docker ~]# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
[iyunv@docker ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
94ea720a2c41        centos              "/bin/bash"         6 seconds ago       Up 4 seconds                            volume-test04      
[iyunv@docker ~]# ./in.sh 94ea720a2c41
[iyunv@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
[iyunv@94ea720a2c41 /]# cd /opt/
[iyunv@94ea720a2c41 opt]# ls
opt.iso  rh
[iyunv@94ea720a2c41 opt]# cd /centos-data/
[iyunv@94ea720a2c41 centos-data]# ls
abrt-2.0.8  passwd
[iyunv@94ea720a2c41 centos-data]# touch 12345.txt
[iyunv@94ea720a2c41 centos-data]# cd /opt
[iyunv@94ea720a2c41 opt]# ls
opt.iso  rh
[iyunv@94ea720a2c41 opt]# touch 111
touch: cannot touch ‘111’: Read-only file system
拥有volume-test2的容器的操作:
[iyunv@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        
[iyunv@docker ~]# ./in.sh 37652d460194
[iyunv@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
[iyunv@centos7 /]# cd /centos-data/
[iyunv@centos7 centos-data]# ls
abrt-2.0.8  passwd
[iyunv@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
[iyunv@docker ~]# docker run -it -d --restart=always --name volume-test04 --volumes-from volume-test2 centos
94ea720a2c418286d26068d126df35523742e2f910685863f876c3ba9c09638c
宿主机上的操作:
[iyunv@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   
[iyunv@docker ~]# docker stop 37652d460194
37652d460194     
[iyunv@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        
[iyunv@docker _data]# cd /volume-test2-data
[iyunv@docker volume-test2-data]# ls
12345.txt  abrt-2.0.8  passwd
[iyunv@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
容器内的操作:
[iyunv@94ea720a2c41 opt]# cd /centos-data/
[iyunv@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
[iyunv@94ea720a2c41 centos-data]#



说明:

1、验证后可以发现,ro权限是正常的
2、这种映射在Dockerfile里面是没法使用的
3、这里我们不管volume-test2 所属的容器是否正在允许,在新主机中均能够继承


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-182519-1-1.html 上篇帖子: 手动构建Docker镜像 下篇帖子: Docker基础
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表