如何清除旧的和未使用的Docker图像 [英] How to remove old and unused Docker images
问题描述
当运行docker很长时间,系统中有很多图像。如何一次卸载所有未使用的码头图像以释放存储空间?
When running docker a long time, there are a lot of images in system. How can I remove all unused docker images at once safety to free up the storage?
此外,我还要删除几个月前拉出的图像,这些图像具有正确的 TAG
In addition, I also want to remove images pulled months ago, which have correct TAG
所以,我不是要求删除未标记的图像。我正在寻找一种方法来删除一般未使用的图像,其中包括未标记的图像和其他图像,例如在几个月前拉出正确的 TAG
So, I'm not asking for removing untagged images only. I'm searching for a way to remove general unused images, which includes both untagged and other images such as pulled months ago with correct TAG
推荐答案
更新9月2016年docker即将到来的码头1.13: PR 26108 和提交86de7c0 介绍几个新命令来帮助有助于可视化Docker守护进程数据占用的磁盘空间,并允许轻松清理不需要的超量。
Update Sept. 2016 for docker upcoming docker 1.13: PR 26108 and commit 86de7c0 introduce a few new commands to help facilitate visualizing how much space the docker daemon data is taking on disk and allowing for easily cleaning up "unneeded" excess.
docker系统修剪
将删除所有未使用数据(即按顺序:容器停止,没有容器的卷和没有容器的图像)。
docker system prune
will delete ALL unused data (i.e. In order: containers stopped, volumes without containers and images with no containers).
您还有:
docker container prune
docker image prune
docker network prune
docker volume prune
原始答案(9月) 2016)
Original answer (Sep. 2016)
我通常会做:
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
我有一个< a href =https://github.com/docker/docker/blob/634a848b8e3bdd8aed834559f3b2e0dfc7f5ae3a/man/docker-images.1.md#options =noreferrer>删除那些[悬挂图片]的别名 8 : drmi
I have an alias for removing those [dangling images]8: drmi
dangling = true
未使用的图像
这样,任何不再被标记图像引用的中间图像都将被删除。
That way, any intermediate image no longer referenced by a labelled image is removed.
I do the same first for exited processes (containers)
alias drmae='docker rm $(docker ps -qa --no-trunc --filter "status=exited")'
haridsv 指出中删除旧的和未使用的docker-images / 32723127#comment63457575_32723127>
As haridsv points out in the comments:
从技术上讲,你应该首先清理容器,然后再清理图像,因为这样会吸引更多的悬挂图像和更少的错误。
Jess Frazelle(jfrazelle)拥有 bashrc函数:
dcleanup(){
docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/null
docker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null
}
要删除旧图像,而不仅仅是未引用的悬挂图像,您可以考虑 docker-gc
:
To remove old images, and not just "unreferenced-dangling" images, you can consider docker-gc
:
一个简单的Docker容器和图像垃圾收集脚本。
A simple Docker container and image garbage collection script.
- 退出的容器超过一小时前被删除。
- 删除后不属于任何剩余容器的图像。
这篇关于如何清除旧的和未使用的Docker图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!