背景


在经常使用容器进行测试的过程中,若服务器空间过小,则容易发现磁盘占用满的问题,此时我们可以使用该docker system df命令显示有关docker守护程序使用的磁盘空间量的信息,也可参照官方文档:docker system prune官方文档docker system df官方文档

1
2
3
4
5
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 13 8 3.935 GB 1.875 GB (47%)
Containers 9 8 645 MB 556.2 MB (86%)
Local Volumes 94 4 17.37 GB 9.374 GB (53%)

单独清理镜像、容器、挂载卷和网络


删除所有未被 tag 标记和未被容器使用的镜像:

1
2
3
4
[root@localhost ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
...

删除所有未被挂载的卷:

1
2
3
4
[root@localhost ~]# docker volume prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
...

删除所有停止运行的容器:

1
2
3
4
[root@localhost ~]# docker container prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
...

删除所有网络:

1
2
3
4
[root@localhost ~]# docker network prune
WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
...

删除 docker 所有未被使用的资源


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# docker system prune
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all dangling images
Are you sure you want to continue? [y/N] y
Deleted Containers:
8fe77d443e0055598fceef15b3ffdc01c774a10e0321907ff14924f87671b432

Deleted Volumes:
873f99403f3b951522f94975a4bb7b02e37a22290190764064b92e50e9835f6f
d55050690da4724033d45ad841ef54778b4cbf3516870e4838b28caa923c35f9
...
Total reclaimed space: 9.93 GB