8.容器数据卷

2023-11-19  本文已影响0人  _FireFly_

什么是容器数据卷?

docker的理念回顾

将应用环境打包成一个镜像!
数据该怎么处理?如果数据都在容器中,那么我们在删除容器的时候,数据也会被删除丢失。
此时需要找到一个数据可以持久化的办法
Mysql容器删了,删库跑路? 需求:Mysql数据可以存储在本地
容器之间可以有一个数据共享的技术,Dokcer容器中产生的数据,同步到本地。
这种技术就是卷技术,也就是目录的挂载,将我们容器内的目录,挂载在linux上面。

目录的挂载
总结一句话:容器的持久化和同步操作,容器之间也是可以数据共享的。

使用数据卷

方式一:直接使用命令来挂载 -v

直接使用命令来挂载 -v

运行centos

docker run -it -v /home/ceshi:/home centos /bin/bash
#-v:  linux上的home文件夹下的ceshi文件 与docker镜像中的home文件进行映射挂载
# 此时俩个文件夹内容只要有一方进行修改,另一方相应也会发生同样的变化
运行centos

查看镜像上的/home文件的内容

查看镜像上的/home文件的内容

查看linux上的/home/ceshi内容

查看linux上的/home/ceshi内容

查看docker容器中的centos镜像元数据

docker inspect 4b919874be24
[root@localhost ~]# docker inspect 4b919874be24
[
    {
        "Id": "4b919874be24d51856fdeb447c19956a51765f6a44f5f0c992916713699044c5",
        "Created": "2023-11-19T01:52:42.173286272Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1896,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2023-11-19T01:52:43.149374434Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/4b919874be24d51856fdeb447c19956a51765f6a44f5f0c992916713699044c5/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/4b919874be24d51856fdeb447c19956a51765f6a44f5f0c992916713699044c5/hostname",
        "HostsPath": "/var/lib/docker/containers/4b919874be24d51856fdeb447c19956a51765f6a44f5f0c992916713699044c5/hosts",
        "LogPath": "/var/lib/docker/containers/4b919874be24d51856fdeb447c19956a51765f6a44f5f0c992916713699044c5/4b919874be24d51856fdeb447c19956a51765f6a44f5f0c992916713699044c5-json.log",
        "Name": "/amazing_mclaren",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/ceshi:/home"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                46,
                193
            ],
            "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",
                "/sys/devices/virtual/powercap"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/b87a1127a509c3f9ca414a897987fc4720b3c0ab534fe64bfed840439727bc39-init/diff:/var/lib/docker/overlay2/5e980180ea2e6be0c4f4ebaa8e21ef829533f1ae28c06f68c285590ac4de71a7/diff",
                "MergedDir": "/var/lib/docker/overlay2/b87a1127a509c3f9ca414a897987fc4720b3c0ab534fe64bfed840439727bc39/merged",
                "UpperDir": "/var/lib/docker/overlay2/b87a1127a509c3f9ca414a897987fc4720b3c0ab534fe64bfed840439727bc39/diff",
                "WorkDir": "/var/lib/docker/overlay2/b87a1127a509c3f9ca414a897987fc4720b3c0ab534fe64bfed840439727bc39/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/ceshi",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "4b919874be24",
            "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": null,
            "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": "85394b339d65ff82203c7b7e67247282479aac87562a2a45e15ec73bec735ab2",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/85394b339d65",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "285ad0cc1ec17789278ac4113497e419b2e40d07a7f49e2bed51eb5dbe33ff32",
            "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": "d6032c8a10557b37a95ab87aa418a86226166852796e189e4e2880f901955b26",
                    "EndpointID": "285ad0cc1ec17789278ac4113497e419b2e40d07a7f49e2bed51eb5dbe33ff32",
                    "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
                }
            }
        }
    }
]

查看docker容器中的centos镜像元数据
查看docker容器中的centos镜像元数据的挂载对应详情

在docker容器中,退出容器 Ctrl +P+Q :退出窗口 容器继续运行不进行关闭
exit命令:退出窗口 容器停止运行关闭


在docker容器中,退出容器 Ctrl +P+Q

在linux系统中,进入正在运行的docker容器命令

docker exec -it 4b919874be24 /bin/bash
在linux系统中,进入正在运行的docker容器命令

进入容器中的home目录下

进入容器中的home目录下
测试查看同步情况

测试将docker容器停止,在linux上的home/ceshi文件进行修改,发现即使docker容器停止,文件也可以进行同步

解决linux找不到vim命令 linux中的test.java 在linux中的test.java中进行编写
docker attach 4b919874be24    #进入正在运行的命令行
进入正在运行的命令行 查看docker中的home目录下的test.java,发现内容同步成功

结论:我们以后修改只需要在本地修改即可,容器内会自动进行同步~~~

具名挂载和匿名挂载

匿名挂载

-v 容器内路径
使用匿名挂载方式运行nginx

docker run -d -P --name nginx01 -v /etc/nginx nginx   
#-P:随机映射端口
#不指定本地位置  直接-v
#只指定了容器内位置 没有指定容器外地址  

查看本地所有的卷的情况

docker volume ls  #查看所有volume的情况
查看本地所有的卷的情况 匿名卷挂载

具名方式运行nginx

docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
具名方式运行nginx

此时查看卷 里面有了juming-nginx卷名字


此时查看卷

查看具体数据卷的位置

docker volume inspect juming-nginx
查看具体数据卷的位置 查看具体数据卷的位置

所有的docker容器内的卷的位置

image.png

进入docker的工作目录

[root@localhost /]# cd /var/lib/docker
[root@localhost docker]# ls

进入docker的工作目录
所有docker卷的实际位置

查看juming-nginx卷的内容 发现已经把nginx的配置文件全部都复制过来了

[root@localhost /]# cd /var/lib/docker
[root@localhost docker]# ls
buildkit  containers  engine-id  image  network  overlay2  plugins  runtimes  swarm  tmp  volumes
[root@localhost docker]# cd volumes/
[root@localhost volumes]# ls
584f53911411f2f38e1f19647310868e7faff28c7c227daca2554560783994ab  backingFsBlockDev  juming-nginx  metadata.db
[root@localhost volumes]# cd juming-nginx/
[root@localhost juming-nginx]# ls
_data
[root@localhost juming-nginx]# cd _data/
[root@localhost _data]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

查看juming-nginx卷的内容 发现已经把nginx的配置文件全部都复制过来了

具名挂载、匿名挂载、指定路径的挂载 三种挂载的说明

具名挂载、匿名挂载、指定路径的挂载 三种挂载的说明 扩展

多个mysql同步数据

同步原理

运行docker01

docker run -it --name docker01 guobaofeng/centos:1.0
运行docker01 运行docker01

再运行一个docker02 然后通过--volumes-from命令让它同步docker01

docker run -it --name docker02 --volumes-from docker01  guobaofeng/centos:1.0
docker02

再次进入docker01容器

docker attach c7326450eeda
再次进入docker01容器 查看docker02的volume01文件
--volumes-from命令解释
--volumes-from命令解释

再次启动一个docker03也挂在docker01上

docker run -it --name docker03 --volumes-from docker01  guobaofeng/centos:1.0

在docker03的volume01文件夹中再touch一个docker03文件,看看docker01和docker02是否也可以同步创建docker03

在docker03的volume01文件夹中再touch一个docker03文件,看看docker01和docker02是否也可以同步创建docker03

查看docker01的volume01文件夹下是否有docker03

查看docker01的volume01文件夹下是否有docker03
--volumes-from解释
--volumes-from解释

移除docker01 ,查看docker02和docker03的同步是否受影响

docker rm -f c7326450eeda

再次查看docker03的volume01文件,发现虽然docker01移除了,但是docker03的volume01依旧存在

再次查看docker03的volume01文件,发现虽然docker01移除了,但是docker03的volume01依旧存在 --volumes-from 拷贝的概念 多个mysql实现数据共享
结论
上一篇下一篇

猜你喜欢

热点阅读