引言
如果对Docker不太熟悉的小伙伴们可以先去看一下Docker入门,再来看这篇文章哟!
https://blog.youkuaiyun.com/m0_53559551/article/details/121358336
那么如果是从我Docker入门文章过来的或者是对Docker有个基本了解的小伙伴们现在跟我一起学习一下Docker的容器数据卷吧。
什么是容器数据卷?
如果数据在容器中,那么删除容器后数据也跟着删除了,那么我们不希望数据也跟着丢失,就需要数据持久化,这时就要用的容器数据卷,Docker容器中产生的数据会跟着同步到本地,也就是目录的挂载,容器中的目录挂载到本地。
使用容器数据卷
直接使用命令挂载 -v
docker run -it -v 主机目录:容器内目录
PS C:\home> docker run -it -v /home/ceshi:/home centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
接下来看下是否挂载上了
PS C:\home> docker inspect c41a549b0655
[
{
"Id": "c41a549b065575493d52dfb2aa08fc1a0a4acd46fc0ec50f72dd124058f846ec",
"Created": "2021-11-29T08:16:49.35722Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1546,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-11-29T08:16:51.9560701Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/c41a549b065575493d52dfb2aa08fc1a0a4acd46fc0ec50f72dd124058f846ec/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c41a549b065575493d52dfb2aa08fc1a0a4acd46fc0ec50f72dd124058f846ec/hostname",
"HostsPath": "/var/lib/docker/containers/c41a549b065575493d52dfb2aa08fc1a0a4acd46fc0ec50f72dd124058f846ec/hosts",
"LogPath": "/var/lib/docker/containers/c41a549b065575493d52dfb2aa08fc1a0a4acd46fc0ec50f72dd124058f846ec/c41a549b065575493d52dfb2aa08fc1a0a4acd46fc0ec50f72dd124058f846ec-json.log",
"Name": "/jovial_torvalds",
"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,
"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": [
50,
120
],
"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/f5e86750de02f45236e523870ddd3a53319ad2fd24849e34fbbb60fafebf7807-init/diff:/var/lib/docker/overlay2/4c9e39e6a74ffc64a7d06d6aec6d31a73ded79645671bcae69e357817c362d32/diff",
"MergedDir": "/var/lib/docker/overlay2/f5e86750de02f45236e523870ddd3a53319ad2fd24849e34fbbb60fafebf7807/merged",
"UpperDir": "/var/lib/docker/overlay2/f5e86750de02f45236e523870ddd3a53319ad2fd24849e34fbbb60fafebf7807/diff",
"WorkDir": "/var/lib/docker/overlay2/f5e86750de02f45236e523870ddd3a53319ad2fd24849e34fbbb60fafebf7807/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/home/ceshi",
"Destination": "/home",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "c41a549b0655",
"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": "481615ddea80e186624c00093a59d1048b4be6feed52f4b6023ee94ce29b8cf7",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/481615ddea80",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "cd1336cee04dff3372b59e50a92da5dc3d98d6cc89c9d9a713d0b3731f2a2c0a",
"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": "0f01c6319ec1fb1bb8534fb25e88a6a70a5031c71623ecc3f4b108fcf586ae6a",
"EndpointID": "cd1336cee04dff3372b59e50a92da5dc3d98d6cc89c9d9a713d0b3731f2a2c0a",
"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”: [
{
“Type”: “bind”,
“Source”: “/home/ceshi”,
“Destination”: “/home”,
“Mode”: “”,
“RW”: true,
“Propagation”: “rprivate”
}
],
这一段就证明他们已经挂载上了!!!小伙伴们可以在主机/home/ceshi里加内容或者容器的/home加内容,他们的数据都会同步,这一点可以理解为双向绑定!