docker深入1-docker的数据卷
<code>参考:</code>
<code>http:</code><code>//docs</code><code>.docker.com</code><code>/userguide/dockervolumes/</code>
<code>针对数据的存储,有个data volume的概念。</code>
<code>使用参数: -</code><code>v</code>
<code>在container中创建一个volume,或者类似目录映射的方式,挂载一个数据盘或者目录到docker的container中。</code>
<code>环境准备:</code>
<code>[root@svr200-10 ~]</code><code># yum install docker-io -y</code>
<code>[root@svr200-10 ~]</code><code># docker -v</code>
<code>Docker version 1.5.0, build a8a31ef</code><code>/1</code><code>.5.0</code>
<code>[root@svr200-10 ~]</code><code># service docker start</code>
<code>[root@svr200-10 ~]</code><code># useradd Jack && usermod -a -G docker Jack</code>
<code>[root@svr200-10 ~]</code><code># su Jack</code>
<code>[Jack@svr200-10 bin]$ docker pull centos</code>
<code>[Jack@svr200-10 bin]$ docker images</code>
<code>REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE</code>
<code>centos 7 fd44297e2ddb 11 days ago 215.7 MB</code>
<code>centos centos7 fd44297e2ddb 11 days ago 215.7 MB</code>
<code>centos latest fd44297e2ddb 11 days ago 215.7 MB</code>
<code>一、简单的方式是:挂载一个数据目录到container中</code>
<code>[Jack@svr200-10 bin]$ docker run -d -it -</code><code>v</code> <code>/home/datacenter</code><code>:</code><code>/datacenter</code> <code>--name datacenter centos </code>
<code>66f5e0e0e7042e371c092ff24117598055b7f65d4224f9738efbf13ba6273127</code>
<code>[Jack@svr200-10 bin]$ docker </code><code>ps</code> <code>-a</code>
<code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</code>
<code>66f5e0e0e704 centos:7 </code><code>"/bin/bash"</code> <code>12 seconds ago Up 11 seconds datacenter </code>
<code>[Jack@svr200-10 bin]$ docker attach test01</code>
<code>[root@66f5e0e0e704 /]</code><code># ll /</code>
<code>total 60</code>
<code>lrwxrwxrwx. 1 root root 7 Apr 15 14:28 bin -> usr</code><code>/bin</code>
<code>drwxr-xr-x. 2 root root 4096 May 4 02:55 datacenter</code>
<code>drwxr-xr-x. 5 root root 380 May 4 02:51 dev</code>
<code>drwxr-xr-x. 47 root root 4096 May 4 02:51 etc</code>
<code>drwxr-xr-x. 2 root root 4096 Jun 10 2014 home</code>
<code>lrwxrwxrwx. 1 root root 7 Apr 15 14:28 lib -> usr</code><code>/lib</code>
<code>lrwxrwxrwx. 1 root root 9 Apr 15 14:28 lib64 -> usr</code><code>/lib64</code>
<code>drwx------. 2 root root 4096 Apr 15 14:26 lost+found</code>
<code>drwxr-xr-x. 2 root root 4096 Jun 10 2014 media</code>
<code>drwxr-xr-x. 2 root root 4096 Jun 10 2014 mnt</code>
<code>drwxr-xr-x. 2 root root 4096 Jun 10 2014 opt</code>
<code>dr-xr-xr-x. 235 root root 0 May 4 02:51 proc</code>
<code>dr-xr-x---. 2 root root 4096 Apr 15 14:29 root</code>
<code>drwxr-xr-x. 10 root root 4096 Apr 15 14:29 run</code>
<code>lrwxrwxrwx. 1 root root 8 Apr 15 14:28 sbin -> usr</code><code>/sbin</code>
<code>drwxr-xr-x. 2 root root 4096 Jun 10 2014 srv</code>
<code>dr-xr-xr-x. 13 root root 0 May 4 02:51 sys</code>
<code>-rw-r--r--. 1 root root 11 May 4 02:52 test01</code>
<code>-rw-r--r--. 1 root root 11 May 4 02:52 test04</code>
<code>drwxrwxrwt. 7 root root 4096 May 4 02:51 tmp</code>
<code>drwxr-xr-x. 13 root root 4096 Apr 15 14:28 usr</code>
<code>drwxr-xr-x. 19 root root 4096 Apr 15 14:29 var</code>
<code>[root@66f5e0e0e704 /]</code><code># df -h</code>
<code>Filesystem Size Used Avail Use% Mounted on</code>
<code>/dev/mapper/docker-252</code><code>:0-262241-66f5e0e0e7042e371c092ff24117598055b7f65d4224f9738efbf13ba6273127 9.8G 254M 9.0G 3% /</code>
<code>tmpfs 1.9G 0 1.9G 0% </code><code>/dev</code>
<code>shm 64M 0 64M 0% </code><code>/dev/shm</code>
<code>/dev/mapper/vg_svr20010-lv_root</code> <code>50G 9.3G 38G 20% </code><code>/etc/hosts</code>
<code>/dev/mapper/vg_svr20010-lv_home</code> <code>405G 48G 338G 13% </code><code>/datacenter</code>
<code>tmpfs 1.9G 0 1.9G 0% </code><code>/proc/kcore</code>
<code>写入数据:</code>
<code>[root@66f5e0e0e704 /]</code><code># echo "`date` aaa" >/datacenter/test01</code>
<code>[root@66f5e0e0e704 /]</code><code># echo "`date` 123" >/datacenter/test02 </code>
<code>[root@66f5e0e0e704 /]</code><code># ls /datacenter/</code>
<code>test01 test02</code>
<code>[root@66f5e0e0e704 /]</code><code># cat /datacenter/test0*</code>
<code>Mon May 4 02:57:19 UTC 2015 aaa</code>
<code>Mon May 4 02:57:27 UTC 2015 123</code>
<code>[root@66f5e0e0e704 /]</code><code># exit </code>
<code>exit</code>
<code>查看宿主机挂载目录的文件和内容:</code>
<code>[Jack@svr200-10 bin]$ ll </code><code>/home/datacenter</code>
<code>total 8</code>
<code>-rw-r--r--. 1 root root 33 May 4 10:57 test01</code>
<code>-rw-r--r--. 1 root root 33 May 4 10:57 test02</code>
<code>[Jack@svr200-10 bin]$ </code><code>cat</code> <code>/home/datacenter/test0</code><code>*</code>
<code>[Jack@svr200-10 bin]$ docker stop datacenter</code>
<code>datacenter</code>
<code>[Jack@svr200-10 bin]$ docker </code><code>rm</code> <code>datacenter </code>
<code>二、复杂一点儿:创建一个data volume container,共享给其他container</code>
<code>如官网所示:</code>
<code>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 </code><code>then</code> <code>to </code><code>mount</code> <code>the data from it.</code>
<code>1)创建一个container,提供一个数据卷供其他container使用:</code>
<code>[Jack@svr200-10 bin]$ docker run -d -it -</code><code>v</code> <code>/home/datacenter</code><code>:</code><code>/datacenter</code> <code>--name Data_Vol centos </code>
<code>7691eccc73f6e4e2e2c3d6816cf6ba6a80a5f98f5067d48db6e8bafb4e4db021</code>
<code>7691eccc73f6 centos:7 </code><code>"/bin/bash"</code> <code>7 seconds ago Up 5 seconds Data_Vol </code>
<code>再创建2个container,使用刚才创建的数据卷Data_Vol来存储数据。</code>
<code>在启动container时,用这个参数:--volumes-from Data_Vol,而不是之前提到的-</code><code>v</code><code>参数,来挂载数据卷。</code>
<code>2)创建一个container:app1 ,写入一点儿数据 </code>
<code>[Jack@svr200-10 bin]$ docker run -d -it --volumes-from Data_Vol --name app1 centos</code>
<code>1474f622b7f04c98da98e320d10864538b50b0b053677bbda039c8fb657062c1</code>
<code>[Jack@svr200-10 bin]$ docker attach app1</code>
<code>[root@1474f622b7f0 /]</code><code># ll /</code>
<code>total 52</code>
<code>drwxr-xr-x. 2 root root 4096 May 4 02:56 datacenter</code>
<code>drwxr-xr-x. 5 root root 380 May 4 03:40 dev</code>
<code>drwxr-xr-x. 47 root root 4096 May 4 03:40 etc</code>
<code>dr-xr-xr-x. 244 root root 0 May 4 03:40 proc</code>
<code>dr-xr-xr-x. 13 root root 0 May 4 03:40 sys</code>
<code>drwxrwxrwt. 7 root root 4096 May 4 03:40 tmp</code>
<code>[root@1474f622b7f0 /]</code><code># ls /datacenter/</code>
<code>[root@1474f622b7f0 /]</code><code># touch /datacenter/app1</code>
<code>[root@1474f622b7f0 /]</code><code># ls /datacenter/ </code>
<code>app1 test01 test02</code>
<code>[root@1474f622b7f0 /]</code><code># echo "`date` hello app1" >/datacenter/app1 </code>
<code>[root@1474f622b7f0 /]</code><code># cat /datacenter/app1 </code>
<code>Mon May 4 03:43:11 UTC 2015 hello app1</code>
<code>[root@1474f622b7f0 /]</code><code># exit</code>
<code>2)再创建一个container:app2 ,写入一点儿数据 </code>
<code>[Jack@svr200-10 bin]$ docker run -d -it --volumes-from Data_Vol --name app2 centos</code>
<code>c4d743681ec95d78b21a52d3a558b4cab40a9f7ba43e884e4686c57c313b6923</code>
<code>[Jack@svr200-10 bin]$ docker attach app2</code>
<code>[root@c4d743681ec9 /]</code><code># df -h</code>
<code>/dev/mapper/docker-252</code><code>:0-262241-c4d743681ec95d78b21a52d3a558b4cab40a9f7ba43e884e4686c57c313b6923 9.8G 254M 9.0G 3% /</code>
<code>[root@c4d743681ec9 /]</code><code># ls /datacenter/ </code>
<code>[root@c4d743681ec9 /]</code><code># cat /datacenter/app1 </code>
<code>[root@c4d743681ec9 /]</code><code># echo "`date` this is app2" >/datacenter/app2 </code>
<code>[root@c4d743681ec9 /]</code><code># exit</code>
<code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</code>
<code>c4d743681ec9 centos:7 </code><code>"/bin/bash"</code> <code>6 minutes ago Exited (0) 6 seconds ago app2 </code>
<code>1474f622b7f0 centos:7 </code><code>"/bin/bash"</code> <code>6 minutes ago Exited (130) About a minute ago app1 </code>
<code>7691eccc73f6 centos:7 </code><code>"/bin/bash"</code> <code>7 minutes ago Up 7 minutes Data_Vol </code>
<code>[Jack@svr200-10 bin]$ </code>
<code>3)我们停止Data_Vol这个容器,再试试写入数据</code>
<code>[Jack@svr200-10 bin]$ docker stop Data_Vol</code>
<code>Data_Vol</code>
<code>[Jack@svr200-10 bin]$ docker start app1</code>
<code>app1</code>
<code>[Jack@svr200-10 bin]$ docker </code><code>ps</code> <code>-a </code>
<code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES</code>
<code>c4d743681ec9 centos:7 </code><code>"/bin/bash"</code> <code>7 minutes ago Exited (0) About a minute ago app2 </code>
<code>1474f622b7f0 centos:7 </code><code>"/bin/bash"</code> <code>8 minutes ago Up 7 seconds app1 </code>
<code>7691eccc73f6 centos:7 </code><code>"/bin/bash"</code> <code>8 minutes ago Exited (137) 16 seconds ago Data_Vol </code>
<code>app1 app2 test01 test02</code>
<code>[root@1474f622b7f0 /]</code><code># echo "`date` app1 is back here" >>/datacenter/app1 </code>
<code>[root@1474f622b7f0 /]</code><code># cat /datacenter/app1</code>
<code>Mon May 4 03:48:53 UTC 2015 app1 is back here</code>
<code>看起来,,这个用来持久化的Data_Vol不用启动,,其他的container用--volumes-from Data_Vol来挂载数据卷,也是可以正常使用的。</code>
<code>4)回到宿主机了,我们看下数据</code>
<code>[Jack@svr200-10 bin]$ </code><code>ls</code> <code>/home/datacenter/</code>
<code>看起来,文件都在这里呢,再看下数据:</code>
<code>[Jack@svr200-10 bin]$ </code><code>cat</code> <code>/home/datacenter/app1</code>
<code>[Jack@svr200-10 bin]$ </code><code>cat</code> <code>/home/datacenter/app2</code>
<code>Mon May 4 03:46:37 UTC 2015 this is app2</code>
<code>看,符合预期。</code>
本文转自 pcnk 51CTO博客,原文链接:http://blog.51cto.com/nosmoking/1641684,如需转载请自行联系原作者