参考: http://docs.docker.com/userguide/dockervolumes/ 针对数据的存储,有个data volume的概念。 使用参数: -v 在container中创建一个volume,或者类似目录映射的方式,挂载一个数据盘或者目录到docker的container中。 环境准备: [[email protected] ~]# yum install docker-io -y [[email protected] ~]# docker -v Docker version 1.5.0, build a8a31ef/1.5.0 [[email protected] ~]# service docker start [[email protected] ~]# useradd Jack && usermod -a -G docker Jack [[email protected] ~]# su Jack [[email protected] bin]$ docker pull centos [[email protected] bin]$ 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中 [[email protected] bin]$ docker run -d -it -v /home/datacenter:/datacenter --name datacenter centos 66f5e0e0e7042e371c092ff24117598055b7f65d4224f9738efbf13ba6273127 [[email protected] bin]$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 66f5e0e0e704 centos:7 "/bin/bash" 12 seconds ago Up 11 seconds datacenter [[email protected] bin]$ docker attach test01 [[email protected] /]# ll / total 60 lrwxrwxrwx. 1 root root 7 Apr 15 14:28 bin -> usr/bin drwxr-xr-x. 2 root root 4096 May 4 02:55 datacenter drwxr-xr-x. 5 root root 380 May 4 02:51 dev drwxr-xr-x. 47 root root 4096 May 4 02:51 etc drwxr-xr-x. 2 root root 4096 Jun 10 2014 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 10 2014 media drwxr-xr-x. 2 root root 4096 Jun 10 2014 mnt drwxr-xr-x. 2 root root 4096 Jun 10 2014 opt dr-xr-xr-x. 235 root root 0 May 4 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 10 2014 srv dr-xr-xr-x. 13 root root 0 May 4 02:51 sys -rw-r--r--. 1 root root 11 May 4 02:52 test01 -rw-r--r--. 1 root root 11 May 4 02:52 test04 drwxrwxrwt. 7 root root 4096 May 4 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 [[email protected] /]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/docker-252:0-262241-66f5e0e0e7042e371c092ff24117598055b7f65d4224f9738efbf13ba6273127 9.8G 254M 9.0G 3% / tmpfs 1.9G 0 1.9G 0% /dev shm 64M 0 64M 0% /dev/shm /dev/mapper/vg_svr20010-lv_root 50G 9.3G 38G 20% /etc/hosts /dev/mapper/vg_svr20010-lv_home 405G 48G 338G 13% /datacenter tmpfs 1.9G 0 1.9G 0% /proc/kcore 写入数据: [[email protected] /]# echo "`date` aaa" >/datacenter/test01 [[email protected] /]# echo "`date` 123" >/datacenter/test02 [[email protected] /]# ls /datacenter/ test01 test02 [[email protected] /]# cat /datacenter/test0* Mon May 4 02:57:19 UTC 2015 aaa Mon May 4 02:57:27 UTC 2015 123 [[email protected] /]# exit exit 查看宿主机挂载目录的文件和内容: [[email protected] bin]$ ll /home/datacenter total 8 -rw-r--r--. 1 root root 33 May 4 10:57 test01 -rw-r--r--. 1 root root 33 May 4 10:57 test02 [[email protected] bin]$ cat /home/datacenter/test0* Mon May 4 02:57:19 UTC 2015 aaa Mon May 4 02:57:27 UTC 2015 123 [[email protected] bin]$ docker stop datacenter datacenter [[email protected] bin]$ 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使用: [[email protected] bin]$ docker run -d -it -v /home/datacenter:/datacenter --name Data_Vol centos 7691eccc73f6e4e2e2c3d6816cf6ba6a80a5f98f5067d48db6e8bafb4e4db021 [[email protected] bin]$ 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 ,写入一点儿数据 [[email protected] bin]$ docker run -d -it --volumes-from Data_Vol --name app1 centos 1474f622b7f04c98da98e320d10864538b50b0b053677bbda039c8fb657062c1 [[email protected] bin]$ docker attach app1 [[email protected] /]# ll / total 52 lrwxrwxrwx. 1 root root 7 Apr 15 14:28 bin -> usr/bin drwxr-xr-x. 2 root root 4096 May 4 02:56 datacenter drwxr-xr-x. 5 root root 380 May 4 03:40 dev drwxr-xr-x. 47 root root 4096 May 4 03:40 etc drwxr-xr-x. 2 root root 4096 Jun 10 2014 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 10 2014 media drwxr-xr-x. 2 root root 4096 Jun 10 2014 mnt drwxr-xr-x. 2 root root 4096 Jun 10 2014 opt dr-xr-xr-x. 244 root root 0 May 4 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 10 2014 srv dr-xr-xr-x. 13 root root 0 May 4 03:40 sys drwxrwxrwt. 7 root root 4096 May 4 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 [[email protected] /]# ls /datacenter/ test01 test02 [[email protected] /]# touch /datacenter/app1 [[email protected] /]# ls /datacenter/ app1 test01 test02 [[email protected] /]# echo "`date` hello app1" >/datacenter/app1 [[email protected] /]# cat /datacenter/app1 Mon May 4 03:43:11 UTC 2015 hello app1 [[email protected] /]# exit exit 2)再创建一个container:app2 ,写入一点儿数据 [[email protected] bin]$ docker run -d -it --volumes-from Data_Vol --name app2 centos c4d743681ec95d78b21a52d3a558b4cab40a9f7ba43e884e4686c57c313b6923 [[email protected] bin]$ docker attach app2 [[email protected] /]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/docker-252:0-262241-c4d743681ec95d78b21a52d3a558b4cab40a9f7ba43e884e4686c57c313b6923 9.8G 254M 9.0G 3% / tmpfs 1.9G 0 1.9G 0% /dev shm 64M 0 64M 0% /dev/shm /dev/mapper/vg_svr20010-lv_root 50G 9.3G 38G 20% /etc/hosts /dev/mapper/vg_svr20010-lv_home 405G 48G 338G 13% /datacenter tmpfs 1.9G 0 1.9G 0% /proc/kcore [[email protected] /]# ls /datacenter/ app1 test01 test02 [[email protected] /]# cat /datacenter/app1 Mon May 4 03:43:11 UTC 2015 hello app1 [[email protected] /]# echo "`date` this is app2" >/datacenter/app2 [[email protected] /]# exit exit [[email protected] bin]$ 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 [[email protected] bin]$ 3)我们停止Data_Vol这个容器,再试试写入数据 [[email protected] bin]$ docker stop Data_Vol Data_Vol [[email protected] bin]$ [[email protected] bin]$ docker start app1 app1 [[email protected] bin]$ 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 [[email protected] bin]$ docker attach app1 [[email protected] /]# ls /datacenter/ app1 app2 test01 test02 [[email protected] /]# echo "`date` app1 is back here" >>/datacenter/app1 [[email protected] /]# cat /datacenter/app1 Mon May 4 03:43:11 UTC 2015 hello app1 Mon May 4 03:48:53 UTC 2015 app1 is back here [[email protected] /]# exit exit 看起来,,这个用来持久化的Data_Vol不用启动,,其他的container用--volumes-from Data_Vol来挂载数据卷,也是可以正常使用的。 4)回到宿主机了,我们看下数据 [[email protected] bin]$ ls /home/datacenter/ app1 app2 test01 test02 看起来,文件都在这里呢,再看下数据: [[email protected] bin]$ cat /home/datacenter/app1 Mon May 4 03:43:11 UTC 2015 hello app1 Mon May 4 03:48:53 UTC 2015 app1 is back here [[email protected] bin]$ cat /home/datacenter/app2 Mon May 4 03:46:37 UTC 2015 this is app2 看,符合预期。
时间: 2024-11-08 06:14:47