容器数据卷

**由来**

Docker 是将应用和环境打包成一个镜像。 这样,数据就不应该保存在容器中,否则容器删除,数据就会丢失,有着非常大的风险。 为此,容器和主机之间需要有一个数据共享技术,使得在 Docker 容器中产生的数据能够同步到本地。 这就是数据卷技术。其本质上是一个目录挂载,将容器内的目录挂载到主机上。

**使用**

1.挂载

[root@localhost ~]# `docker run -it -v /home/roo/:/home centos /bin/bash`

[root@439732944dea /]# cd home/
[root@439732944dea home]# ls
[root@439732944dea home]# touch a.txt
[root@439732944dea home]# ls
a.txt
[root@439732944dea home]# touch b.txt
[root@439732944dea home]# touch c.txt
[root@439732944dea home]# ls         
a.txt  b.txt  bbb  c.txt

2.主机查看

[root@localhost ~]# cd /home/
[root@localhost home]# ls
roo
[root@localhost home]# cd roo/
[root@localhost roo]# ls
a.txt  b.txt  c.txt
[root@localhost roo]# touch bbb
[root@localhost roo]# ls
a.txt  bbb  b.txt  c.txt

3.查看挂载信息

[root@localhost roo]# docker inspect 439732944dea

查看代码
 [
    {
        "Id": "439732944dea832eb6c55d08966a87260b45260d6bf8fbeb59e81a5a851ceb65",
        "Created": "2024-05-28T12:54:50.427403411Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 15119,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2024-05-28T12:54:50.743374799Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/439732944dea832eb6c55d08966a87260b45260d6bf8fbeb59e81a5a851ceb65/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/439732944dea832eb6c55d08966a87260b45260d6bf8fbeb59e81a5a851ceb65/hostname",
        "HostsPath": "/var/lib/docker/containers/439732944dea832eb6c55d08966a87260b45260d6bf8fbeb59e81a5a851ceb65/hosts",
        "LogPath": "/var/lib/docker/containers/439732944dea832eb6c55d08966a87260b45260d6bf8fbeb59e81a5a851ceb65/439732944dea832eb6c55d08966a87260b45260d6bf8fbeb59e81a5a851ceb65-json.log",
        "Name": "/pensive_bartik",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/home/roo/:/home"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "bridge",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "ConsoleSize": [
                41,
                102
            ],
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "private",
            "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": null,
            "PidsLimit": null,
            "Ulimits": [],
            "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/1055d2ae164ff6fdacc9432765a585e2274d4eb5eb9f25ea5709cbe8a69ba2ec-init/diff:/var/lib/docker/overlay2/5011e75a42f46fdc508b256c9d323103abcc258f08f1608f57e813a4fd47bfec/diff",
                "MergedDir": "/var/lib/docker/overlay2/1055d2ae164ff6fdacc9432765a585e2274d4eb5eb9f25ea5709cbe8a69ba2ec/merged",
                "UpperDir": "/var/lib/docker/overlay2/1055d2ae164ff6fdacc9432765a585e2274d4eb5eb9f25ea5709cbe8a69ba2ec/diff",
                "WorkDir": "/var/lib/docker/overlay2/1055d2ae164ff6fdacc9432765a585e2274d4eb5eb9f25ea5709cbe8a69ba2ec/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/roo",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "439732944dea",
            "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": "3789d1f9b1472ad5e63ae546afdcd5ba77211cda54910389775f72c5ac790f75",
            "SandboxKey": "/var/run/docker/netns/3789d1f9b147",
            "Ports": {},
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "c4a436e01d0099b66f1fbabf53c39e87db6885fc01f9a9b1bc0f3afc4a4a18cc",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.3",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:03",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "MacAddress": "02:42:ac:11:00:03",
                    "NetworkID": "e45fcd10035c41062d04781650d7ede49afc5a3671a9a52d7d52c13f43c8036e",
                    "EndpointID": "c4a436e01d0099b66f1fbabf53c39e87db6885fc01f9a9b1bc0f3afc4a4a18cc",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "DriverOpts": null,
                    "DNSNames": null
                }
            }
        }
    }
]

posted on   沐心q  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示