Docker数据管理

1、简介

使用Docker过程中,往往需要对数据进行持久化,或者多个容器之间进行数据共享,这就需要我们熟悉容器的数据管理操作。

2、容器中管理数据主要的两种方式

  • 数据卷(Data Volumes):容器内数据直接映射到本地主机环境。

  • 数据卷容器(Data Volumes Containers):使用特定容器维护数据。

3、为什么要数据卷

 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -d -P --name nginx nginx
 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker inspect nginx
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/6661385372ff31c9d58c2717b7a7687d781d0fc7d3d89c1a51dd06409ffdafbf-init/diff:/var/lib/docker/overlay2/13473af70546151bbc055c2ec2f2291c45f17c9e4dd8cb433ad83005b9475294/diff:/var/lib/docker/overlay2/c0edf541af68e2da3037e87cd427f8bcf906967eb08891a7383799b061686093/diff:/var/lib/docker/overlay2/7149885d7a9fbdc9ff50f4d51d9250f6ed7927703ef4f122e0d6020a8befe00f/diff",
                "MergedDir": "/var/lib/docker/overlay2/6661385372ff31c9d58c2717b7a7687d781d0fc7d3d89c1a51dd06409ffdafbf/merged",
                "UpperDir": "/var/lib/docker/overlay2/6661385372ff31c9d58c2717b7a7687d781d0fc7d3d89c1a51dd06409ffdafbf/diff",
                "WorkDir": "/var/lib/docker/overlay2/6661385372ff31c9d58c2717b7a7687d781d0fc7d3d89c1a51dd06409ffdafbf/work"
 1.方便修改nginx的配置和html页面等
 2.容器中/usr/share/nginx/html/
 3.容器中/etc/nginx/

4、容器与主机之间的数据拷贝(cp命令)

 # 拷贝目录里的内容
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker cp nginx:/usr/share/nginx/html/. /opt/heiheinginx
 [root@iZ2zeir6vcnpz8qw3t455tZ heiheinginx]# ls
 50x.html index.html
 
 # 如果目录没有创建就把目录的内容拷贝过来
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker cp nginx:/usr/share/nginx/html/ /opt/hahanginx
 [root@iZ2zeir6vcnpz8qw3t455tZ hahanginx]# ls
 50x.html index.html
 
 # 如果目录已经创建就把整个目录拷贝过来
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# mkdir /opt/hehenginx
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker cp nginx:/usr/share/nginx/html /opt/hehenginx/
 [root@iZ2zeir6vcnpz8qw3t455tZ heiheinginx]# cd /opt/hehenginx/
 [root@iZ2zeir6vcnpz8qw3t455tZ hehenginx]# ls
 html
 
 # 不能递归拷贝(连续创建两层目录)
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker cp nginx:/usr/share/nginx/html /opt/ywm/testnginx
 invalid output path: directory "/opt/ywm"does not exist

5、启动容器的两种报错

1.访问内容403
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -d -P --name nginx2 -v /opt/nginx/html:/usr/share/nginx/html/ nginx
 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker ps
 85364f05a690       nginx               "nginx -g 'daemon of…"  6hours ago         Up 6hours          0.0.0.0:32791->80/tcp   nginx

为什么看到的是403而不是index页面内容。原来容器里面变为空?主机的内容复制到容器中,导致,容器同步到了主机的空文件夹。

2.特别是配置文件,必须提前保证我们主机挂载的目录里面提前有内容
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -d -P --name nginx -v /opt/nginx/:/etc/nginx/ nginx
 8488c13fb861aa0a635c7c4ed15bd0e88a2e36afd0303b409138a66462725b68
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker logs -f nginx
 2020/05/03 15:13:26 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
 nginx: [emerg] open() "/etc/nginx/nginx.conf"failed (2: No such file or directory)

6、如何保证web应用正常访问

 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -d -P --name nginx nginx
 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# mkdir /opt/nginx
 
 # 将配置文件拷贝出来
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker cp nginx:/usr/share/nginx/html /opt/nginx/
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker cp nginx:/etc/nginx/. /opt/nginx/conf
 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# cd /opt/nginx/
 [root@iZ2zeir6vcnpz8qw3t455tZ nginx]# ls
 conf html
 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -d -P --name nginx2 -v /opt/nginx/conf:/etc/nginx/ -v /opt/nginx/html:/usr/share/nginx/html nginx

7、匿名挂载

 # 匿名卷方式volume,挂载任意目录
 [root@iZ2zeir6vcnpz8qw3t455tZ nginx]# docker run -d -P --name nginx04 -v /usr/share/nginx/html -v /etc/nginx nginx
 1ae26a3306bcfed2dbff46be2d8c37fe123b108f0f4f669468d879706d4fabdb
 
 [root@iZ2zeir6vcnpz8qw3t455tZ nginx]# docker ps
 CONTAINER ID       IMAGE               COMMAND                 CREATED             STATUS             PORTS                   NAMES
 1ae26a3306bc       nginx               "nginx -g 'daemon of…"  15seconds ago     Up 15seconds       0.0.0.0:32793->80/tcp   nginx04
 
 [root@iZ2zeir6vcnpz8qw3t455tZ nginx]# docker inspect nginx04
        "Mounts": [
           {
                "Type": "volume",
                "Name": "fddc1bd52f8377f25f39af3b528ff856eeacdf78b1eedbe4a385d0820b57c611", # 随机生成的UUID
                "Source": "/var/lib/docker/volumes/fddc1bd52f8377f25f39af3b528ff856eeacdf78b1eedbe4a385d0820b57c611/_data",
                "Destination": "/usr/share/nginx/html",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
           },
           {
                "Type": "volume",
                "Name": "bec16b90bb32cc1827d14fd181458653695663b67d507c063ad1e9cc2cb2889c", # 随机生成的UUID
                "Source": "/var/lib/docker/volumes/bec16b90bb32cc1827d14fd181458653695663b67d507c063ad1e9cc2cb2889c/_data",
                "Destination": "/etc/nginx",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
           }
       ]
        
 [root@iZ2zeir6vcnpz8qw3t455tZ nginx]# cd /var/lib/docker/volumes/fddc1bd52f8377f25f39af3b528ff856eeacdf78b1eedbe4a385d0820b57c611/_data
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# ls
 50x.html index.html
 
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# cd /var/lib/docker/volumes/bec16b90bb32cc1827d14fd181458653695663b67d507c063ad1e9cc2cb2889c/_data
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# ls
 conf.d         koi-utf mime.types nginx.conf   uwsgi_params
 fastcgi_params koi-win modules     scgi_params win-utf
 ⚠️ 缺点:运维不好维护,目录是随机生成的UUID,不好维护。

