在长时间运行Docker时,系统中存在大量的镜像。如何一次安全地删除所有未使用的Docker映像以释放存储空间?

另外,我还想删除几个月前拉的图片,这些图片有正确的TAG。

因此,我并不是只要求删除未标记的图像。我正在寻找一种方法来删除一般不使用的图像,其中包括未标记和其他图像,如几个月前拉正确的TAG。


当前回答

这招对我很管用:

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

其他回答

下面是一个清理Docker映像并回收空间的脚本。

#!/bin/bash -x
## Removing stopped container
docker ps -a | grep Exited | awk '{print $1}' | xargs docker rm

## If you do not want to remove all container you can have filter for days and weeks old like below
#docker ps -a | grep Exited | grep "days ago" | awk '{print $1}' | xargs docker rm
#docker ps -a | grep Exited | grep "weeks ago" | awk '{print $1}' | xargs docker rm

## Removing Dangling images
## There are the layers images which are being created during building a Docker image. This is a great way to recover the spaces used by old and unused layers.

docker rmi $(docker images -f "dangling=true" -q)

## Removing images of perticular pattern For example
## Here I am removing images which has a SNAPSHOT with it.

docker rmi $(docker images | grep SNAPSHOT | awk '{print $3}')

## Removing weeks old images

docker images | grep "weeks ago" | awk '{print $3}' | xargs docker rmi

## Similarly you can remove days, months old images too.

原始的脚本

https://github.com/vishalvsh1/docker-image-cleanup

通常Docker将所有与图像构建和图层相关的临时文件保存在

/var/lib/docker

该路径是系统的本地路径,通常位于根分区“/”。

您可以挂载一个更大的磁盘空间,并将/var/lib/docker的内容移动到新的挂载位置,并进行符号链接。

这样,即使Docker映像占用空间,也不会影响您的系统,因为它将使用其他一些挂载位置。

原文章:管理本地磁盘上的Docker映像

其他答案都很棒,具体来说:

docker system prune # doesn't clean out old images
docker system prune --all # cleans out too much

但我需要在两个命令中间的一些东西,所以过滤器选项是我所需要的:

docker image prune --all --filter "until=4320h" # delete images older than 6 months ago; 4320h = 24 hour/day * 30 days/month * 6 months

参考:https://docs.docker.com/config/pruning/#prune-images

如果你想删除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

几周前移除旧容器。

Docker rm $(Docker ps -a | grep "weeks" | awk '{打印$1;}”)

几周前删除旧图像。小心些而已。这将删除几周前创建的基本图像,但您的新图像可能会使用这些图像。

Docker rmi $(Docker images | grep 'weeks' | awk '{打印$3;}”)

如果你自己构建这些修剪过的映像(从其他一些较旧的基础映像),请谨慎使用上述基于docker image prune的可接受解决方案,因为该命令是生硬的,并将尝试删除最新映像所需的所有依赖关系(该命令可能应该重命名为docker image*s* prune)。

我为我的docker映像构建管道(其中有每日构建和标签=日期YYYYMMDD格式)提出的解决方案是:

# carefully narrow down the image to be deleted (to avoid removing useful static stuff like base images)
my_deleted_image=mirekphd/ml-cpu-py37-vsc-cust

# define the monitored image (tested for obsolescence), which will be usually the same as deleted one, unless deleting some very infrequently built image which requires a separate "clock"
monitored_image=mirekphd/ml-cache

# calculate the oldest acceptable tag (date)
date_week_ago=$(date -d "last week" '+%Y%m%d')

# get the IDs of obsolete tags of our deleted image
# note we use monitored_image to test for obsolescence
my_deleted_image_obsolete_tag_ids=$(docker images --filter="before=$monitored_image:$date_week_ago" | grep $my_deleted_image | awk '{print $3}')

# remove the obsolete tags of the deleted image
# (note it typically has to be forced using -f switch)
docker rmi -f $my_deleted_image_obsolete_tag_ids