数据管理
这一章介绍如何在 Docker 内部以及容器之间管理数据,在容器中管理数据主要有两种方式:
数据卷(Volumes)
挂载主机目录 (Bind mounts)
数据卷
1.数据卷 可以在容器之间共享和重用
2.数据卷 的修改会立马生效
3.数据卷 的更新,不会影响镜像
4.数据卷 默认会一直存在,即使容器被删除
注意: 数据卷 的使用,类似于 Linux 下对目录或文件进行 mount,镜像中的被指定为挂
载点的目录中的文件会隐藏掉,能显示看的是挂载的 数据卷 。
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu 16.04 657d80a6401d Less than a second ago 121MB nginx latest ab56bba91343 Less than a second ago 126MB ubuntu v2 a66d0fda4c36 5 hours ago 86.1MB <none> <none> 986426ddabcf 5 hours ago 86.1MB nginx v2 06292a546f31 25 hours ago 126MB commit-test v1 28a5df57c0b9 45 hours ago 447MB commit-test v2 28a5df57c0b9 45 hours ago 447MB erixero/centos7.3-base-image latest ae904afa83bb 2 years ago 288MB ansible/centos7-ansible latest 688353a31fde 2 years ago 447MB [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c7a2a75a5536 nginx "nginx -g 'daemon of��" 26 hours ago Up 26 hours 0.0.0.0:80->80/tcp webserver ce4ccea0bde8 ansible/centos7-ansible "bash" 47 hours ago Up 47 hours web3 [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# docker run -itd -P -v ghd_volume:/data centos7.3-base-image bash Unable to find image 'centos7.3-base-image:latest' locally ^C [root@localhost ~]# docker run -itd -P -v ghd_volume:/data erixero/centos7.3-base-image bash f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f6bbea443680 erixero/centos7.3-base-image "bash" 6 seconds ago Up 5 seconds vibrant_lalande c7a2a75a5536 nginx "nginx -g 'daemon of��" 26 hours ago Up 26 hours 0.0.0.0:80->80/tcp webserver ce4ccea0bde8 ansible/centos7-ansible "bash" 47 hours ago Up 47 hours web3 [root@localhost ~]# docker inspect f6bbea443680 [ { "Id": "f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce", "Created": "2019-08-31T08:53:49.082498253Z", "Path": "bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 87155, "ExitCode": 0, "Error": "", "StartedAt": "2019-08-31T08:53:50.080729254Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:ae904afa83bbbb1eb542c356fb42a15f39ce96ce0dba9aa49a8fe51f18a44116", "ResolvConfPath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/resolv.conf", "HostnamePath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/hostname", "HostsPath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/hosts", "LogPath": "/var/lib/docker/containers/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce/f6bbea44368060f520a27915e5446ad0cde67ebb4cad99c39370b6b6aa7fa8ce-json.log", "Name": "/vibrant_lalande", "RestartCount": 0, "Driver": "devicemapper", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": [ "ghd_volume:/data" ], "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Capabilities": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": true, "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": { "DeviceId": "93", "DeviceName": "docker-253:0-51547343-1e12217a5354101071e294c14c2d50853608f06644729fc76a240de07c2276d5", "DeviceSize": "10737418240" }, "Name": "devicemapper" }, "Mounts": [ { "Type": "volume", "Name": "ghd_volume", "Source": "/var/lib/docker/volumes/ghd_volume/_data", "Destination": "/data", "Driver": "local", "Mode": "z", "RW": true, "Propagation": "" } ],
删除数据卷
[root@localhost ~]# docker volume rm ghd_volume
挂载主机目录
--mount和-v的区别,-v如果容器中没有目标目录会自动创建,使用--mount如果目标目录不存在会报错,为了避免挂错的情况发生,还是建议使用--mount
docker run -it --mount type=bind,source=/opt,target=/opt erixero/centos7.3-base-image bash bash-4.2# cd /opt/ bash-4.2# ls bbb.txt containerd dockerfile ghd redis ubuntu.tar.gz bash-4.2# exit [root@localhost ~]# cd /opt/ [root@localhost opt]# ls bbb.txt containerd dockerfile ghd redis ubuntu.tar.gz #可以看到/opt目录已挂在到容器中