8、具名挂载

1.管理数据卷
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# docker volume --help
 
 Usage:docker volume COMMAND
 
 Manage volumes
 
 Commands:
 create     Create a volume
 inspect     Display detailed information on one or more volumes
  ls        List volumes
 prune       Remove all unused local volumes
  rm        Remove one or more volumes
 
 Run 'docker volume COMMAND --help'formore information on a command.
2.查看数据卷
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# docker volume ls
 local               bec16b90bb32cc1827d14fd181458653695663b67d507c063ad1e9cc2cb2889c
 local               fddc1bd52f8377f25f39af3b528ff856eeacdf78b1eedbe4a385d0820b57c611
3.查看数据卷具体信息
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# docker volume inspect bec16b90bb32cc1827d14fd181458653695663b67d507c063ad1e9cc2cb2889c
 [
   {
        "CreatedAt": "2020-05-04T21:00:02+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/bec16b90bb32cc1827d14fd181458653695663b67d507c063ad1e9cc2cb2889c/_data",
        "Name": "bec16b90bb32cc1827d14fd181458653695663b67d507c063ad1e9cc2cb2889c",
        "Options": null,
        "Scope": "local"
   }
 ]
4.创建数据卷
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# docker volume create nginxconf
 nginxconf
 
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# docker volume ls
 local               nginxconf
5.查看所创建数据卷的具体信息
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# docker volume inspect nginxconf
 [
   {
        "CreatedAt": "2020-05-04T21:17:09+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/nginxconf/_data",
        "Name": "nginxconf",
        "Options": {},
        "Scope": "local"
   }
 ]
6.自定义数据卷启动服务
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -d -P --name nginx05 -v nginxconf:/etc/nginx -v nginxhtml:/usr/share/nginx/html nginx
 750e4d887d1252206ad8b5c732febdb5cdc4385deea53c712c6eadc346f633af
 
 # nginxhtml没有创建,系统会自动创建
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker volume ls
 local               nginxconf
 local               nginxhtml

9、删除数据卷

 # 先删除容器
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker rm -f $(docker ps -aq)
 
 # 再清理数据卷
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker volume rm nginxhtml
 nginxhtml

10、改变文件读写权限

 # ro只有读权限(只针对docker容器)
 # rw拥有读写权限(只针对docker容器)
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker run -d -P --name nginx07 -v nginx07conf:/etc/nginx:ro -v nginx07html:/usr/share/nginx/html:rw nginx
 eb5e83207b857dc9258b3108e69e2ed46c2329d330e15f5ac48a02b715dfbcc3
 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# docker volume inspect nginx07conf
 [
   {
        "CreatedAt": "2020-05-04T21:33:44+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/nginx07conf/_data",
        "Name": "nginx07conf",
        "Options": null,
        "Scope": "local"
   }
 ]
 
 [root@iZ2zeir6vcnpz8qw3t455tZ ~]# cd /var/lib/docker/volumes/nginx07conf/_data
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# ls
 conf.d         koi-utf mime.types nginx.conf   uwsgi_params
 fastcgi_params koi-win modules     scgi_params win-utf
 
 # 宿主机可以写入
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# echo '111' > 1.txt
 
 [root@iZ2zeir6vcnpz8qw3t455tZ _data]# docker exec -it nginx07 /bin/bash
 root@6e08229fc5f5:/# cd /etc/nginx
 
 # 容器无法写入
 root@6e08229fc5f5:/etc/nginx# echo '111' > 1.txt
 bash: 1.txt: Read-only file system

如果文章有任何错误欢迎不吝赐教,其次大家有任何关于运维的疑难杂问,也欢迎和大家一起交流讨论。关于运维学习、分享、交流,笔者开通了微信公众号【运维猫】,感兴趣的朋友可以关注下,欢迎加入,建立属于我们自己的小圈子,一起学运维知识。群主还经营一家Orchis饰品店,喜欢的小伙伴欢迎????前来下单。

扫描二维码

获取更多精彩

运维猫公众号

有需要技术交流的小伙伴可以加我微信,期待与大家共同成长,本人微信:

扫描二维码

添加私人微信

运维猫博主

扫码加微信

最近有一些星友咨询我知识星球的事,我也想继续在星球上发布更优质的内容供大家学习和探讨。运维猫公众号平台致力于为大家提供免费的学习资源,知识星球主要致力于即将入坑或者已经入坑的运维行业的小伙伴。

点击阅读原文  查看更多精彩内容!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值