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

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

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


当前回答

Docker系统修剪-a

(系统会要求您确认命令。使用-f强制运行,如果你知道你在做什么。)

其他回答

下面是一个清理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系统剪枝——全部

Occasionally I have run into issues where Docker will allocate and continue to use disk space, even when the space is not allocated to any particular image or existing container. The latest way I generated this issue accidentally was using "docker-engine" centos build instead of "docker" in RHEL 7.1. What seems to happen is sometimes the container clean-ups are not completed successfully and then the space is never reused. When the 80GB drive I allocated as / was filled with /var/lib/docker files I had to come up with a creative way to resolve the issue.

这是我想到的。首先解决磁盘满的错误:

Stop docker: systemctl Stop docker 分配一个新的驱动器挂载为say /mnt/docker。 移动/var/lib/docker目录下的所有文件到/mnt/docker目录下。我使用命令: rsync -aPHSx——remove-source-files /var/lib/docker/ /mnt/docker/ 将新驱动器挂载到/var/lib/docker。

此时,我不再有磁盘满的错误,但我仍然浪费了大量的空间。接下来的步骤就是解决这个问题。

Start Docker: systemctl Start Docker 保存所有图片: 码头工人节省$(码头工人图片| sed - e ' / ^ <无> / d ' - e / ^库/ d - e的年代 ,[ ][ ]*,:,' - e ' s ,[ ].*,,') > / 根/ docker.img 卸载码头工人。 删除/var/lib/docker中的所有内容: Rm -rf /var/lib/docker/[cdintv]* 重新安装码头工人 启用docker: systemctl Enable docker 启动docker: systemctl Start docker 恢复图片: Docker load < /root/docker.img 启动任何需要运行的持久容器。

这使我的磁盘使用量从docker的67 GB下降到6 GB。

我不建议日常使用。但是,当docker由于软件错误或意外重启而失去使用磁盘空间的跟踪时,运行它是有用的。

你可以使用sparrow插件docker-remove-dangling-images来清理停止的容器和未使用的(悬挂的)图像:

运行docker-remove-dangling-images

它适用于Linux和Windows操作系统。

我正在使用这个命令:

export BEFORE_DATETIME=$(date --date='10 weeks ago' +"%Y-%m-%dT%H:%M:%S.%NZ")
docker images -q | while read IMAGE_ID; do
    export IMAGE_CTIME=$(docker inspect --format='{{.Created}}' --type=image ${IMAGE_ID})
    if [[ "${BEFORE_DATETIME}" > "${IMAGE_CTIME}" ]]; then
        echo "Removing ${IMAGE_ID}, ${BEFORE_DATETIME} is earlier then ${IMAGE_CTIME}"
        docker rmi -f ${IMAGE_ID};
    fi;
done

这将删除创建时间大于10周的所有映像。