如何删除旧的和未使用的 Docker 镜像 [英] How to remove old and unused Docker images

查看:192
本文介绍了如何删除旧的和未使用的 Docker 镜像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

此外,我还想删除几个月前提取的具有正确TAG的图像.

所以,我不是要求只删除未标记的图像.我正在寻找一种方法来删除一般未使用的图像,其中包括未标记的图像和其他图像,例如几个月前使用正确的 TAG 提取的图像.

解决方案

2016 年 9 月更新:Docker 1.13:PR 26108commit 86de7c0 引入了一些新命令以帮助促进可视化 docker daemon 数据在磁盘上占用了多少空间,并允许轻松清理不需要的";过量.

docker 系统修剪 将删除所有悬空数据(即按顺序:容器停止,没有容器的卷和没有容器的图像).甚至未使用的数据,使用 -a 选项.

你还有:

对于未使用的图像,使用docker image prune -a(用于删除悬垂的未使用的图像).
警告:未使用"表示图像未被任何容器引用":在使用 -a 之前要小心.

AL答案docker system prune --all 将删除所有未使用 的图像,而不仅仅是悬空的图像......这可能有点太多了.

结合 docker xxx prune--filter 选项 是限制修剪的好方法 (docker SDK API 1.28 最低,所以 docker 17.04+)

<块引用>

目前支持的过滤器有:

  • until () - 只删除在给定时间戳之前创建的容器、图像和网络
  • label (label=, label==, label!=<key>, or label!=<key>=<value>) - 只删除容器、图像、网络和卷(或没有,如果使用了 label!=...)指定的标签.

参见修剪图像";举个例子.

原始答案(2016 年 9 月)

我通常这样做:

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

我有一个 [别名用于删除那些 悬空图像:drmi]13

<块引用>

dangling=true 过滤器查找未使用的图像

这样,任何不再被标记图像引用的中间图像都会被删除.

首先退出的进程执行相同的操作(容器)

alias drmae='docker rm $(docker ps -qa --no-trunc --filter "status=exited")'

正如 haridsv 指出的那样 在评论中:

<块引用>

从技术上讲,您应该先清理容器,然后再清理图像,因为这样可以捕获更多悬空图像并减少错误.


Jess Frazelle (jfrazelle) 拥有 bashrc 函数:

dcleanup(){docker rm -v $(docker ps --filter status=exited -q 2>/dev/null) 2>/dev/nulldocker rmi $(docker images --filter dangling=true -q 2>/dev/null) 2>/dev/null}


删除旧图像,而不仅仅是未引用的悬垂"图片,可以考虑docker-gc:


<块引用>

一个简单的 Docker 容器和图像垃圾收集脚本.

<块引用>

  • 一个多小时前退出的容器被移除.
  • 删除之后不属于任何剩余容器的图像.

When running Docker for 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?

In addition, I also want to remove images pulled months ago, which have the correct 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.

解决方案

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.

docker system prune will delete ALL dangling data (i.e. In order: containers stopped, volumes without containers and images with no containers). Even unused data, with -a option.

You also have:

For unused images, use docker image prune -a (for removing dangling and ununsed images).
Warning: 'unused' means "images not referenced by any container": be careful before using -a.

As illustrated in A L's answer, docker system prune --all will remove all unused images not just dangling ones... which can be a bit too much.

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+)

The currently supported filters are:

  • until (<timestamp>) - only remove containers, images, and networks created before given timestamp
  • label (label=<key>, label=<key>=<value>, label!=<key>, or label!=<key>=<value>) - only remove containers, images, networks, and volumes with (or without, in case label!=... is used) the specified labels.

See "Prune images" for an example.

Original answer (Sep. 2016)

I usually do:

docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

I have an [alias for removing those dangling images: drmi]13

The dangling=true filter finds unused images

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:

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:

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:


A simple Docker container and image garbage collection script.

  • Containers that exited more than an hour ago are removed.
  • Images that don't belong to any remaining container after that are removed.

这篇关于如何删除旧的和未使用的 Docker 镜像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