在长时间运行Docker时,系统中存在大量的镜像。如何一次安全地删除所有未使用的Docker映像以释放存储空间?
另外,我还想删除几个月前拉的图片,这些图片有正确的TAG。
因此,我并不是只要求删除未标记的图像。我正在寻找一种方法来删除一般不使用的图像,其中包括未标记和其他图像,如几个月前拉正确的TAG。
在长时间运行Docker时,系统中存在大量的镜像。如何一次安全地删除所有未使用的Docker映像以释放存储空间?
另外,我还想删除几个月前拉的图片,这些图片有正确的TAG。
因此,我并不是只要求删除未标记的图像。我正在寻找一种方法来删除一般不使用的图像,其中包括未标记和其他图像,如几个月前拉正确的TAG。
当前回答
如果你想删除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
其他回答
根据文档,下面的命令将删除超过48小时的图像。
$ docker image prune --all --filter until=48h
下面是一个清理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映像
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 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 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