首页 > 网站 > 建站经验 > 正文

Docker 数据卷及数据容器详细介绍及示例

2019-11-02 16:33:38
字体:
来源:转载
供稿:网友

Docker 数据卷及数据容器

这两天开始学习docker,发现docker确实很强大,让网站部署和维护的效率大大提高。遂准备将手头维护的几个小站,全部docker化。整理的过程中感觉到,docker可以以功能或者进程为单位进行部署和维护,不用再花时间在繁琐的配置上面,但是docker和宿主之间的数据共享以及docker间的数据共享仍然是让人头疼和操心的地方。

几个基本概念:

docker: 一种容器管理技术,这里也指既有的开发工具链。

container: 容器

image: 镜像

volum:卷 [ 译者:卷可以理解成计算机中的文件路径 ]

容器中管理数据主要有两种方式:

数据卷(Data Volumes)

数据卷容器(Data Volume Containers)

数据卷

数据卷是一个可供容器使用的特殊目录,它绕过文件系统,可以提供很多有用的特性:

数据卷可以在容器之间共享和重用; 对数据卷的修改会立马有效; 对数据卷的更新,不会影响镜像; 卷会一直存在,直到没有容器使用。

数据卷的使用,类似于Linux下对目录或文件进行mount操作。

挂载本地的目录到容器里

[root@localhost ~]# docker imagesREPOSITORY          TAG         IMAGE ID      CREATED       VIRTUAL SIZEregistry           latest       5c929a8b587a    29 hours ago    33.27 MBgenesis_centos        latest       85bc3a58f134    5 days ago     277.6 MB192.168.1.179:5000/busybox  latest       9967c5ad88de    12 days ago     1.093 MBbusybox           latest       9967c5ad88de    12 days ago     1.093 MBcentos-6-x86         latest       8fca9486a39b    13 days ago     341.3 MBcentos_with_net       latest       3e8ea8607f08    4 weeks ago     294.9 MBcentos            latest       9baab0af79c4    6 weeks ago     196.7 MB[root@localhost ~]# ls /data/ls: 无法访问/data/: 没有那个文件或目录[root@localhost ~]# mkdir /data/[root@localhost ~]# docker run -itd -v /data/:/data1 centos bash096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5
-v 用来指定挂载目录 “:”前面的/data/为本地目录 “:”后面的/data1/为容器里的目录

[root@localhost ~]# touch /data/1.txt[root@localhost ~]# echo "test" > /data/1.txt[root@localhost ~]# docker exec -it 09646 bash[root@096460f831bf /]# df -hFilesystem                                             Size Used Avail Use% Mounted on/dev/mapper/docker-253:0-1447735-096460f831bfd72b2efc6ba6b7e7bb060152afa49506ef26e0fa3cb03974f8d5 9.8G 231M 9.0G  3% /tmpfs                                               936M   0 936M  0% /devshm                                                 64M   0  64M  0% /dev/shm/dev/mapper/VolGroup-lv_root                                    35G 6.0G  28G 18% /data1[root@096460f831bf /]# ls /data1/1.txt[root@096460f831bf /]# cat /data1/1.txttest[root@096460f831bf /]# touch /data1/2.txt[root@096460f831bf /]# exitexit[root@localhost ~]# ls /data/1.txt 2.txt

不管是把容器停掉、还是删除,数据还是存在的

[root@localhost ~]# docker stop 0964609646[root@localhost ~]# ls /data/1.txt 2.txt[root@localhost ~]# docker rm 0964609646[root@localhost ~]# ls /data/1.txt 2.txt
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表