本文翻译自: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提取的图像。


#1楼

参考:https://stackoom.com/question/2DIm7/如何删除旧的和未使用的Docker映像


#2楼

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: 您还有:

  • docker container prune
  • docker image prune
  • docker network prune
  • docker volume prune

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 timestamp until (<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. labellabel=<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 images dangling=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/nulldocker 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/bashimgs=$(docker images | awk '/<none>/ { print $3 }')
if [ "${imgs}" != "" ]; thenecho docker rmi ${imgs}docker rmi ${imgs}
elseecho "No images to remove"
fiprocs=$(docker ps -a -q --no-trunc)
if [ "${procs}" != "" ]; thenecho docker rm ${procs}docker rm ${procs}
elseecho "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: 参考文献:

  • docker ps -f
  • docker rm
  • docker images -f
  • docker rmi
  • Docker v1.9.0 release notes Docker v1.9.0发行说明
  • docker volume ls
  • docker volume rm

如何删除旧的和未使用的Docker映像相关推荐

  1. docker数据卷容器卷_如何删除Docker映像,容器和卷

    docker数据卷容器卷 Docker备忘单 (A Docker Cheat Sheet) 介绍 (Introduction) Docker makes it easy to wrap your ap ...

  2. 清理SQL Server中的旧代码和未使用的对象

    In this article, we are going to talk about discovering and archiving SQL references to invalid proc ...

  3. ubuntu删除旧的linux内核

    ubuntu16.04删除旧的linux内核:(在/boot空间满了的情况) 今天想安装搜狗输入法,遇到了一个问题,就是/boot空间满了,配置不了一些文件. 这里记录以下如何删除不用的内核. 第一步 ...

  4. 苹果电脑怎么删除旧账户_如何找到您不记得的旧在线帐户

    苹果电脑怎么删除旧账户 What do you think: How many online accounts do you have? Well, there is Facebook, Instag ...

  5. 清理 linux 内核版本,grub2 - 如何删除旧的内核版本以清理引导菜单? - Ubuntu问答...

    问题描述 每次安装新的Linux内核时,它都会留在grub_config中,从而使引导菜单每次都更长. 我知道我可以手动搜索已安装的软件包并将其删除. Ubuntu是否提供任何更简便的方法来清理它们或 ...

  6. 在 CentOS 8 中删除旧的 Linux 内核

    如果更新了 Linux 操作系统,那么你会注意到,每次升级 Linux 内核后,GRUB 菜单都会添加一个新的引导条目,同时保持以前的条目不变.本文介绍如何删掉多余的内核. 默认情况下 yum 包管理 ...

  7. Windows下批量删除旧文件、清除缓存文件、解救C盘、拒绝C盘爆炸

    Windows下批量删除旧文件.清除缓存文件.解救C盘.拒绝C盘爆炸 目录 Windows下批量删除旧文件.清除缓存文件.解救C盘.拒绝C盘爆炸 #删除旧文件1 #删除旧文件2 #删除旧文件1 rem ...

  8. 使用Python批量删除windows下特定目录的N天前的旧文件实战:Windows下批量删除旧文件、清除缓存文件、解救C盘、拒绝C盘爆炸

    使用Python批量删除windows下特定目录的N天前的旧文件实战:Windows下批量删除旧文件.清除缓存文件.解救C盘.拒绝C盘爆炸 目录

  9. ASP.NET 5 DNX SDK删除旧版本

    ASP.NET 5各种升级后旧版本的DNX不会删除,想删除旧版本的DNX,可以通过以下命令完成 首先打开CMD或者Powershell 1.先输入dnvm看看命令中是否有uninstall 2.如果没 ...

最新文章

  1. mysql 硬盘写入速度_MySQL存储写入速度慢分析
  2. Ubuntu复制相同的目录结构(不复制文件)
  3. linux fedora下vscode终端字体间距不正常解决办法
  4. opencv python教程简书_OpenCV-Python系列二:常用的图像属性
  5. 端智能揭秘|促使双十一GMV大幅提升,手淘用了什么秘密武器?
  6. 主成分分析PCA案例及原理
  7. python不可实现的领域3d_岩土工程新手入门指南---FLAC3D学习指导与建议
  8. python网络编程系列
  9. 输入身高、体重、性别,判断是否是标准体重,男性标准=(身高-100)+-3,女性标准=(身高-110)+-3...
  10. PHP Filter 简介
  11. 网络聊天室——低仿QQ
  12. Nacos集群环境搭建
  13. 【实战】下载歌曲只能开绿钻?NoNoNo, Python爬虫,无所不能。
  14. CC2640R2FRSMR低功耗M3内核蓝牙MCU
  15. zabbix 报警 Lack of free swap space on Zabbix server 处理
  16. hive------内部函数与自定义函数
  17. 「历时6个月招聘数据收集」一份Python招聘分析报告
  18. OpenStack+Ceph存储空间回收
  19. uniAPP 自定义页面导航烂 - 搜索 APP有效
  20. video.js播放rtmp视频

热门文章

  1. java网络编程3 -- NIO一些简单说明
  2. js实现字符串的加密与解密
  3. 二维凸包(模板) hdu 1348 求凸包的周长
  4. touchend与click顺序
  5. jquery.alerts.js模拟js的alert,confirm的插件
  6. JavaScript学习_第2章_JS语法规则
  7. Kofi's back
  8. 《梦幻西游》打响反盗号战役:为2亿玩家提供360安全武器
  9. vs2005环境的一些快捷键
  10. django中的media