容器使用卷
1.创建一个卷并让容器挂载该卷
1.创建一个卷
[root@host1 ~]# docker volume create test-vol
test-vol
2.列出本地 Docker 主机上的卷
[root@host1 ~]# docker volume ls
DRIVER VOLUME NAME
local test-vol
3.查看该卷的详细信息
[root@host1 ~]# docker volume inspect test-vol
[{"CreatedAt": "2025-09-12T13:36:19+08:00","Driver": "local","Labels": null,"Mountpoint": "/var/lib/docker/volumes/test-vol/_data","Name": "test-vol","Options": null,"Scope": "local"}
]
4.启动一个容器,并将 test-vol 卷挂载到容器中的 /world 目录
[root@host1 ~]# docker run -it --mount source=test-vol,target=/world ubuntu /bin/bash
root@c80a58c8574d:/#
5.在容器中列出目录
root@c80a58c8574d:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr world
6.退出该容器
root@c80a58c8574d:/# exit
exit
7.查看容器详细信息
[root@host1 ~]# docker inspect c80a58
[{"Id": "c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92","Created": "2025-09-12T05:39:47.881462671Z","Path": "/bin/bash","Args": [],"State": {"Status": "exited","Running": false,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 0,"ExitCode": 0,"Error": "","StartedAt": "2025-09-12T05:39:47.900715314Z","FinishedAt": "2025-09-12T05:40:41.484171725Z"},"Image": "sha256:802541663949fbd5bbd8f35045af10005f51885164e798e2ee8d1dc39ed8888d","ResolvConfPath": "/var/lib/docker/containers/c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92/resolv.conf","HostnamePath": "/var/lib/docker/containers/c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92/hostname","HostsPath": "/var/lib/docker/containers/c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92/hosts","LogPath": "/var/lib/docker/containers/c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92/c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92-json.log","Name": "/nostalgic_curie","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "bridge","PortBindings": {},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"ConsoleSize": [36,101],"CapAdd": null,"CapDrop": null,"CgroupnsMode": "private","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","Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": [],"BlkioDeviceReadBps": [],"BlkioDeviceWriteBps": [],"BlkioDeviceReadIOps": [],"BlkioDeviceWriteIOps": [],"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": [],"DeviceCgroupRules": null,"DeviceRequests": null,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": null,"PidsLimit": null,"Ulimits": [],"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"Mounts": [{"Type": "volume","Source": "test-vol","Target": "/world"}],"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware","/sys/devices/virtual/powercap"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"ID": "c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92","LowerDir": "/var/lib/docker/overlay2/7e0526682fe66a44de0b861118fc4d8692f429dedecc2f4c8585e55e776fa8c7-init/diff:/var/lib/docker/overlay2/e721707f22e5c14f3e0591ab1c2c6cb336f864c6033c36cbd4834cdb84113b6b/diff","MergedDir": "/var/lib/docker/overlay2/7e0526682fe66a44de0b861118fc4d8692f429dedecc2f4c8585e55e776fa8c7/merged","UpperDir": "/var/lib/docker/overlay2/7e0526682fe66a44de0b861118fc4d8692f429dedecc2f4c8585e55e776fa8c7/diff","WorkDir": "/var/lib/docker/overlay2/7e0526682fe66a44de0b861118fc4d8692f429dedecc2f4c8585e55e776fa8c7/work"},"Name": "overlay2"},"Mounts": [{"Type": "volume","Name": "test-vol","Source": "/var/lib/docker/volumes/test-vol/_data","Destination": "/world","Driver": "local","Mode": "z","RW": true,"Propagation": ""}],"Config": {"Hostname": "c80a58c8574d","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": "ubuntu","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.opencontainers.image.ref.name": "ubuntu","org.opencontainers.image.version": "24.04"}},"NetworkSettings": {"Bridge": "","SandboxID": "","SandboxKey": "","Ports": {},"HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "","Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "","IPPrefixLen": 0,"IPv6Gateway": "","MacAddress": "","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"MacAddress": "","DriverOpts": null,"GwPriority": 0,"NetworkID": "2dba964368f785973cac101f2112a37a034ddf1667583203846b25e06839cac9","EndpointID": "","Gateway": "","IPAddress": "","IPPrefixLen": 0,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"DNSNames": null}}}}
]
8.删除该卷
[root@host1 ~]# docker volume rm test-vol
Error response from daemon: remove test-vol: volume is in use - [c80a58c8574dd8eb57522ee034a38703af68465ad5e6094c6ca4931f80abff92]
9.删除该容器之后,即可成功删除该卷
[root@host1 ~]# docker container rm c80a58
c80a58
[root@host1 ~]# docker volume rm test-vol
test-vol
2.启动容器时自动创建卷
[root@host1 ~]# docker run -d --name nginx-autovol --mount source=myvol,target=/app nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
d107e437f729: Pull complete
cb497a329a81: Pull complete
f1c4d397f477: Pull complete
f72106e86507: Pull complete
899c83fc198b: Pull complete
a785b80f5a67: Pull complete
6c50e4e0c439: Pull complete
Digest: sha256:d5f28ef21aabddd098f3dbc21fe5b7a7d7a184720bc07da0b6c9b9820e97f25e
Status: Downloaded newer image for nginx:latest
d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056
查看
[root@host1 ~]# docker inspect nginx-autovol
[{"Id": "d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056","Created": "2025-09-12T05:55:15.494302078Z","Path": "/docker-entrypoint.sh","Args": ["nginx","-g","daemon off;"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 170175,"ExitCode": 0,"Error": "","StartedAt": "2025-09-12T05:55:15.663704532Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:41f689c209100e6cadf3ce7fdd02035e90dbd1d586716bf8fc6ea55c365b2d81","ResolvConfPath": "/var/lib/docker/containers/d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056/resolv.conf","HostnamePath": "/var/lib/docker/containers/d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056/hostname","HostsPath": "/var/lib/docker/containers/d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056/hosts","LogPath": "/var/lib/docker/containers/d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056/d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056-json.log","Name": "/nginx-autovol","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "bridge","PortBindings": {},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"ConsoleSize": [36,101],"CapAdd": null,"CapDrop": null,"CgroupnsMode": "private","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","Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": [],"BlkioDeviceReadBps": [],"BlkioDeviceWriteBps": [],"BlkioDeviceReadIOps": [],"BlkioDeviceWriteIOps": [],"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": [],"DeviceCgroupRules": null,"DeviceRequests": null,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": null,"PidsLimit": null,"Ulimits": [],"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"Mounts": [{"Type": "volume","Source": "myvol","Target": "/app"}],"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware","/sys/devices/virtual/powercap"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"ID": "d9fcfc06b30d759704db72bd3de1ae7c6959e61cdcd286b247d613c438569056","LowerDir": "/var/lib/docker/overlay2/20dd9450b1e2186125c79bed645dc4e7a65b36ad4fa3e45411639319b1b751b2-init/diff:/var/lib/docker/overlay2/8b57d2f007b17a853b402d7981962ae0217011bd75f8f4aa6ec1288aebb0dc7e/diff:/var/lib/docker/overlay2/7c5719df2bf53f07bea95c0dab086a15fc9c3b85bea2225098d49d78a8a766e8/diff:/var/lib/docker/overlay2/5a31009658ea6524de00dca63c21fc65f89a0e88be6264074c0ea65be9bfd24c/diff:/var/lib/docker/overlay2/6655e09d62aa340b5368fe6a07108907f813224356ee4cfa7a8cfa802ec87e66/diff:/var/lib/docker/overlay2/23c934857988d94756762e22771276121b35cab43c4549582e8d7d54d2c7ecf1/diff:/var/lib/docker/overlay2/7959c6d39ca832474bf8aba5077a8d407f7f4d087a8c8480d8c282e0deae1ad1/diff:/var/lib/docker/overlay2/ca8efaabeb43dce901c82c3111b6f5d376ee91641aa5c3268760982d8552d4d1/diff","MergedDir": "/var/lib/docker/overlay2/20dd9450b1e2186125c79bed645dc4e7a65b36ad4fa3e45411639319b1b751b2/merged","UpperDir": "/var/lib/docker/overlay2/20dd9450b1e2186125c79bed645dc4e7a65b36ad4fa3e45411639319b1b751b2/diff","WorkDir": "/var/lib/docker/overlay2/20dd9450b1e2186125c79bed645dc4e7a65b36ad4fa3e45411639319b1b751b2/work"},"Name": "overlay2"},"Mounts": [{"Type": "volume","Name": "myvol","Source": "/var/lib/docker/volumes/myvol/_data","Destination": "/app","Driver": "local","Mode": "z","RW": true,"Propagation": ""}],"Config": {"Hostname": "d9fcfc06b30d","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"ExposedPorts": {"80/tcp": {}},"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NGINX_VERSION=1.29.1","NJS_VERSION=0.9.1","NJS_RELEASE=1~bookworm","PKG_RELEASE=1~bookworm","DYNPKG_RELEASE=1~bookworm"],"Cmd": ["nginx","-g","daemon off;"],"Image": "nginx","Volumes": null,"WorkingDir": "","Entrypoint": ["/docker-entrypoint.sh"],"OnBuild": null,"Labels": {"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"},"StopSignal": "SIGQUIT"},"NetworkSettings": {"Bridge": "","SandboxID": "c899cd58221bfcf96bae6acf312ed706db7981baa37d959cc888be5b1f0fd286","SandboxKey": "/var/run/docker/netns/c899cd58221b","Ports": {"80/tcp": null},"HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "1ca8c5c9cd73cf62858823ff5b724ac0115336833f28158a6072a1b31ead3187","Gateway": "172.17.0.1","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","MacAddress": "b6:fe:a1:9a:3e:a6","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"MacAddress": "b6:fe:a1:9a:3e:a6","DriverOpts": null,"GwPriority": 0,"NetworkID": "2dba964368f785973cac101f2112a37a034ddf1667583203846b25e06839cac9","EndpointID": "1ca8c5c9cd73cf62858823ff5b724ac0115336833f28158a6072a1b31ead3187","Gateway": "172.17.0.1","IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"DNSNames": null}}}}
]
-v 选项挂载
[root@host1 ~]# docker run -d --name nginx-autovol -v myvol:/app nginx
e0e06ebbe8acd076c67b79aa0718680ca980f17ead02aa4b40951cd6cb01bca3
[root@host1 ~]# docker stop nginx-autovol
nginx-autovol
[root@host1 ~]# docker rm nginx-autovol
nginx-autovol
3.使用容器填充卷
1.启动一个运行 nginx 的容器,并使用容器的 /usr/share/nginx/html 目录的内容填充新卷 nginx-vol
[root@host1 ~]# docker run -d --name=nginxtest --mount source=nginx-vol,destination=/usr/share/nginx/html nginx
e1a10e82d3c28055195c9297750a5a5087cecb17883c8593e6469edfeb5877d3
2.查看该卷的详细信息
[root@host1 ~]# docker volume inspect nginx-vol
[{"CreatedAt": "2025-09-12T14:07:50+08:00","Driver": "local","Labels": null,"Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data","Name": "nginx-vol","Options": null,"Scope": "local"}
]
3.查看主机上该卷所在目录的内容
[root@host1 ~]# ls /var/lib/docker/volumes/nginx-vol/_data
50x.html index.html
4.基于 Ubuntu 镜像启动另一个容器挂载该卷,以使用其中预先填充的内容
[root@host1 ~]# docker run -it --name=ubuntutest --mount source=nginx-vol,destination=/nginx ubuntu /bin/bash
root@9e9fa5374d11:/# ls /nginx
50x.html index.html
root@9e9fa5374d11:/# exit
exit
5.删除容器和卷
[root@host1 ~]# docker container stop nginxtest ubuntutest
nginxtest
ubuntutest
[root@host1 ~]# docker container rm nginxtest ubuntutest
nginxtest
ubuntutest
[root@host1 ~]# docker volume rm nginx-vol
nginx-vol
4.使用只读卷
[root@host1 ~]# docker run -d --name=nginxtest --mount source=nginx-vol,destination=/usr/share/nginx/html,readonly nginx
9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca
[root@host1 ~]# docker inspect nginxtest
[{"Id": "9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca","Created": "2025-09-12T06:22:29.990534978Z","Path": "/docker-entrypoint.sh","Args": ["nginx","-g","daemon off;"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 403636,"ExitCode": 0,"Error": "","StartedAt": "2025-09-12T06:22:30.008735096Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:41f689c209100e6cadf3ce7fdd02035e90dbd1d586716bf8fc6ea55c365b2d81","ResolvConfPath": "/var/lib/docker/containers/9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca/resolv.conf","HostnamePath": "/var/lib/docker/containers/9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca/hostname","HostsPath": "/var/lib/docker/containers/9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca/hosts","LogPath": "/var/lib/docker/containers/9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca/9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca-json.log","Name": "/nginxtest","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "bridge","PortBindings": {},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"ConsoleSize": [36,101],"CapAdd": null,"CapDrop": null,"CgroupnsMode": "private","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","Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": [],"BlkioDeviceReadBps": [],"BlkioDeviceWriteBps": [],"BlkioDeviceReadIOps": [],"BlkioDeviceWriteIOps": [],"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": [],"DeviceCgroupRules": null,"DeviceRequests": null,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": null,"PidsLimit": null,"Ulimits": [],"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"Mounts": [{"Type": "volume","Source": "nginx-vol","Target": "/usr/share/nginx/html","ReadOnly": true}],"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware","/sys/devices/virtual/powercap"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"ID": "9a2198c3c7aaa3e50ed0cca31062d0b8eea7c376112eb7d4b447a6c08fea15ca","LowerDir": "/var/lib/docker/overlay2/d15973fc0f00ca792bd6be2002a70f0f81a5d8052b96178a3d2e39f9e026a164-init/diff:/var/lib/docker/overlay2/8b57d2f007b17a853b402d7981962ae0217011bd75f8f4aa6ec1288aebb0dc7e/diff:/var/lib/docker/overlay2/7c5719df2bf53f07bea95c0dab086a15fc9c3b85bea2225098d49d78a8a766e8/diff:/var/lib/docker/overlay2/5a31009658ea6524de00dca63c21fc65f89a0e88be6264074c0ea65be9bfd24c/diff:/var/lib/docker/overlay2/6655e09d62aa340b5368fe6a07108907f813224356ee4cfa7a8cfa802ec87e66/diff:/var/lib/docker/overlay2/23c934857988d94756762e22771276121b35cab43c4549582e8d7d54d2c7ecf1/diff:/var/lib/docker/overlay2/7959c6d39ca832474bf8aba5077a8d407f7f4d087a8c8480d8c282e0deae1ad1/diff:/var/lib/docker/overlay2/ca8efaabeb43dce901c82c3111b6f5d376ee91641aa5c3268760982d8552d4d1/diff","MergedDir": "/var/lib/docker/overlay2/d15973fc0f00ca792bd6be2002a70f0f81a5d8052b96178a3d2e39f9e026a164/merged","UpperDir": "/var/lib/docker/overlay2/d15973fc0f00ca792bd6be2002a70f0f81a5d8052b96178a3d2e39f9e026a164/diff","WorkDir": "/var/lib/docker/overlay2/d15973fc0f00ca792bd6be2002a70f0f81a5d8052b96178a3d2e39f9e026a164/work"},"Name": "overlay2"},"Mounts": [{"Type": "volume","Name": "nginx-vol","Source": "/var/lib/docker/volumes/nginx-vol/_data","Destination": "/usr/share/nginx/html","Driver": "local","Mode": "z","RW": false,"Propagation": ""}],"Config": {"Hostname": "9a2198c3c7aa","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"ExposedPorts": {"80/tcp": {}},"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NGINX_VERSION=1.29.1","NJS_VERSION=0.9.1","NJS_RELEASE=1~bookworm","PKG_RELEASE=1~bookworm","DYNPKG_RELEASE=1~bookworm"],"Cmd": ["nginx","-g","daemon off;"],"Image": "nginx","Volumes": null,"WorkingDir": "","Entrypoint": ["/docker-entrypoint.sh"],"OnBuild": null,"Labels": {"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"},"StopSignal": "SIGQUIT"},"NetworkSettings": {"Bridge": "","SandboxID": "f44c7703497a797aea09bc53c4a8ed98b9b255309b5e8a322c7ba31e435e3f81","SandboxKey": "/var/run/docker/netns/f44c7703497a","Ports": {"80/tcp": null},"HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "d10aaa48ef33ede432de0f9bcbf51cda454b2334f768939700e102e97aca7879","Gateway": "172.17.0.1","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","MacAddress": "1a:71:69:34:52:c0","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"MacAddress": "1a:71:69:34:52:c0","DriverOpts": null,"GwPriority": 0,"NetworkID": "2dba964368f785973cac101f2112a37a034ddf1667583203846b25e06839cac9","EndpointID": "d10aaa48ef33ede432de0f9bcbf51cda454b2334f768939700e102e97aca7879","Gateway": "172.17.0.1","IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"DNSNames": null}}}}
]
[root@host1 ~]# docker container stop nginxtest
nginxtest
[root@host1 ~]# docker container rm nginxtest
nginxtest
[root@host1 ~]# docker volume rm nginx-vol
nginx-vol
[root@host1 ~]# docker run -d --name=nginxtest -v nginx-vol:/usr/share/nginx/html:ro nginx
827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b
[root@host1 ~]# docker inspect nginxtest
[{"Id": "827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b","Created": "2025-09-12T06:25:36.24549778Z","Path": "/docker-entrypoint.sh","Args": ["nginx","-g","daemon off;"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 430109,"ExitCode": 0,"Error": "","StartedAt": "2025-09-12T06:25:36.259842343Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:41f689c209100e6cadf3ce7fdd02035e90dbd1d586716bf8fc6ea55c365b2d81","ResolvConfPath": "/var/lib/docker/containers/827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b/resolv.conf","HostnamePath": "/var/lib/docker/containers/827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b/hostname","HostsPath": "/var/lib/docker/containers/827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b/hosts","LogPath": "/var/lib/docker/containers/827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b/827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b-json.log","Name": "/nginxtest","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": ["nginx-vol:/usr/share/nginx/html:ro"],"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "bridge","PortBindings": {},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"ConsoleSize": [36,101],"CapAdd": null,"CapDrop": null,"CgroupnsMode": "private","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","Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": [],"BlkioDeviceReadBps": [],"BlkioDeviceWriteBps": [],"BlkioDeviceReadIOps": [],"BlkioDeviceWriteIOps": [],"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": [],"DeviceCgroupRules": null,"DeviceRequests": null,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": null,"PidsLimit": null,"Ulimits": [],"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware","/sys/devices/virtual/powercap"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"ID": "827c938aa558c29363c78458fd79bf93ed29f499dbd28facb6937a685782f12b","LowerDir": "/var/lib/docker/overlay2/75cd9c1bdce67c6b3ed1f1c0b9193164257e22caad95e0947a05948fefa802b1-init/diff:/var/lib/docker/overlay2/8b57d2f007b17a853b402d7981962ae0217011bd75f8f4aa6ec1288aebb0dc7e/diff:/var/lib/docker/overlay2/7c5719df2bf53f07bea95c0dab086a15fc9c3b85bea2225098d49d78a8a766e8/diff:/var/lib/docker/overlay2/5a31009658ea6524de00dca63c21fc65f89a0e88be6264074c0ea65be9bfd24c/diff:/var/lib/docker/overlay2/6655e09d62aa340b5368fe6a07108907f813224356ee4cfa7a8cfa802ec87e66/diff:/var/lib/docker/overlay2/23c934857988d94756762e22771276121b35cab43c4549582e8d7d54d2c7ecf1/diff:/var/lib/docker/overlay2/7959c6d39ca832474bf8aba5077a8d407f7f4d087a8c8480d8c282e0deae1ad1/diff:/var/lib/docker/overlay2/ca8efaabeb43dce901c82c3111b6f5d376ee91641aa5c3268760982d8552d4d1/diff","MergedDir": "/var/lib/docker/overlay2/75cd9c1bdce67c6b3ed1f1c0b9193164257e22caad95e0947a05948fefa802b1/merged","UpperDir": "/var/lib/docker/overlay2/75cd9c1bdce67c6b3ed1f1c0b9193164257e22caad95e0947a05948fefa802b1/diff","WorkDir": "/var/lib/docker/overlay2/75cd9c1bdce67c6b3ed1f1c0b9193164257e22caad95e0947a05948fefa802b1/work"},"Name": "overlay2"},"Mounts": [{"Type": "volume","Name": "nginx-vol","Source": "/var/lib/docker/volumes/nginx-vol/_data","Destination": "/usr/share/nginx/html","Driver": "local","Mode": "ro","RW": false,"Propagation": ""}],"Config": {"Hostname": "827c938aa558","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"ExposedPorts": {"80/tcp": {}},"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","NGINX_VERSION=1.29.1","NJS_VERSION=0.9.1","NJS_RELEASE=1~bookworm","PKG_RELEASE=1~bookworm","DYNPKG_RELEASE=1~bookworm"],"Cmd": ["nginx","-g","daemon off;"],"Image": "nginx","Volumes": null,"WorkingDir": "","Entrypoint": ["/docker-entrypoint.sh"],"OnBuild": null,"Labels": {"maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"},"StopSignal": "SIGQUIT"},"NetworkSettings": {"Bridge": "","SandboxID": "4378e5cff9f79e6e787a25ea0b1e0001c678d58f1b0a13e62315445db14aee61","SandboxKey": "/var/run/docker/netns/4378e5cff9f7","Ports": {"80/tcp": null},"HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "2589496aa5d11b670387c15ef23696756af8d1b45eb6ef9e8ac47a23b2e36ff4","Gateway": "172.17.0.1","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","MacAddress": "f2:e5:bb:e2:af:9e","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"MacAddress": "f2:e5:bb:e2:af:9e","DriverOpts": null,"GwPriority": 0,"NetworkID": "2dba964368f785973cac101f2112a37a034ddf1667583203846b25e06839cac9","EndpointID": "2589496aa5d11b670387c15ef23696756af8d1b45eb6ef9e8ac47a23b2e36ff4","Gateway": "172.17.0.1","IPAddress": "172.17.0.3","IPPrefixLen": 16,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"DNSNames": null}}}}
]
[root@host1 ~]# docker container stop nginxtest
nginxtest
[root@host1 ~]# docker container rm nginxtest
nginxtest
[root@host1 ~]# docker volume rm nginx-vol
nginx-vol
5.使用匿名卷
[root@host1 ~]# docker run -it --name ubuntutest --mount destination=/world ubuntu /bin/bash
root@98bedbd6f7a6:/# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr world
root@98bedbd6f7a6:/# exit
exit
[root@host1 ~]# docker inspect ubuntutest
[{"Id": "98bedbd6f7a6d73904f25a3f86343db37e00634d742e2d1988ce66c7b64db811","Created": "2025-09-12T06:28:05.399086199Z","Path": "/bin/bash","Args": [],"State": {"Status": "exited","Running": false,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 0,"ExitCode": 0,"Error": "","StartedAt": "2025-09-12T06:28:05.415091551Z","FinishedAt": "2025-09-12T06:28:22.957142359Z"},"Image": "sha256:802541663949fbd5bbd8f35045af10005f51885164e798e2ee8d1dc39ed8888d","ResolvConfPath": "/var/lib/docker/containers/98bedbd6f7a6d73904f25a3f86343db37e00634d742e2d1988ce66c7b64db811/resolv.conf","HostnamePath": "/var/lib/docker/containers/98bedbd6f7a6d73904f25a3f86343db37e00634d742e2d1988ce66c7b64db811/hostname","HostsPath": "/var/lib/docker/containers/98bedbd6f7a6d73904f25a3f86343db37e00634d742e2d1988ce66c7b64db811/hosts","LogPath": "/var/lib/docker/containers/98bedbd6f7a6d73904f25a3f86343db37e00634d742e2d1988ce66c7b64db811/98bedbd6f7a6d73904f25a3f86343db37e00634d742e2d1988ce66c7b64db811-json.log","Name": "/ubuntutest","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "bridge","PortBindings": {},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"ConsoleSize": [36,101],"CapAdd": null,"CapDrop": null,"CgroupnsMode": "private","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","Isolation": "","CpuShares": 0,"Memory": 0,"NanoCpus": 0,"CgroupParent": "","BlkioWeight": 0,"BlkioWeightDevice": [],"BlkioDeviceReadBps": [],"BlkioDeviceWriteBps": [],"BlkioDeviceReadIOps": [],"BlkioDeviceWriteIOps": [],"CpuPeriod": 0,"CpuQuota": 0,"CpuRealtimePeriod": 0,"CpuRealtimeRuntime": 0,"CpusetCpus": "","CpusetMems": "","Devices": [],"DeviceCgroupRules": null,"DeviceRequests": null,"MemoryReservation": 0,"MemorySwap": 0,"MemorySwappiness": null,"OomKillDisable": null,"PidsLimit": null,"Ulimits": [],"CpuCount": 0,"CpuPercent": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"Mounts": [{"Type": "volume","Target": "/world"}],"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware","/sys/devices/virtual/powercap"],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"GraphDriver": {"Data": {"ID": "98bedbd6f7a6d73904f25a3f86343db37e00634d742e2d1988ce66c7b64db811","LowerDir": "/var/lib/docker/overlay2/03b5819f92ede0b41d33b3f536a035820d9593fac774dd65bac3930d7fc0578a-init/diff:/var/lib/docker/overlay2/e721707f22e5c14f3e0591ab1c2c6cb336f864c6033c36cbd4834cdb84113b6b/diff","MergedDir": "/var/lib/docker/overlay2/03b5819f92ede0b41d33b3f536a035820d9593fac774dd65bac3930d7fc0578a/merged","UpperDir": "/var/lib/docker/overlay2/03b5819f92ede0b41d33b3f536a035820d9593fac774dd65bac3930d7fc0578a/diff","WorkDir": "/var/lib/docker/overlay2/03b5819f92ede0b41d33b3f536a035820d9593fac774dd65bac3930d7fc0578a/work"},"Name": "overlay2"},"Mounts": [{"Type": "volume","Name": "ef136ecbd418cca8eed5c3910531c587520788be718cf480d33490035acd3240","Source": "/var/lib/docker/volumes/ef136ecbd418cca8eed5c3910531c587520788be718cf480d33490035acd3240/_data","Destination": "/world","Driver": "local","Mode": "z","RW": true,"Propagation": ""}],"Config": {"Hostname": "98bedbd6f7a6","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": "ubuntu","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {"org.opencontainers.image.ref.name": "ubuntu","org.opencontainers.image.version": "24.04"}},"NetworkSettings": {"Bridge": "","SandboxID": "","SandboxKey": "","Ports": {},"HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "","Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "","IPPrefixLen": 0,"IPv6Gateway": "","MacAddress": "","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"MacAddress": "","DriverOpts": null,"GwPriority": 0,"NetworkID": "2dba964368f785973cac101f2112a37a034ddf1667583203846b25e06839cac9","EndpointID": "","Gateway": "","IPAddress": "","IPPrefixLen": 0,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"DNSNames": null}}}}
]
[root@host1 ~]# docker volume ls
DRIVER VOLUME NAME
local ef136ecbd418cca8eed5c3910531c587520788be718cf480d33490035acd3240
local myvol
[root@host1 ~]# docker stop 98bedbd6f7a6d
98bedbd6f7a6d
[root@host1 ~]# docker rm 98bedbd6f7a6d
98bedbd6f7a6d
[root@host1 ~]# docker volume rm ef136ecbd418cca8eed5c3910531c587520788be718cf480d33490035acd3240
ef136ecbd418cca8eed5c3910531c587520788be718cf480d33490035acd3240
[root@host1 ~]# docker volume ls
DRIVER VOLUME NAME
local myvol