方法一:使用 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 命令
docker inspect
命令可以查看容器的详细信息,包括其状态和资源使用情况。
获取所有停止的容器的 id:
docker ps -a -q --filter status=exited
查看某个停止的容器的详细信息:
docker inspect <container_id>
解析输出:
在输出的 json 中,查找 sizerw
和 sizerootfs
字段,这些字段分别表示容器的可写层大小和根文件系统的总大小。
方法三:使用 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 容器占用的内存和其他资源。
到此这篇关于查看docker停止容器占用的内存的实现方法小结的文章就介绍到这了,更多相关查看docker停止容器占用内存内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论