天天看點

Docker資料卷

卷是目錄或檔案,存在于一個或多個容器中,由docker挂載到容器,但不屬于聯合檔案系統,是以能夠繞過聯合檔案系統提供一些用于持續存儲或共享資料的特性:

卷的目的就是資料持久化,完全獨立于容器的生命周期,是以Docker不會在容器删除時候删除其挂載的資料卷。

特點:

1.資料卷可在容器之間共享或重用資料

2.卷中更改可以直接生效

3.資料卷中的更改不會包含在鏡像的更新中

4.資料卷的生命周期一直持續到沒有容器使用它為止

容器内添加資料卷有兩種方式:

1.直接指令添加

2.DockFile添加

docker run -it /主控端絕對路徑目錄:/容器内目錄 鏡像名

docker run -it -v /myDataVolume:/dataVolumerContainer centos
           

啟動容器:

Docker資料卷

主控端中檢視:

Docker資料卷

主控端中執行

Docker資料卷
[
    {
        "Id": "c7176f1ca5ccd412138c6aefc0ae6456a118d7df2fa30efa2010931b051b2b13",
        "Created": "2021-07-31T13:18:15.879445403Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 5383,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-31T13:18:17.602809512Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/c7176f1ca5ccd412138c6aefc0ae6456a118d7df2fa30efa2010931b051b2b13/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/c7176f1ca5ccd412138c6aefc0ae6456a118d7df2fa30efa2010931b051b2b13/hostname",
        "HostsPath": "/var/lib/docker/containers/c7176f1ca5ccd412138c6aefc0ae6456a118d7df2fa30efa2010931b051b2b13/hosts",
        "LogPath": "/var/lib/docker/containers/c7176f1ca5ccd412138c6aefc0ae6456a118d7df2fa30efa2010931b051b2b13/c7176f1ca5ccd412138c6aefc0ae6456a118d7df2fa30efa2010931b051b2b13-json.log",
        "Name": "/flamboyant_euler",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [
                "/myDataVolume:/dataVolumerContainer"
            ],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "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",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "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/29112dcdd9f0b15b16a2a17e88a7f0953df0d4699dad280745df0fe27a9ba34d-init/diff:/var/lib/docker/overlay2/3f7ee985dd7d958a3dcc02a25152a8037dd8f5cf51ff69d49929459fcd0ceff4/diff",
                "MergedDir": "/var/lib/docker/overlay2/29112dcdd9f0b15b16a2a17e88a7f0953df0d4699dad280745df0fe27a9ba34d/merged",
                "UpperDir": "/var/lib/docker/overlay2/29112dcdd9f0b15b16a2a17e88a7f0953df0d4699dad280745df0fe27a9ba34d/diff",
                "WorkDir": "/var/lib/docker/overlay2/29112dcdd9f0b15b16a2a17e88a7f0953df0d4699dad280745df0fe27a9ba34d/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/myDataVolume",
                "Destination": "/dataVolumerContainer",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
            "Hostname": "c7176f1ca5cc",
            "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": "20201204",
                "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": "540fb4961552173dcf8fbcef224b68ad979e0d69d4c446b0b0b30971f4205c47",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/540fb4961552",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "0acfad2c74e67b334e19a0e80c8fb29e7a04fed1a4bb5595ee49e47fc361c1f6",
            "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": "76c08e7b852eed7c164b40f637886bae674b2ccfc13f1248a6a5a2780426852b",
                    "EndpointID": "0acfad2c74e67b334e19a0e80c8fb29e7a04fed1a4bb5595ee49e47fc361c1f6",
                    "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
                }
            }
        }
    }
]
           

主控端

touch test.txt           
Docker資料卷

容器中檢視:

繼續閱讀