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

相关推荐
钱多多_qdd6 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
waicsdn_haha8 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
Q_192849990618 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏21 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
蜜獾云34 分钟前
docker 安装雷池WAF防火墙 守护Web服务器
linux·运维·服务器·网络·网络安全·docker·容器
禁默1 小时前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑1 小时前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb42152871 小时前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶1 小时前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot