Docker 数据管理

文章目录

前言

为什么要有数据管理?

因为:

  • Docker 是不提供持久化的 ,容器是不稳定的;
  • 一个容器一个服务;
  • 多个容器可能要共享一块数据;
  • docker容器每次创建都是新开一个容器,不必理会旧的容器什么状态。

这种情况下,每次的更新部署非常方便,但是这样一来所有的数据就需要持久化存储到宿主机里。就引出了docker的数据管理知识。

1、Dcoker 文件体系

docker 数据卷默认位置:

/var/lib/docker/volumes

挂载以后,目录是共享的;数据不应该放到容器中,而是要求宿主机分一块目录给容器用来存放数据。

删除容器,是不会删除volumes目录的(强制除外)

2、volume挂载案例

2.1、挂载运行一个容器实例

方法1

bash 复制代码
docker run -it --name centosTest -v /opt/data centos /bin/bash

此刻就进入了容器内部。

然后查看确实产生了 /opt/data 目录,在该目录下新建一个123 文件,并去查看宿主机目录下是否存在:

然后先查看 容器:

查看元数据及文件新增

bash 复制代码
[
    {
        "Id": "dee355965bca29c9355a5e9bff5132f89337b59b4e30c2459e89b6f64238ad2f",
        "Created": "2023-08-06T11:12:05.096013057Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 14786,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-08-06T11:12:05.96660496Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/dee355965bca29c9355a5e9bff5132f89337b59b4e30c2459e89b6f64238ad2f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/dee355965bca29c9355a5e9bff5132f89337b59b4e30c2459e89b6f64238ad2f/hostname",
        "HostsPath": "/var/lib/docker/containers/dee355965bca29c9355a5e9bff5132f89337b59b4e30c2459e89b6f64238ad2f/hosts",
        "LogPath": "/var/lib/docker/containers/dee355965bca29c9355a5e9bff5132f89337b59b4e30c2459e89b6f64238ad2f/dee355965bca29c9355a5e9bff5132f89337b59b4e30c2459e89b6f64238ad2f-json.log",
        "Name": "/centosTest",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                50,
                180
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": [],
            "BlkioDeviceWriteBps": [],
            "BlkioDeviceReadIOps": [],
            "BlkioDeviceWriteIOps": [],
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/6b5f1d67ac77a57910dbe442356bec38416d3fa9f355c9810a04de8a0032c20a-init/diff:/var/lib/docker/overlay2/0351cac31df5ec6bd716f7dd314842417100097dcad45cdbf3b2b602df85ac0d/diff",
                "MergedDir": "/var/lib/docker/overlay2/6b5f1d67ac77a57910dbe442356bec38416d3fa9f355c9810a04de8a0032c20a/merged",
                "UpperDir": "/var/lib/docker/overlay2/6b5f1d67ac77a57910dbe442356bec38416d3fa9f355c9810a04de8a0032c20a/diff",
                "WorkDir": "/var/lib/docker/overlay2/6b5f1d67ac77a57910dbe442356bec38416d3fa9f355c9810a04de8a0032c20a/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "volume",
                "Name": "75d6a87411d050df05a6a6b9179ff9abbd89afc34641ce7bbf435d6671172565",
                "Source": "/var/lib/docker/volumes/75d6a87411d050df05a6a6b9179ff9abbd89afc34641ce7bbf435d6671172565/_data",
                "Destination": "/opt/data",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
            "Hostname": "dee355965bca",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "centos",
            "Volumes": {
                "/opt/data": {}
            },
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "f35da6a5c2575f9ed03c6474b59c0358c9b3944ce5f0fb6b20655df61a2dc185",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/f35da6a5c257",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "ba7973b2a91ca6dc3e75351903537ecd4158116edfc2871a2c94a8d77a19cc95",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "348aaaac8b83d35b8f2b750e66eefa696566a774d5791710e2b15e0e3d101332",
                    "EndpointID": "ba7973b2a91ca6dc3e75351903537ecd4158116edfc2871a2c94a8d77a19cc95",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

里面的 Mounts 部分 是挂载的意思:

然后查看对应的宿主机位置:

bash 复制代码
ls /var/lib/docker/volumes/75d6a87411d050df05a6a6b9179ff9abbd89afc34641ce7bbf435d6671172565/_data

这里确实看到了之前创建的文件。

然后进入宿主机目录下新增文件,看容器内部是否会出现同样的文件:

转到容器终端窗口:

同样产生了 456文件。两边是互通的。

方法2

命令:

bash 复制代码
docker run -it --name centosTest2 -v /opt/data:/data centos /bin/bash

这种方式对于容器来说是一样的,对于宿主机来说,多出了如下目录:

需要修改主机目录时,可以使用该命令。

3、volumes-from 案例

引用数据卷命令:

bash 复制代码
docker run -it --name cen1 --volumes-from centosTest centos

4、备份/恢复数据卷

备份:

bash 复制代码
docker run --rm --volumes-from data -v $(pwd):/backup centos tar cvf /backup/data.tar /opt/data

恢复:

bash 复制代码
docker run --rm --volumes-from data -v $(pwd):/backup centos tar xvf /backup/data.tar -C /

释义:

docker run --rm ----- 启动一个新的容器,执行完毕删除

--volumes-from data ------- data容器中挂载卷

-v $(pwd):/backup --------挂载当前目录到容器中为backup

cvf /backup/data.tar /opt/data --------- 备份/opt/data目录(即卷中所有的数据)为data.tar

xvf /backup/data.tar -C / ---------- 解压data.tar 到根目录/ ,因tar归档中已包含了/opt/data路径

5、删除数据卷

bash 复制代码
docker rm -v data
相关推荐
运维老司机1 分钟前
Jenkins修改LOGO
运维·自动化·jenkins
D-海漠17 分钟前
基础自动化系统的特点
运维·自动化
我言秋日胜春朝★25 分钟前
【Linux】进程地址空间
linux·运维·服务器
C-cat.1 小时前
Linux|环境变量
linux·运维·服务器
wenyue11211 小时前
Revolutionize Your Kubernetes Experience with Easegress: Kubernetes Gateway API
容器·kubernetes·gateway
yunfanleo1 小时前
docker run m3e 配置网络,自动重启,GPU等 配置渠道要点
linux·运维·docker
烦躁的大鼻嘎2 小时前
【Linux】深入理解GCC/G++编译流程及库文件管理
linux·运维·服务器
乐大师2 小时前
Deepin登录后提示“解锁登陆密钥环里的密码不匹配”
运维·服务器
ac.char2 小时前
在 Ubuntu 上安装 Yarn 环境
linux·运维·服务器·ubuntu
敲上瘾2 小时前
操作系统的理解
linux·运维·服务器·c++·大模型·操作系统·aigc