docker system df命令,类似于Linux上的df命令,用于查看Docker的磁盘使用情况:
docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 147367.204GB 3.887GB (53%)
Containers 3710104.8MB 102.6MB (97%)
Local Volumes 331.421GB 0B (0%)
Build Cache 0B 0B
docker system prune命令可以用于清理磁盘,删除关闭的容器、无用的数据卷和网络,以及dangling镜像(即无tag的镜像)。docker system prune -a命令清理得更加彻底,可以将没有容器使用Docker镜像都删掉。注意,这两个命令会把你暂时关闭的容器,以及暂时没有用到的Docker镜像都删掉了…所以使用之前一定要想清楚吶。
执行docker system prune -a命令之后,Docker占用的磁盘空间减少了很多:
docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 10102.271GB 630.7MB (27%)
Containers 10102.211MB 0B (0%)
Local Volumes 331.421GB 0B (0%)
Build Cache 0B 0B
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a376aa694b22 192.168.59.224:5000/nginx:1.12.1"nginx -g 'daemon off"9 weeks ago Up10 minutes nginx
还有一次,当我清理了镜像、容器以及数据卷之后,发现磁盘空间并没有减少。根据Docker disk usage提到过的建议,我重启了Docker,发现磁盘使用率从83%降到了19%。根据高手指点,这应该是与内核3.13相关的BUG,导致Docker无法清理一些无用目录:
it’s quite likely that for some reason when those container shutdown, docker couldn’t remove the directory because the shm device was busy. This tends to happen often on 3.13 kernel. You may want to update it to the 4.4 version supported on trusty 14.04.5 LTS.
The reason it disappeared after a restart, is that daemon probably tried and succeeded to clean up left over data from stopped containers.