docker 容器间数据同步 --volumes-from 指定父容器 子容器 有点像主从复制
十年河东,十年河西,莫欺少年穷
学无止境,精益求精
接着上篇博客:docker 镜像的生成方式,初识dockerfile,dockerfile 指定匿名挂载
废话不想多说,心情不好,直接上
1、查看镜像
docker images
镜像如下:
2、查看容器
3、新建一个容器,并通过 --volumes-from 指定父容器,实现数据卷挂载共享
docker run -it --name mydockerimages02 --volumes-from 7ff053ebd0af 38f5719bab58 /bin/bash
4、退出容器,在新建一个名称为: mydockerimages03 的容器
exit docker run -it --name mydockerimages03 --volumes-from 7ff053ebd0af 38f5719bab58 /bin/bash
5、查看数据是否共享成功
[root@23a93bcd984c /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var volume01 volume02 [root@23a93bcd984c /]# ls /volume02 02.txt [root@23a93bcd984c /]# cat /volume02/02.txt I iove shaofu
02.txt 的内容为 i iove saofu 表示,数据共享成功
6、查看容器 03 的挂载信息
[root@localhost dockerfiles]# docker inspect 23a93bcd984c [ { "Id": "23a93bcd984cf5f35eb6ef81197498f8fdac5eb11d18c549cdd9a7f17f5eaf5a", "Created": "2022-04-08T08:18:35.339506509Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "exited", "Running": false, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 0, "ExitCode": 0, "Error": "", "StartedAt": "2022-04-08T08:18:35.920809835Z", "FinishedAt": "2022-04-08T08:22:19.557677364Z" }, "Image": "sha256:38f5719bab581ccd7c7ca0ee03bc7787de5d875713d751c6a4e2f9c24b9dc867", "ResolvConfPath": "/var/lib/docker/containers/23a93bcd984cf5f35eb6ef81197498f8fdac5eb11d18c549cdd9a7f17f5eaf5a/resolv.conf", "HostnamePath": "/var/lib/docker/containers/23a93bcd984cf5f35eb6ef81197498f8fdac5eb11d18c549cdd9a7f17f5eaf5a/hostname", "HostsPath": "/var/lib/docker/containers/23a93bcd984cf5f35eb6ef81197498f8fdac5eb11d18c549cdd9a7f17f5eaf5a/hosts", "LogPath": "/var/lib/docker/containers/23a93bcd984cf5f35eb6ef81197498f8fdac5eb11d18c549cdd9a7f17f5eaf5a/23a93bcd984cf5f35eb6ef81197498f8fdac5eb11d18c549cdd9a7f17f5eaf5a-json.log", "Name": "/mydockerimages03", "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": [ "7ff053ebd0af" ], "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/1034e81c0db89890906dee2a0781de3dece55db77abc3754761780ec36b34aa3-init/diff:/var/lib/docker/overlay2/cf1e13089623186798352e30e58b8fd15ad716904a8a5442bd9c1ad57f8484cb/diff", "MergedDir": "/var/lib/docker/overlay2/1034e81c0db89890906dee2a0781de3dece55db77abc3754761780ec36b34aa3/merged", "UpperDir": "/var/lib/docker/overlay2/1034e81c0db89890906dee2a0781de3dece55db77abc3754761780ec36b34aa3/diff", "WorkDir": "/var/lib/docker/overlay2/1034e81c0db89890906dee2a0781de3dece55db77abc3754761780ec36b34aa3/work" }, "Name": "overlay2" }, "Mounts": [ { "Type": "volume", "Name": "06154073d94c9c6138b9b2c46d2021b5de9a4d972a634d3143536844f7a10f98", "Source": "/var/lib/docker/volumes/06154073d94c9c6138b9b2c46d2021b5de9a4d972a634d3143536844f7a10f98/_data", "Destination": "volume01", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" }, { "Type": "volume", "Name": "177b53a05672781ba288ff566397f0291679c402437c8052d151c039f24421e2", "Source": "/var/lib/docker/volumes/177b53a05672781ba288ff566397f0291679c402437c8052d151c039f24421e2/_data", "Destination": "volume02", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], "Config": { "Hostname": "23a93bcd984c", "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": "38f5719bab58", "Volumes": { "volume01": {}, "volume02": {} }, "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": "37d5bcb4d7115fd615456026286e3fad7a68576b0e0e7b0be7387da879c68239", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/37d5bcb4d711", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "", "Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "MacAddress": "", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "bbd98b26ca3e8fbfcdc5f8e7ebe4d491c296c4c5128d492f49c980d83029e4be", "EndpointID": "", "Gateway": "", "IPAddress": "", "IPPrefixLen": 0, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "", "DriverOpts": null } } } } ] [root@localhost dockerfiles]#
三个容器中 无论修改那么数据卷的文件内容,信息都会同步修改。
即使你把容器删完,在linux本地的文件也不会删除
@天才卧龙的博客