docker学习-数据卷挂载方式
在docker学习中,学习到了数据卷这块,知道了挂载方式分为三种,分别是:匿名挂载,具名挂载,指定路径挂载
实现挂载之后会将你设定的两个路径分别绑定,任何一个下面有变动都会同步
指定路径挂载
docker run -it -v /服务器路径:/容器内路径 centos:7 /bin/bash
具体例子
[root@localhost ~]# docker run -it -v /home/test:/home/test --name="centos04" centos:7 /bin/bash [root@f613054c92f6 /]# cd /home/test/ [root@f613054c92f6 test]# ll total 0 [root@f613054c92f6 test]# touch test.py [root@f613054c92f6 test]# exit exit [root@localhost ~]# cd /home/t test/ txt/ [root@localhost ~]# cd /home/test/ [root@localhost test]# ll 总用量 0 -rw-r--r--. 1 root root 0 4月 29 18:01 test.py
匿名挂载
docker run -it -v /容器内路径 centos:7 /bin/bash
具体列子
[root@localhost test]# docker run -it -v /home/test --name="centos05" centos:7 /bin/bash [root@localhost test]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3d7861b02a3 centos:7 "/bin/bash" 4 minutes ago Exited (0) 4 minutes ago centos05 f613054c92f6 centos:7 "/bin/bash" 7 minutes ago Up 2 minutes centos04 065cb91304a9 centos:7 "/bin/bash" 40 minutes ago Exited (0) 10 minutes ago centos03 b203533606bb centos:7 "/bin/bash" 46 minutes ago Up 43 minutes centos02 [root@localhost test]# docker inspect c3d7861b02a3 [ { "Id": "c3d7861b02a3f680d5249234ca19b4599e0d545bd81bb4fbbee3c39ed95fb779", "Created": "2021-04-29T10:04:21.506482594Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2021-04-29T10:04:21.851035392Z", "FinishedAt": "2021-04-29T10:04:21.850103974Z" }, "Image": "sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf", "ResolvConfPath": "/var/lib/docker/containers/c3d7861b02a3f680d5249234ca19b4599e0d545bd81bb4fbbee3c39ed95fb779/resolv.conf", "HostnamePath": "/var/lib/docker/containers/c3d7861b02a3f680d5249234ca19b4599e0d545bd81bb4fbbee3c39ed95fb779/hostname", "HostsPath": "/var/lib/docker/containers/c3d7861b02a3f680d5249234ca19b4599e0d545bd81bb4fbbee3c39ed95fb779/hosts", "LogPath": "/var/lib/docker/containers/c3d7861b02a3f680d5249234ca19b4599e0d545bd81bb4fbbee3c39ed95fb779/c3d7861b02a3f680d5249234ca19b4599e0d545bd81bb4fbbee3c39ed95fb779-json.log", "Name": "/centos05", "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, "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/7fbff5c8dc47d7cb935388b108082cd9662db2dca8d264530fe4ded9ae56317e-init/diff:/var/lib/docker/overlay2/37354dc5163006f2077c3b174f100154a36017fb45ab7b921a4c6d2f49251e89/diff", "MergedDir": "/var/lib/docker/overlay2/7fbff5c8dc47d7cb935388b108082cd9662db2dca8d264530fe4ded9ae56317e/merged", "UpperDir": "/var/lib/docker/overlay2/7fbff5c8dc47d7cb935388b108082cd9662db2dca8d264530fe4ded9ae56317e/diff", "WorkDir": "/var/lib/docker/overlay2/7fbff5c8dc47d7cb935388b108082cd9662db2dca8d264530fe4ded9ae56317e/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "a38b94e2dc0d8192782fbb16888a1c18462d5f74a36ada631169006ba9b3e277", "Source": "/var/lib/docker/volumes/a38b94e2dc0d8192782fbb16888a1c18462d5f74a36ada631169006ba9b3e277/_data", # 挂载的服务器的地址 "Destination": "/home/test", # 挂载的容器地址 "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "c3d7861b02a3", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" ], "Cmd": [ "/bin/bash" ], "Image": "centos:7", "Volumes": { "/home/test": {} }, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201113", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS", "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00", "org.opencontainers.image.licenses": "GPL-2.0-only", "org.opencontainers.image.title": "CentOS Base Image", "org.opencontainers.image.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "55fc030b756625f2cd2337a78b547480854f80e2d09e666c9a7105f2a2fbad70", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/55fc030b7566", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "136ad104ebba32aa0e5a6a0303ffbdaee9c485b0f6773601bc3de551dfd27de3", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ]
具名挂载
docker run -it -v 卷名:/容器内路径 centos:7 /bin/bash
具体例子(或者直接使用:docker inspect 卷名来查看路径)
[root@localhost test]# docker run -it -v test:/home/test --name="centos06" centos:7 /bin/bash [root@afff5dbc7d58 /]# exit [root@localhost test]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES afff5dbc7d58 centos:7 "/bin/bash" About a minute ago Exited (0) About a minute ago centos06 c3d7861b02a3 centos:7 "/bin/bash" 10 minutes ago Exited (0) 10 minutes ago centos05 f613054c92f6 centos:7 "/bin/bash" 13 minutes ago Up 8 minutes centos04 065cb91304a9 centos:7 "/bin/bash" 45 minutes ago Exited (0) 16 minutes ago centos03 b203533606bb centos:7 "/bin/bash" 51 minutes ago Up 49 minutes centos02 [root@localhost test]# docker inspect afff5dbc7d58 [ { "Id": "afff5dbc7d58a251a559c7299eb9dd943612f0e5e803e41ac2b0564cf3c9317f", "Created": "2021-04-29T10:13:40.855076144Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2021-04-29T10:13:41.851901079Z", "FinishedAt": "2021-04-29T10:13:45.028580786Z" }, "Image": "sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf", "ResolvConfPath": "/var/lib/docker/containers/afff5dbc7d58a251a559c7299eb9dd943612f0e5e803e41ac2b0564cf3c9317f/resolv.conf", "HostnamePath": "/var/lib/docker/containers/afff5dbc7d58a251a559c7299eb9dd943612f0e5e803e41ac2b0564cf3c9317f/hostname", "HostsPath": "/var/lib/docker/containers/afff5dbc7d58a251a559c7299eb9dd943612f0e5e803e41ac2b0564cf3c9317f/hosts", "LogPath": "/var/lib/docker/containers/afff5dbc7d58a251a559c7299eb9dd943612f0e5e803e41ac2b0564cf3c9317f/afff5dbc7d58a251a559c7299eb9dd943612f0e5e803e41ac2b0564cf3c9317f-json.log", "Name": "/centos06", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": [ "test:/home/test" ], "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/068810a461fc9420a9bd523198eaff25c5c673ac94261b947ee5e6a4dac2b548-init/diff:/var/lib/docker/overlay2/37354dc5163006f2077c3b174f100154a36017fb45ab7b921a4c6d2f49251e89/diff", "MergedDir": "/var/lib/docker/overlay2/068810a461fc9420a9bd523198eaff25c5c673ac94261b947ee5e6a4dac2b548/merged", "UpperDir": "/var/lib/docker/overlay2/068810a461fc9420a9bd523198eaff25c5c673ac94261b947ee5e6a4dac2b548/diff", "WorkDir": "/var/lib/docker/overlay2/068810a461fc9420a9bd523198eaff25c5c673ac94261b947ee5e6a4dac2b548/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "test", "Source": "/var/lib/docker/volumes/test/_data", "Destination": "/home/test", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "afff5dbc7d58", "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:7", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20201113", "org.label-schema.license": "GPLv2", "org.label-schema.name": "CentOS Base Image", "org.label-schema.schema-version": "1.0", "org.label-schema.vendor": "CentOS", "org.opencontainers.image.created": "2020-11-13 00:00:00+00:00", "org.opencontainers.image.licenses": "GPL-2.0-only", "org.opencontainers.image.title": "CentOS Base Image", "org.opencontainers.image.vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "4d5e0a5cf4164c3e70b5bd074ce6a881a7e084c9652cf4cb80ce84775e84dc78", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/4d5e0a5cf416", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "136ad104ebba32aa0e5a6a0303ffbdaee9c485b0f6773601bc3de551dfd27de3", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ] [root@localhost test]#