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

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

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


当前回答

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

运行docker-remove-dangling-images

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

其他回答

假设你有Docker 1.13或更高版本,你可以只使用修剪命令。对于你的问题,特别是关于删除旧图像,你想要第一个。

# Remove unused images
docker image prune

# Remove stopped containers.
docker container prune

# Remove unused volumes
docker volume prune

# Remove unused networks
docker network prune

# Command to run all prunes:
docker system prune

我建议不要习惯使用docker系统的prune命令。我认为用户会不小心删除他们不想删除的内容。就我个人而言,我将主要使用docker映像修剪和docker容器修剪命令。

如何删除带标签的图像

Docker先取标签 Docker对图像进行了描述。 #可以在一个docker rmi调用中完成,例如:# Docker rmi <repo:tag> <imageid>

(2016年11月,Docker版本1.12.2)

e.g.

$ docker images 
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
usrxx/the-application   16112805            011fd5bf45a2        12 hours ago        5.753 GB
usryy/the-application   vx.xx.xx            5af809583b9c        3 days ago          5.743 GB
usrzz/the-application   vx.xx.xx            eef00ce9b81f        10 days ago         5.747 GB
usrAA/the-application   vx.xx.xx            422ba91c71bb        3 weeks ago         5.722 GB
usrBB/the-application   v1.00.18            a877aec95006        3 months ago        5.589 GB

$ docker rmi usrxx/the-application:16112805 && docker rmi 011fd5bf45a2
$ docker rmi usryy/the-application:vx.xx.xx && docker rmi 5af809583b9c
$ docker rmi usrzz/the-application:vx.xx.xx eef00ce9b81f
$ docker rmi usrAA/the-application:vx.xx.xx 422ba91c71bb
$ docker rmi usrBB/the-application:v1.00.18 a877aec95006

例:脚本删除超过2周的内容。

IMAGESINFO=$(docker images --no-trunc --format '{{.ID}} {{.Repository}} {{.Tag}} {{.CreatedSince}}' |grep -E " (weeks|months|years)")
TAGS=$(echo "$IMAGESINFO" | awk '{ print $2 ":" $3 }' )
IDS=$(echo "$IMAGESINFO" | awk '{ print $1 }' )
echo remove old images TAGS=$TAGS IDS=$IDS
for t in $TAGS; do docker rmi $t; done
for i in $IDS; do docker rmi $i; done
docker rm `docker ps -aq`

or

docker rm $(docker ps -q -f status=exited)

要删除没有容器运行的标记图像,你必须使用一个小脚本:

#!/bin/bash

# remove not running containers
docker rm $(docker ps -f "status=exited" -q)

declare -A used_images

# collect images which has running container
for image in $(docker ps | awk 'NR>1 {print $2;}'); do
    id=$(docker inspect --format="{{.Id}}" $image);
    used_images[$id]=$image;
done

# loop over images, delete those without a container
for id in $(docker images --no-trunc -q); do
    if [ -z ${used_images[$id]} ]; then
        echo "images is NOT in use: $id"
        docker rmi $id
    else
        echo "images is in use:     ${used_images[$id]}"
    fi
done

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由于软件错误或意外重启而失去使用磁盘空间的跟踪时,运行它是有用的。