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

Docker基础学习之数据管理

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

前言

docker容器中管理数据主要有两种方式,数据卷(Data Volumes)和数据卷容器(Data Volume Containers),下面我们详细介绍Docker中的数据管理,有需要的一起来学习学习吧。

数据卷

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

      数据卷可以在容器之间共享和重用;

      对数据卷的修改会立马有效;

      对数据卷的更新,不会影响镜像;

      卷会一直存在,直到没有容器使用。

数据卷的使用,类似于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

挂载数据卷

[root@localhost ~]# docker run -itd -v /data/:/data1 centos bashe136b27a8e177d878e76c60aafade32df947a60f77b3f95dcaf0680b7ffbc6e8[root@localhost ~]# docker psCONTAINER ID  IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMESe136b27a8e17  centos    "bash"    14 seconds ago  Up 13 seconds       tender_euclid
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表