edfwe 发表于 2015-5-5 08:35:56

docker深入1-docker的数据卷

docker深入1-docker的数据卷

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
参考:
http://docs.docker.com/userguide/dockervolumes/


针对数据的存储,有个data volume的概念。
使用参数: -v
在container中创建一个volume,或者类似目录映射的方式,挂载一个数据盘或者目录到docker的container中。
环境准备:
# yum install docker-io -y
# docker -v
Docker version 1.5.0, build a8a31ef/1.5.0
# service docker start
# useradd Jack && usermod -a -G docker Jack
# su Jack
$ docker pull centos
$ docker images
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
centos            7                   fd44297e2ddb      11 days ago         215.7 MB
centos            centos7             fd44297e2ddb      11 days ago         215.7 MB
centos            latest            fd44297e2ddb      11 days ago         215.7 MB


一、简单的方式是:挂载一个数据目录到container中
$ docker run -d -it -v /home/datacenter:/datacenter --name datacenter centos               
66f5e0e0e7042e371c092ff24117598055b7f65d4224f9738efbf13ba6273127
$ docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
66f5e0e0e704      centos:7            "/bin/bash"         12 seconds ago      Up 11 seconds                           datacenter            
$ docker attach test01

# ll /
total 60
lrwxrwxrwx.   1 root root    7 Apr 15 14:28 bin -> usr/bin
drwxr-xr-x.   2 root root 4096 May4 02:55 datacenter
drwxr-xr-x.   5 root root380 May4 02:51 dev
drwxr-xr-x.47 root root 4096 May4 02:51 etc
drwxr-xr-x.   2 root root 4096 Jun 102014 home
lrwxrwxrwx.   1 root root    7 Apr 15 14:28 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Apr 15 14:28 lib64 -> usr/lib64
drwx------.   2 root root 4096 Apr 15 14:26 lost+found
drwxr-xr-x.   2 root root 4096 Jun 102014 media
drwxr-xr-x.   2 root root 4096 Jun 102014 mnt
drwxr-xr-x.   2 root root 4096 Jun 102014 opt
dr-xr-xr-x. 235 root root    0 May4 02:51 proc
dr-xr-x---.   2 root root 4096 Apr 15 14:29 root
drwxr-xr-x.10 root root 4096 Apr 15 14:29 run
lrwxrwxrwx.   1 root root    8 Apr 15 14:28 sbin -> usr/sbin
drwxr-xr-x.   2 root root 4096 Jun 102014 srv
dr-xr-xr-x.13 root root    0 May4 02:51 sys
-rw-r--r--.   1 root root   11 May4 02:52 test01
-rw-r--r--.   1 root root   11 May4 02:52 test04
drwxrwxrwt.   7 root root 4096 May4 02:51 tmp
drwxr-xr-x.13 root root 4096 Apr 15 14:28 usr
drwxr-xr-x.19 root root 4096 Apr 15 14:29 var
# df -h
Filesystem                                                                                        SizeUsed Avail Use% Mounted on
/dev/mapper/docker-252:0-262241-66f5e0e0e7042e371c092ff24117598055b7f65d4224f9738efbf13ba62731279.8G254M9.0G   3% /
tmpfs                                                                                             1.9G   01.9G   0% /dev
shm                                                                                                64M   0   64M   0% /dev/shm
/dev/mapper/vg_svr20010-lv_root                                                                  50G9.3G   38G20% /etc/hosts
/dev/mapper/vg_svr20010-lv_home                                                                   405G   48G338G13% /datacenter
tmpfs                                                                                             1.9G   01.9G   0% /proc/kcore

写入数据:
# echo "`date` aaa" >/datacenter/test01
# echo "`date` 123" >/datacenter/test02   
# ls /datacenter/
test01test02
# cat /datacenter/test0*
Mon May4 02:57:19 UTC 2015 aaa
Mon May4 02:57:27 UTC 2015 123

# exit
exit

查看宿主机挂载目录的文件和内容:
$ ll /home/datacenter
total 8
-rw-r--r--. 1 root root 33 May4 10:57 test01
-rw-r--r--. 1 root root 33 May4 10:57 test02
$ cat /home/datacenter/test0*
Mon May4 02:57:19 UTC 2015 aaa
Mon May4 02:57:27 UTC 2015 123


$ docker stop datacenter
datacenter
$ docker rm datacenter
datacenter


二、复杂一点儿:创建一个data volume container,共享给其他container
如官网所示:
If you have some persistent data that you want to share between containers, or want to use from non-persistent containers, it's best to create a named Data Volume Container, and then to mount the data from it.


1)创建一个container,提供一个数据卷供其他container使用:
$ docker run -d -it -v /home/datacenter:/datacenter --name Data_Vol centos
7691eccc73f6e4e2e2c3d6816cf6ba6a80a5f98f5067d48db6e8bafb4e4db021

$ docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS            PORTS               NAMES
7691eccc73f6      centos:7            "/bin/bash"         7 seconds ago       Up 5 seconds                            Data_Vol   


再创建2个container,使用刚才创建的数据卷Data_Vol来存储数据。
在启动container时,用这个参数:--volumes-from Data_Vol,而不是之前提到的-v参数,来挂载数据卷。

