FROM alpine RUN echo foo RUN dd if=/dev/zero of=1g1.img bs=1G count=1 RUN dd if=/dev/zero of=1g2.img bs=1G count=1 # RUN dd if=/dev/zero of=1g3.img bs=1G count=1 CMD /bin/true
本来以为这样可以节省1GB磁盘空间,然而实际情况更加糟糕!
$ df -h Filesystem Size Used Available Use% Mounted on /dev/mapper/... 10.0G 5.4G 4.5G 54% /
旧的Docker镜像一直存在,最终磁盘空间会很快被用完。Docker 1.13引入了docker system df命令,类似于Linux上的df命令,用于查看Docker的磁盘使用情况。
$ docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 3 0 5.373GB 5.373GB (100%) Containers 0 0 0B 0B Local Volumes 0 0 0B 0B
可知,实例上一共有3个Docker镜像: apline镜像,包含3个1GB随机文件的镜像以及包含2个1GB随机文件的镜像。这些镜像占用了超过5GB磁盘空间。由于我们并没有基于这些镜像运行容器,所以它们都可以被删除,所以可回收的(RECLAIMABLE)磁盘空间为100%。使用docker run test运行test镜像再查看:
$ docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 3 1 5.373GB 3.225GB (60%) Containers 1 0 0B 0B Local Volumes 0 0 0B 0B
现在来清理一下磁盘空间。Docker提供了docker system prune,可以用于清理dangling镜像(参考What are Docker:images?)和容器,以及失效的数据卷和网络。
$ 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: 1cdf866157b4a97e151125af3c2a7f186a59b6f63807e2014ce1a00d68f44e1d Deleted Images: deleted: sha256:f59bb277... deleted: sha256:695b8e70... deleted: sha256:93b1cceb... deleted: sha256:c74d6bcd... deleted: sha256:df8b9bb1... deleted: sha256:dfe8340f... deleted: sha256:ce1ee654... Total reclaimed space: 3.221GB
$ docker system prune -a 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 images without at least one container associated to them Are you sure you want to continue? [y/N] y Deleted Images: untagged: test:latest deleted: sha256:c515ebfa2... deleted: sha256:07302c011... deleted: sha256:37c0c6474... deleted: sha256:5cc2b6bc4... deleted: sha256:b283b9c35... deleted: sha256:8a8b9bd8b... untagged: alpine:latest untagged: alpine@sha256:58e1a1bb75db1... deleted: sha256:4a415e366... deleted: sha256:23b9c7b43... Total reclaimed space: 2.151GB