原文:https://docs.docker.com/engine/admin/pruning/
对于不再使用的对象如镜像、容器、volumes以及网络 Docker采取的是被动清理(类比垃圾回收)机制:除非使用docker提供的命令手动进行清理,否则它们一般是不会被清除掉。这些没有使用的对象当然会占用额外宝贵的空间资源。Docker为不同的对象提供了各自的对象清理命令prune;另外还提供了docker system prune命令可一次性清理多个未使用对象。今天要讨论的就是这些关于对象清理的命令:prune。
$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
有时侯我们想要清除所有没有容器引用的镜像,增加一个
-a 标志就可以搞定:
$ docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
清除操作会提醒你是否真心想要清除对象,默认是选项会是yes;但是如果你嫌提示麻烦,可以通过
-f 或者--force标志来进行强制清除。
$ docker image prune -a --filter "until=24h"
当然还能够通过其他的表达式来定制我的镜像清理计划。更多的示例参考 docker image prune。
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N]
和镜像清理的情况一样,也会有提示信息告诉你是否继续,默认是yes;如果提示信息烦到了你的话就加上
-f 或者
--force标志强制清除就可以了。
$ docker container prune --filter "until=24h"
其他的筛选条件的实现可以参考: docker container prune reference, 这里有更多的详细的例子。
$ 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
和conatiner一样,手动清理Volume时会有提示信息,增加
-f 或
--force标志可以跳过提示信息直接清理。使用过滤参数
--filter来筛选出不希望清理的无用Volume,否则默认会将所有没有使用的volumes都清理掉。下面的例子演示了除
lable=keep外的volume外都清理掉(没有引用的volume):
$ docker volume prune --filter "label!=keep"
其他的筛选条件的实现可以参考: docker volume prune reference,这里给出了更多参考示例。
$ 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
可以通过
-f 或者
--force标志跳过提示信息来强制执行该命令。默认情况会清除所有没有再被引用的networks,如果想要过滤一些特定的networks,可以使用
--filter来实现。下面这个例子就是通过--filter来清理没有被引用的、创建超过24小时的networks:
$ docker network prune --filter "until=24h"
更多关于docker network的--filter的筛选条件可参考示例: docker network prune reference 。
$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
在Docker 17.06.1或更高版本中添加
--volumes标志的情况:
$ docker system prune --volumes
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y