在长时间运行Docker时,系统中存在大量的镜像。如何一次安全地删除所有未使用的Docker映像以释放存储空间?
另外,我还想删除几个月前拉的图片,这些图片有正确的TAG。
因此,我并不是只要求删除未标记的图像。我正在寻找一种方法来删除一般不使用的图像,其中包括未标记和其他图像,如几个月前拉正确的TAG。
在长时间运行Docker时,系统中存在大量的镜像。如何一次安全地删除所有未使用的Docker映像以释放存储空间?
另外,我还想删除几个月前拉的图片,这些图片有正确的TAG。
因此,我并不是只要求删除未标记的图像。我正在寻找一种方法来删除一般不使用的图像,其中包括未标记和其他图像,如几个月前拉正确的TAG。
当前回答
首先,运行docker images查看图像列表,并将IMAGE HASH ID复制到剪贴板中。
运行docker rmi -f <镜像>
记住选项-f是强制删除。
其他回答
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由于软件错误或意外重启而失去使用磁盘空间的跟踪时,运行它是有用的。
其他答案都很棒,具体来说:
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
如果你有很多这样的图像,移除它们可能真的很乏味,但幸运的是Docker有几个命令可以帮助我们消除悬浮图像。在旧版本的Docker中(这在今天仍然有效),你可以通过运行Docker rmi -f $(Docker images -f " hanging =true" -q)来删除悬浮图像。
几周前移除旧容器。
Docker rm $(Docker ps -a | grep "weeks" | awk '{打印$1;}”)
几周前删除旧图像。小心些而已。这将删除几周前创建的基本图像,但您的新图像可能会使用这些图像。
Docker rmi $(Docker images | grep 'weeks' | awk '{打印$3;}”)
docker rm `docker ps -aq`
or
docker rm $(docker ps -q -f status=exited)