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
.
Assuming you have Docker 1.13 or higher you can just use the prune commands. For your question specifically for removing old images, you want the first one.
I would recommend not getting used to using the
docker system prune
command. I reckon users will accidentally remove things they don't mean to. Personally, I'm going to mainly be using thedocker image prune
anddocker container prune
commands.To remove tagged images which have not container running, you will have to use a little script:
If you want to remove images pulled X months ago, you can try the below example which remove images created three months ago:
The other answers are great, specifically:
But I needed something in the middle of the two commands so the
filter
option was what I needed:Hope that helps :)
For reference: https://docs.docker.com/config/pruning/#prune-images
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.
Here is what I came up with. First to resolve the disk full error:
At this point I no longer had a disk full error, but I was still wasting a huge amount of space. The next steps are to take care of that.
Start Docker: systemctl start docker
Save the all the images: docker save $(docker images |sed -e '/^/d' -e '/^REPOSITORY/d' -e 's,[ ][ ],:,' -e 's,[ ].,,') > /root/docker.img
Uninstall docker.
Erase everything in /var/lib/docker: rm -rf /var/lib/docker/[cdintv]*
Reinstall docker
Enable docker: systemctl enable docker
Start docker: systemctl start docker
Restore images: docker load < /root/docker.img
Start any persistent containers you need running.
This dropped my disk usage from 67 GB for docker to 6 GB for docker.
I do not recommend this for everyday use. But it is useful to run when it looks like docker has lost track of used disk space do to software errors, or unexpected reboots.
Here is a script to clean up Docker images and reclaim the space.
Original script
https://github.com/vishalvsh1/docker-image-cleanup
Usually Docker keeps all temporary files related to image building and layers at
/var/lib/docker
This path is local to the system, usually at THE root partition, "/".
You can mount a bigger disk space and move the content of
/var/lib/docker
to the new mount location and make a symbolic link.This way, even if Docker images occupy space, it will not affect your system as it will be using some other mount location.
Original post: Manage Docker images on local disk