本文翻译自:How to remove old and unused Docker images
When running Docker for a long time, there are a lot of images in system. 长时间运行Docker时,系统中有很多映像。 How can I remove all unused Docker images at once safety to free up the storage? 如何安全地一次删除所有未使用的Docker映像以释放存储空间?
In addition, I also want to remove images pulled months ago, which have the 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
. 我正在寻找一种删除常规未使用图像的方法,其中包括未标记的图像和其他图像,例如几个月前使用正确的TAG
提取的图像。
参考:https://stackoom.com/question/2DIm7/如何删除旧的和未使用的Docker映像
Update Sept. 2016: 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. 2016年9月更新:Docker 1.13: PR 26108和commit 86de7c0引入了一些新命令,以帮助实现可视化docker守护程序数据在磁盘上占用了多少空间,并允许轻松清除“不需要的”多余空间。
docker system prune will delete ALL dangling data (ie In order: containers stopped, volumes without containers and images with no containers). docker system prune会删除所有悬空数据(即,顺序:容器停止,没有容器的卷和没有容器的映像)。 Even unused data, with -a
option. 使用-a
选项甚至可以使用未使用的数据。
You also have: 您还有:
For unused images, use docker image prune -a
(for removing dangling and ununsed images). 对于未使用的图像,请使用docker docker image prune -a
(用于删除悬空和未使用的图像)。
Warning: ' unused ' means "images not referenced by any container": be careful before using -a
. 警告:“ 未使用 ”的意思是“没有任何容器引用的图像”:使用-a
之前要小心。
As illustrated in AL 's answer , docker system prune --all
will remove all unused images not just dangling ones... which can be a bit too much. 如AL的答案所示 , docker system prune --all
会删除所有未使用的映像,而不仅仅是悬空的映像...可能有点太多。
Combining docker xxx prune
with the --filter
option can be a great way to limit the pruning ( docker SDK API 1.28 minimum, so docker 17.04+ ) 将--filter docker xxx prune
与--filter
选项结合使用可能是一种限制修剪的好方法( docker SDK API 1.28最低,因此docker 17.04+ )
The currently supported filters are: 当前支持的过滤器是:
until (<timestamp>)
- only remove containers, images, and networks created before given timestampuntil (<timestamp>)
-仅删除在给定时间戳之前创建的容器,图像和网络label
(label=<key>
,label=<key>=<value>
,label!=<key>
, orlabel!=<key>=<value>
) - only remove containers, images, networks, and volumes with (or without , in caselabel!=...
is used) the specified labels.label
(label=<key>
,label=<key>=<value>
,label!=<key>
或label!=<key>=<value>
)-仅使用()删除容器,图像,网络和卷或不使用 ,如果使用label!=...
则使用指定的标签。
See " Prune images " for an example. 有关示例,请参见“ 修剪图像 ”。
Original answer (Sep. 2016) 原始答案(2016年9月)
I usually do: 我通常这样做:
docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
I have an alias for removing those [dangling images] 13 : drmi
我有一个别名来删除那些[悬挂的图像] 13 : drmi
The
dangling=true
filter finds unused imagesdangling=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")'
As haridsv points out in the comments : 正如haridsv 在评论中指出的那样:
Technically, you should first clean up containers before cleaning up images, as this will catch more dangling images and less errors . 从技术上讲, 您应该在清理图像之前先清理容器,因为这样可以捕获更多的晃动图像并减少错误 。
Jess Frazelle (jfrazelle) has the bashrc function : 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
}
To remove old images, and not just "unreferenced-dangling" images, you can consider docker-gc : 要删除旧映像,而不仅仅是“未引用悬挂”映像,可以考虑docker-gc :
A simple Docker container and image garbage collection script. 一个简单的Docker容器和图像垃圾收集脚本。
- Containers that exited more than an hour ago are removed. 一个多小时前退出的容器将被删除。
- Images that don't belong to any remaining container after that are removed. 之后不属于任何剩余容器的图像将被删除。
#3楼
@VonC already gave a very nice answer, but for completeness here is a little script I have been using---and which also nukes any errand Docker processes should you have some: @VonC已经给出了一个非常好的答案,但是为了完整起见,这里有一个我一直在使用的小脚本---如果您有一些脚本的话,它也会使任何繁琐的Docker进程都受到干扰:
#!/bin/bash
imgs=$(docker images | awk '/<none>/ { print $3 }')
if [ "${imgs}" != "" ]; then
echo docker rmi ${imgs}
docker rmi ${imgs}
else
echo "No images to remove"
fi
procs=$(docker ps -a -q --no-trunc)
if [ "${procs}" != "" ]; then
echo docker rm ${procs}
docker rm ${procs}
else
echo "No processes to purge"
fi
#4楼
If you want to remove images pulled X months ago, you can try the below example which remove images created three months ago: 如果要删除X个月前提取的图像,可以尝试以下示例,该示例删除三个月前创建的图像:
three_months_old_images=`docker images | grep -vi "<none>" | tr -s ' ' | cut -d" " -f3,4,5,6 | grep "3 months ago" | cut -d" " -f1`
docker rmi $three_months_old_images
#5楼
docker rm `docker ps -aq`
要么
docker rm $(docker ps -q -f status=exited)
#6楼
Update the second (2017-07-08): 更新第二个(2017-07-08):
Refer (again) to VonC, using the even more recent system prune
. 使用最新的system prune
(再次)参考VonC。 The impatient can skip the prompt with the -f, --force
option: 不耐烦的可以通过-f, --force
选项跳过提示:
docker system prune -f
The impatient and reckless can additionally remove "unused images not just the dangling ones" with the -a, --all
option: 不耐烦和鲁ck的用户还可以使用-a, --all
选项删除“未使用的图像,而不仅仅是悬空的图像”:
docker system prune -af
https://docs.docker.com/engine/reference/commandline/system_prune/ https://docs.docker.com/engine/reference/commandline/system_prune/
Update: 更新:
Refer to VonC's answer which uses the recently added prune
commands. 请参阅VonC的答案 ,该答案使用最近添加的prune
命令。 Here is the corresponding shell alias convenience: 这是相应的shell别名方便性:
alias docker-clean=' \
docker container prune -f ; \
docker image prune -f ; \
docker network prune -f ; \
docker volume prune -f '
Old answer: 旧答案:
Delete stopped (exited) containers: 删除停止(退出)的容器:
$ docker ps --no-trunc -aqf "status=exited" | xargs docker rm
Delete unused (dangling) images: 删除未使用的(悬挂的)图像:
$ docker images --no-trunc -aqf "dangling=true" | xargs docker rmi
If you have exercised extreme caution with regard to irrevocable data loss , then you can delete unused (dangling) volumes (v1.9 and up): 如果您对不可撤销的数据丢失 非常谨慎 ,则可以删除未使用的(悬挂的)卷(v1.9及更高版本):
$ docker volume ls -qf "dangling=true" | xargs docker volume rm
Here they are in a convenient shell alias: 在这里,它们是一个方便的shell别名:
alias docker-clean=' \
docker ps --no-trunc -aqf "status=exited" | xargs docker rm ; \
docker images --no-trunc -aqf "dangling=true" | xargs docker rmi ; \
docker volume ls -qf "dangling=true" | xargs docker volume rm'
References: 参考文献: