要查看 Docker 停止的容器占用的内存,可以通过以下几种方法来实现。Docker 提供了一些命令和工具来帮助你管理和监控容器的资源使用情况。
docker_system_df__2">方法一:使用 docker system df
命令
docker system df
命令可以显示 Docker 使用的磁盘空间,包括镜像、容器和数据卷的占用情况。
-
运行命令:
docker system df
-
输出示例:
TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 10 5 5.00GB 2.50GB (50%) Containers 20 10 1.00GB 500MB (50%) Local Volumes 5 3 500MB 200MB (40%) Build Cache 0 0 0B 0B
docker_inspect__20">方法二:使用 docker inspect
命令
docker inspect
命令可以查看容器的详细信息,包括其状态和资源使用情况。
-
获取所有停止的容器的 ID:
docker ps -a -q --filter status=exited
-
查看某个停止的容器的详细信息:
docker inspect <container_id>
-
解析输出:
在输出的 JSON 中,查找SizeRw
和SizeRootFs
字段,这些字段分别表示容器的可写层大小和根文件系统的总大小。
docker_system_prune__37">方法三:使用 docker system prune
命令
虽然 docker system prune
命令主要用于清理未使用的资源,但它也可以帮助你了解哪些资源可以被回收。
-
运行命令:
docker system prune
-
输出示例:
WARNING! This will remove:- all stopped containers- all networks not used by at least one container- all dangling images- all build cache Are you sure you want to continue? [y/N] y Total reclaimed space: 2.50GB
方法四:使用 du
命令
你也可以直接在宿主机上使用 du
命令来查看 Docker 目录的磁盘使用情况。
-
查找 Docker 存储目录:
默认情况下,Docker 存储目录位于/var/lib/docker
。 -
运行
du
命令:sudo du -h --max-depth=1 /var/lib/docker
-
输出示例:
4.0K /var/lib/docker/containers 1.0G /var/lib/docker/image 500M /var/lib/docker/volumes 5.0G /var/lib/docker
综合示例
假设你有一个停止的容器 ID 为 abc123
,你可以按照以下步骤查看其占用的内存:
-
获取容器的详细信息:
docker inspect abc123
-
解析输出:
在输出的 JSON 中,查找SizeRw
和SizeRootFs
字段:{"Id": "abc123","Created": "2023-10-01T12:34:56.789Z","Path": "/bin/bash","Args": [],"State": {"Status": "exited","Running": false,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 0,"ExitCode": 0,"Error": "","StartedAt": "2023-10-01T12:34:56.789Z","FinishedAt": "2023-10-01T12:35:00.789Z"},"Image": "sha256:1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef","ResolvConfPath": "/var/lib/docker/containers/abc123/resolv.conf","HostnamePath": "/var/lib/docker/containers/abc123/hostname","HostsPath": "/var/lib/docker/containers/abc123/hosts","LogPath": "/var/lib/docker/containers/abc123/abc123-json.log","Name": "/my_container","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,"CpuUsage": 0,"IOMaximumIOps": 0,"IOMaximumBandwidth": 0,"MaskedPaths": ["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","..."],"ReadonlyPaths": ["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger","..."]},"GraphDriver": {"Data": {"LowerDir": "/var/lib/docker/overlay2/fc1234567890abcdef1234567890abcdef/diff","MergedDir": "/var/lib/docker/overlay2/abc123/merged","UpperDir": "/var/lib/docker/overlay2/abc123/diff","WorkDir": "/var/lib/docker/overlay2/abc123/work"},"Name": "overlay2"},"Mounts": [],"Config": {"Hostname": "abc123","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": "ubuntu:latest","Volumes": null,"WorkingDir": "/","Entrypoint": null,"OnBuild": null,"Labels": {}},"NetworkSettings": {"Bridge": "","SandboxID": "1234567890abcdef1234567890abcdef","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {},"SandboxKey": "/var/run/docker/netns/1234567890ab","SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "","Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress": "","IPPrefixLen": 0,"IPv6Gateway": "","MacAddress": "","Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"NetworkID": "1234567890abcdef1234567890abcdef","EndpointID": "","Gateway": "","IPAddress": "","IPPrefixLen": 0,"IPv6Gateway": "","GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress": "","DriverOpts": null}}},"SizeRw": 10485760, # 可写层大小(10 MB)"SizeRootFs": 209715200 # 根文件系统总大小(200 MB) }
总结
通过上述方法,你可以详细了解停止的 Docker 容器占用的内存和其他资源。