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

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

方法一:使用 docker system df 命令

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

  1. 运行命令

    sh 复制代码
    docker system df
  2. 输出示例

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

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

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

    sh 复制代码
    docker inspect <container_id>
  3. 解析输出

    在输出的 JSON 中,查找 SizeRwSizeRootFs 字段,这些字段分别表示容器的可写层大小和根文件系统的总大小。

方法三:使用 docker system prune 命令

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

  1. 运行命令

    sh 复制代码
    docker system prune
  2. 输出示例

    sh 复制代码
    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 命令

    sh 复制代码
    sudo du -h --max-depth=1 /var/lib/docker
  3. 输出示例

    sh 复制代码
    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. 获取容器的详细信息

    sh 复制代码
    docker inspect abc123
  2. 解析输出

    在输出的 JSON 中,查找 SizeRwSizeRootFs 字段:

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

相关推荐
架构悟道11 分钟前
不当愣头青、聊聊软件架构中的那些惯用的保命手段
java·分布式·架构·设计·高可用·可靠性·容错
蓝田~13 分钟前
Springboot -- 自定义异常,异常处理
java·spring boot·spring
Leslie_Lei16 分钟前
Hutool-Java工具库
java·hutool
斗-匕30 分钟前
Spring & Spring Boot 常用注解总结
java·spring boot·spring
夏微凉.32 分钟前
【JavaEE进阶】Spring 事务和事务传播机制
java·数据库·sql·mysql·spring·java-ee
吴冰_hogan37 分钟前
nacos集群源码解析-cp架构
java·spring boot·spring·架构·服务发现·springcloud
阿七想学习38 分钟前
数据结构《链表》
java·开发语言·数据结构·学习·链表
Yaml438 分钟前
Java的六大排序
java·算法·排序算法
XiaoLiuLB41 分钟前
Tomcat NIO 配置实操指南
java·tomcat·nio
Be_Somebody44 分钟前
[这可能是最好的Spring教程!]Maven的模块管理——如何拆分大项目并且用parent继承保证代码的简介性
java·spring boot·spring·spring入门