清理Docker磁盘空间

docker Sep 27, 2021

Docker的imagecontainer会占用大量的磁盘空间,Docker提供了docker system prune命令和手动的方式清理空间。

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]
使用docker system prune清理空间

或者清理得更加彻底,可以将没有容器使用Docker镜像都删掉:

docker system prune -a
注意:这两个命令会把你暂时关闭的容器,以及暂时没有用到的Docker镜像都删掉

手动清理

使用手动操作进行清理,首先删除所有关闭的容器:

docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm

删除所有dangling镜像(即无tag的镜像)

docker rmi $(docker images | grep "^<none>" | awk "{print $3}")

删除所有dangling数据卷(即无用的Volume)

docker volume rm $(docker volume ls -qf dangling=true)

(完)

标签

毛俊

探索,热爱,分享。

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.