2)创建一个container:app1   ,写入一点儿数据
$ docker run -d -it --volumes-from Data_Vol --name app1 centos
1474f622b7f04c98da98e320d10864538b50b0b053677bbda039c8fb657062c1
$ docker attach app1

# ll /
total 52
lrwxrwxrwx.   1 root root    7 Apr 15 14:28 bin -> usr/bin
drwxr-xr-x.   2 root root 4096 May4 02:56 datacenter
drwxr-xr-x.   5 root root380 May4 03:40 dev
drwxr-xr-x.47 root root 4096 May4 03:40 etc
drwxr-xr-x.   2 root root 4096 Jun 102014 home
lrwxrwxrwx.   1 root root    7 Apr 15 14:28 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Apr 15 14:28 lib64 -> usr/lib64
drwx------.   2 root root 4096 Apr 15 14:26 lost+found
drwxr-xr-x.   2 root root 4096 Jun 102014 media
drwxr-xr-x.   2 root root 4096 Jun 102014 mnt
drwxr-xr-x.   2 root root 4096 Jun 102014 opt
dr-xr-xr-x. 244 root root    0 May4 03:40 proc
dr-xr-x---.   2 root root 4096 Apr 15 14:29 root
drwxr-xr-x.10 root root 4096 Apr 15 14:29 run
lrwxrwxrwx.   1 root root    8 Apr 15 14:28 sbin -> usr/sbin
drwxr-xr-x.   2 root root 4096 Jun 102014 srv
dr-xr-xr-x.13 root root    0 May4 03:40 sys
drwxrwxrwt.   7 root root 4096 May4 03:40 tmp
drwxr-xr-x.13 root root 4096 Apr 15 14:28 usr
drwxr-xr-x.19 root root 4096 Apr 15 14:29 var
# ls /datacenter/
test01test02
# touch /datacenter/app1
# ls /datacenter/      
app1test01test02
# echo "`date` hello app1" >/datacenter/app1
# cat /datacenter/app1
Mon May4 03:43:11 UTC 2015 hello app1
# exit
exit


2)再创建一个container:app2   ,写入一点儿数据
$ docker run -d -it --volumes-from Data_Vol --name app2 centos
c4d743681ec95d78b21a52d3a558b4cab40a9f7ba43e884e4686c57c313b6923

$ docker attach app2

# df -h
Filesystem                                                                                        SizeUsed Avail Use% Mounted on
/dev/mapper/docker-252:0-262241-c4d743681ec95d78b21a52d3a558b4cab40a9f7ba43e884e4686c57c313b69239.8G254M9.0G   3% /
tmpfs                                                                                             1.9G   01.9G   0% /dev
shm                                                                                                64M   0   64M   0% /dev/shm
/dev/mapper/vg_svr20010-lv_root                                                                  50G9.3G   38G20% /etc/hosts
/dev/mapper/vg_svr20010-lv_home                                                                   405G   48G338G13% /datacenter
tmpfs                                                                                             1.9G   01.9G   0% /proc/kcore
# ls /datacenter/
app1test01test02
# cat /datacenter/app1
Mon May4 03:43:11 UTC 2015 hello app1
# echo "`date` this is app2" >/datacenter/app2      
# exit
exit

$ docker ps -a
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS                            PORTS               NAMES
c4d743681ec9      centos:7            "/bin/bash"         6 minutes ago       Exited (0) 6 seconds ago                              app2               
1474f622b7f0      centos:7            "/bin/bash"         6 minutes ago       Exited (130) About a minute ago                     app1               
7691eccc73f6      centos:7            "/bin/bash"         7 minutes ago       Up 7 minutes                                          Data_Vol            
$



3)我们停止Data_Vol这个容器,再试试写入数据
$ docker stop Data_Vol

Data_Vol
$
$ docker start app1
app1
$ docker ps -a      
CONTAINER ID      IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
c4d743681ec9      centos:7            "/bin/bash"         7 minutes ago       Exited (0) About a minute ago                     app2               
1474f622b7f0      centos:7            "/bin/bash"         8 minutes ago       Up 7 seconds                                        app1               
7691eccc73f6      centos:7            "/bin/bash"         8 minutes ago       Exited (137) 16 seconds ago                         Data_Vol            
$ docker attach app1

# ls /datacenter/
app1app2test01test02
# echo "`date` app1 is back here" >>/datacenter/app1      
# cat /datacenter/app1
Mon May4 03:43:11 UTC 2015 hello app1
Mon May4 03:48:53 UTC 2015 app1 is back here
# exit
exit

看起来,,这个用来持久化的Data_Vol不用启动,,其他的container用--volumes-from Data_Vol来挂载数据卷,也是可以正常使用的。

4)回到宿主机了,我们看下数据
$ ls /home/datacenter/
app1app2test01test02

看起来,文件都在这里呢,再看下数据:
$ cat /home/datacenter/app1
Mon May4 03:43:11 UTC 2015 hello app1
Mon May4 03:48:53 UTC 2015 app1 is back here
$ cat /home/datacenter/app2
Mon May4 03:46:37 UTC 2015 this is app2

看,符合预期。



页: [1]
查看完整版本: docker深入1-docker的数据卷