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

相关推荐
AiFlutter24 分钟前
Java实现简单的搜索引擎
java·搜索引擎·mybatis
意疏31 分钟前
【Linux 篇】Docker 的容器之海与镜像之岛:于 Linux 系统内探索容器化的奇妙航行
linux·docker
墨鸦_Cormorant37 分钟前
使用docker快速部署Nginx、Redis、MySQL、Tomcat以及制作镜像
redis·nginx·docker
飞升不如收破烂~44 分钟前
Spring boot常用注解和作用
java·spring boot·后端
秦老师Q1 小时前
Java基础第九章-Java集合框架(超详细)!!!
java·开发语言
计算机毕设源码qq-38365310411 小时前
(附项目源码)Java开发语言,215 springboot 大学生爱心互助代购网站,计算机毕设程序开发+文案(LW+PPT)
java·开发语言·spring boot·mysql·课程设计
ashane13141 小时前
Java list
java·windows·list
袁庭新1 小时前
Cannal实现MySQL主从同步环境搭建
java·数据库·mysql·计算机·java程序员·袁庭新
无尽的大道1 小时前
深入理解 Java 阻塞队列:使用场景、原理与性能优化
java·开发语言·性能优化
岁岁岁平安1 小时前
springboot实战(15)(注解@JsonFormat(pattern=“?“)、@JsonIgnore)
java·spring boot·后端·idea