Docker磁盘空间分析与清理

首先使用du命令逐层分析docker磁盘的使用情况:

(base) [root@openvino docker]# du -h --max-depth=1 | sort
0    ./containerd
0    ./libnetwork
0    ./netns
0    ./plugins
0    ./swarm
32K    .
32K    ./runtime-runc

使用 docker system df 命令查询镜像(Images)、容器(Containers)和本地卷(Local Volumes)等空间使用大户的空间占用情况。 示例输出如下:

[root@localhost docker]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          5         2         1.066GB   577.7MB (54%)
Containers      2         2         129.5MB   0B (0%)
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B

进一步,通过 -v参数查看空间占用细节:

[root@localhost docker]# docker system df -v
Images space usage:

REPOSITORY                TAG       IMAGE ID       CREATED         SIZE      SHARED SIZE   UNIQUE SIZE   CONTAINERS
redis                     latest    ddcca4b8a6f0   5 months ago    105.4MB   69.26MB       36.15MB       1
nginx                     latest    dd34e67e3371   5 months ago    133.2MB   69.26MB       63.92MB       0
mysql                     latest    5a4e492065c7   5 months ago    513.8MB   69.26MB       444.6MB       0
hello-world               latest    d1165f221234   10 months ago   13.34kB   0B            13.34kB       0
centos/mysql-57-centos7   latest    f83a2938370c   2 years ago     452.1MB   0B            452.1MB       1

Containers space usage:

CONTAINER ID   IMAGE                     COMMAND                  LOCAL VOLUMES   SIZE      CREATED        STATUS       NAMES
d4e15edeb4a1   redis                     "docker-entrypoint.s…"   0               0B        4 months ago   Up 5 hours   redis
942ff88698b2   centos/mysql-57-centos7   "container-entrypoin…"   0               129MB     4 months ago   Up 5 hours   mysql

Local Volumes space usage:

VOLUME NAME   LINKS     SIZE

Build cache usage: 0B

CACHE ID   CACHE TYPE   SIZE      CREATED   LAST USED   USAGE     SHARED

空间清理

可以通过 Docker 内置的 CLI 指令 docker system prune来进行自动空间清理。

不同状态的镜像:

  • 已使用镜像(used image): 指所有已被容器(包括已停止的)关联的镜像。即 docker ps -a 看到的所有容器使用的镜像。
[root@localhost docker]# docker ps -a
CONTAINER ID   IMAGE                     COMMAND                  CREATED        STATUS       PORTS                                         NAMES
d4e15edeb4a1   redis                     "docker-entrypoint.s…"   4 months ago   Up 5 hours   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp     redis
942ff88698b2   centos/mysql-57-centos7   "container-entrypoin…"   4 months ago   Up 5 hours   0.0.0.0:33306->3306/tcp, :::33306->3306/tcp   mysql
  • 未引用镜像(unreferenced image):没有被分配或使用在容器中的镜像,但它有 Tag 信息。
  • 悬空镜像(dangling image):未配置任何 Tag (也就无法被引用)的镜像,所以悬空。这通常是由于镜像 build 的时候没有指定 -t 参数配置 Tag 导致的。

docker system prune 自动清理说明:

  • 已停止的容器(container);
  • 未被任何容器所使用的卷(volume);
  • 未被任何容器所关联的网络(network);
  • 所有悬空镜像(image);
  • 添加 -a或 --all可以一并清除为引用的镜像以及悬空镜像
  • 添加-f或--force参数可以忽略警告信息

手动删除镜像

docker rmi [image]
或
docker image rm [image]
  • 可以通过 镜像名或ID 删除;
  • 子命令 -f ,--force:强制删除,即便该镜像已被引用;
  • 子命令-no-prune:不要删除未带标签的父镜像。

 

posted @ 2022-01-19 10:06  干了这瓶老干妈  阅读(219)  评论(0编辑  收藏  举报
Live2D