停止的 Docker 容器占用的内存和其他资源

news/2024/11/14 0:38:45/

要查看 Docker 停止的容器占用的内存,可以通过以下几种方法来实现。Docker 提供了一些命令和工具来帮助你管理和监控容器的资源使用情况。

docker_system_df__2">方法一:使用 docker system df 命令

docker system df 命令可以显示 Docker 使用的磁盘空间,包括镜像、容器和数据卷的占用情况。

  1. 运行命令

    docker system df
    
  2. 输出示例

    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 命令可以查看容器的详细信息,包括其状态和资源使用情况。

  1. 获取所有停止的容器的 ID

    docker ps -a -q --filter status=exited
    
  2. 查看某个停止的容器的详细信息

    docker inspect <container_id>
    
  3. 解析输出
    在输出的 JSON 中,查找 SizeRwSizeRootFs 字段,这些字段分别表示容器的可写层大小和根文件系统的总大小。

docker_system_prune__37">方法三:使用 docker system prune 命令

虽然 docker system prune 命令主要用于清理未使用的资源,但它也可以帮助你了解哪些资源可以被回收。

  1. 运行命令

    docker system prune
    
  2. 输出示例

    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 目录的磁盘使用情况。

  1. 查找 Docker 存储目录
    默认情况下,Docker 存储目录位于 /var/lib/docker

  2. 运行 du 命令

    sudo du -h --max-depth=1 /var/lib/docker
    
  3. 输出示例

    4.0K    /var/lib/docker/containers
    1.0G    /var/lib/docker/image
    500M    /var/lib/docker/volumes
    5.0G    /var/lib/docker
    

综合示例

假设你有一个停止的容器 ID 为 abc123,你可以按照以下步骤查看其占用的内存:

  1. 获取容器的详细信息

    docker inspect abc123
    
  2. 解析输出
    在输出的 JSON 中,查找 SizeRwSizeRootFs 字段:

    {"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 容器占用的内存和其他资源。


http://www.ppmy.cn/news/1546263.html

相关文章

ArcGIS/QGIS按掩膜提取或栅格裁剪后栅格数据的值为什么变了?

问题描述&#xff1a; 现有一栅格数据&#xff0c;使用ArcGIS或者QGIS按照矢量边界进行按掩膜提取或者栅格裁剪以后&#xff0c;其值的范围发生了变化&#xff0c;如下&#xff1a; 可以看到&#xff0c;不论是按掩膜提取还是进行栅格裁剪后&#xff0c;其值的范围均与原来栅…

【日常问题排查小技巧-连载】

线上服务CPU飙高排查 先执行 top&#xff0c;找到CPU占用比较高的进程 id&#xff0c;&#xff08;比如 21448&#xff09; jstack 进程 id > show.txt&#xff08;jstack 21448 > show.txt&#xff09; 找到进程中CPU占用比较高的线程&#xff0c;线程 id 转换为 16 进…

Java反射原理及其性能优化

目录 JVM是如何实现反射的反射的性能开销体现在哪里如何优化反射性能开销 1. JVM是如何实现反射的? 反射是Java语言中的一种强大功能&#xff0c;它允许程序在运行时动态地获取类的信息以及操作对象。下面是一个简单的示例&#xff0c;演示了如何使用反射调用方法&#xff…

go template 模板字符串

普通方式 当我们需要在字符串中拼接参数时可以通过fmt.Sprintf() 方法实现 func main() {var header fmt.Sprintf("# &#x1f4ca; 触达挤压统计 &#x1f4c5;&#xff08;%d月%d日&#xff09;\n<font color\"comment\">&#xff08;1小时最多发送4…

FreeRTOS 23:事件组EventGroup创建、删除、置位操作

FreeRTOS 提供了事件标志组的一些相关操作函数&#xff0c;如下表所示&#xff1a; 创建一个事件组 xEventGroupCreate() 动态方式创建事件标志组 xEventGroupCreate()用于创建一个事件组&#xff0c;并返回对应的句柄。 要想使用该函数必须 在头文件 FreeRTOSConfig.h 定义宏…

19、centos7优化

优化条目&#xff1a; 优化条目&#xff1a; 1.sudo管理用户授权 &#xff08;不用root管理,以普通用户的名义通过sudo提权&#xff09; 2.更改默认的远程连接SSH服务端口,禁止root用户远程连接,&#xff08;提前建立普通用户&#xff09;&#xff08;甚至更改为只监听内网IP…

[High Speed Serial ] Xilinx

Xilinx 高速串行数据接口 收发器产品涵盖了当今高速协议的方方面面。GTH 和 GTY 收发器提供要求苛刻的光互连所需的低抖动&#xff0c;并具有世界一流的自适应均衡功能&#xff0c;具有困难的背板操作所需的 PCS 功能。 Versal™ GTY &#xff08;32.75Gb/s&#xff09;&…

1小时搞定魔乐平台AI应用的构建和离线复现

想象一下&#xff0c;只需一小时&#xff0c;你就能在魔乐平台上构建自己的AI应用&#xff0c;并在本地环境中完美复现。听起来是不是很诱人&#xff1f;今天&#xff0c;我们就来手把手教你如何快速上手魔乐社区的space服务&#xff0c;让你的AI创意变为现实。 一 前言&#x…