Docker overlay 空间清理

Eric 2022年04月23日 2,250次浏览

镜像和容器清理

查看磁盘使用情况

$ du -hs /var/lib/docker/ 

查看Docker的磁盘使用情况

$ docker system df

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          17        3         4.37GB    3.722GB (85%)
Containers      3         3         133.2MB   0B (0%)
Local Volumes   13        1         1.198GB   1.198GB (100%)
Build Cache     0         0         0B        0B

清理磁盘

docker system prune命令可以用于清理磁盘,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)

$ docker system prune

WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Deleted Containers:
1a62ffa2d897f1a398c85685f798ff6be1eac09217b46c552b5f5b4d05a68e51
4afb97c516ecc570caf22b5b262bb193cbb9721df2fdfbc789ad77409b8edf2e
5f963b47468a4903dc55db76323ba198a257eec93fbeb50d9aa6b6f77fa135cf

Deleted Networks:
manager_default
dx_default
lims_default

Total reclaimed space: 47.26MB

Remark docker system prune -a 命令清理得更加彻底,可以将没有容器使用Docker镜像都删掉。注意:这两个命令会把你暂时关闭的容器,以及暂时没有用到的Docker镜像都删掉!所以使用之前一定要想清楚。

磁盘清理

  1. 查看磁盘空间使用量
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           784M  7.8M  776M   1% /run
/dev/vda1        79G   15G   61G  20% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           784M     0  784M   0% /run/user/0
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/92264ba6b581653fa306c9909fc8ecb2800576aa01dc40bab36a185d3cabd082/merged
shm              64M     0   64M   0% /var/lib/docker/containers/0ce8ccdb4834848c211df33a648dcccd7283564bd4e1a4616e956e92820017bf/mounts/shm
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/418295c6197292dd1756151a7165cc03be83fa7ccb3e04c9af04bfd82abf04c8/merged
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/4062462bc704344010d865c09b726ecf53d5cda5bb9e760f5f03f0118b8fd3c8/merged
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/be04a7a7c69800d704013e7de8a83c339ea8961c55e8f879f2abd049dc2f6b5d/merged
shm              64M     0   64M   0% /var/lib/docker/containers/e79e5e79e702988eb1f5ae61800820106d41f64bc03bcbbab85aed0eaafc4d6f/mounts/shm
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/b8ee84828ac3167adcc12d48e295305024b855d9da6901531be645f643742dc3/merged
shm              64M     0   64M   0% /var/lib/docker/containers/29e0ae9c1cbdad68395f14c8312dc51d8b0bab5071b70d9958086da92cbd6c8c/mounts/shm
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/3fe5ca069d2836901b14f83edd37c48916b554f66345d71e788a8204f7f2160f/merged
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/73c027e2aa9fa51005e8a4d773522462a7374c7ac91fe86ffad9e9b7d92bcf68/merged
shm              64M     0   64M   0% /var/lib/docker/containers/a9940199d41926af6bc86b03e7209b21e0f8f9fd836e05daa553b21632c59202/mounts/shm
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/ca354e03b2ac84712062104f0e3043de194bc412b70b9cc9164ac69789dda9e8/merged
shm              64M     0   64M   0% /var/lib/docker/containers/27f2779e50d58b0a155450494e4fa7a144312342b1d49f34dbc5e70f96aade07/mounts/shm
overlay          79G   15G   61G  20% /var/lib/docker/overlay2/76772fc1722aa8c0964e7b6c69dfba90297829a9e420df10fa832a1a4439e391/merged
shm              64M     0   64M   0% /var/lib/docker/containers/6e2ae0cdd0f74712580a0e9d0d6744f4365b3bf01a1172bd5835e97e37e02538/mounts/shm
  1. 进入占用多的目录,找出占用空间最大的文件目录
$ cd /var/lib/docker

$ du -h --max-depth=1
24K     ./plugins
70G     ./containers
104K    ./buildkit
4.0K    ./swarm
5.8G    ./overlay2
6.8M    ./image
4.0K    ./tmp
4.0K    ./runtimes
124K    ./network
4.0K    ./trust
20K     ./builder
1.2G    ./volumes
  1. 进入空间大的目录

当前是 containers,文件夹名称对应的是容器的id,通过 docker ps 可查看是哪个容器的文件

$ cd containers
$ du -h --max-depth=1

进入容器目录,可看到与 {容器ID}-json.log 格式的日志文件占用磁盘,容器正在运行中,可将该文件内容置空;如果删除该文件,将导致 docker logs 无法查看容器日志,若误删可通过重启容器重新生成日志文件。

# 清空文件内容
$ cat /dev/null > e79e5e79e702988eb1f5ae61800820106d41f64bc03bcbbab85aed0eaafc4d6f-json.log