Is there a shortcut rmi
command to delete all images that are currently not in use, or cool shell script I could use?
We are accumulating tons of images during deploys and I need to keeps stuff squeeky on the prod boxes.
Share and learn in the Docker community.
Is there a shortcut rmi
command to delete all images that are currently not in use, or cool shell script I could use?
We are accumulating tons of images during deploys and I need to keeps stuff squeeky on the prod boxes.
My favorite way of removing all stopped docker containers is:
docker ps -q |xargs docker rm
it will list all images (docker ps), but only show the idâs. And then run a docker rm command for each one of them.
Similarly for removing all unused docker images
docker images -q |xargs docker rmi
Will list all docker images and then docker rmi them.
nooooooooo!
docker rmi $(docker ps -q)
My main issue is dealing with orphan images.
docker rmi `docker images | awk '{ print $3; }'`
Does the trick⊠however it feels like I am playing with fire and it raises lots of errors.
Hi,
Iâm using:
docker rmi -f $(docker images | grep "<none>" | awk "{print \$3}")
in order to get rid of all untagged images.
Damien.
Other ways to skin the proverbial catâŠFor images:
alias diclean='docker images | grep '\''<none>'\'' | grep -P '\''[1234567890abcdef]{12}'\'' -o | xargs -L1 docker rmi'
For containers (my data-only containers and named running containers all end in _run, _data or _config):
alias dclean='docker ps -a | grep -v '\''CONTAINER\|_config\|_data\|_run'\'' | cut -c-12 | xargs docker rm'
The ugly escaping/quoting is to make the alias work correctly. You wonât need as much just running the commands directly.
Scott
Appreciate it, my concern though is that there is no âtree of imagesâ aware mechanism out there. It would be super nice if you could just docker rm
all the containers no longer in use and then, docker rmi --unused
to clean everything that is left up.
Raised a feature req here:
and as an example of why simple
things have not been coded yet
I wrote a shell script to remove exited containers, untagged images and images not associated to any container, hope it helps.
I was part of creating a tool for this issue, hope it brings value . here is an example video on how to use it in a coreos cluster:
GitHub repo (tool is generic, this is one of the use-cases):
Where is the actual code that does the cleanup?
So iâts a workflow engine, it has itâs own DSL (Yaml based , like Docker compose)âŠ
The workflow engine codebase is here : https://github.com/CloudSlang/cloud-slang
The Yaml based âflowsâ files are here (look for âclear_unused_docker_images.slâ file as the parent flow) :
A more elaborate tutorial on this in DigitalOcean blog : https://www.digitalocean.com/community/tutorials/how-to-clean-up-your-docker-environment-using-cloudslang-on-a-coreos-cluster-2
I like to use this script from Spotify: GitHub - spotify/docker-gc: INACTIVE: Docker garbage collection of containers and images
From the README:
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.
Although docker normally prevents removal of images that are in use by containers, we take extra care to not remove any image tags (e.g., ubuntu:14.04, busybox, etc) that are in use by containers. A naive
docker rmi $(docker images -q)
will leave images stripped of all tags, forcing docker to re-pull the repositories when starting new containers even though the images themselves are still on disk.
Awesome, @solomon / @jess is this scriptâs functionality something you would consider including in Docker proper?
Another implementation of the same docker-gc
idea is https://github.com/yelp/docker-custodian
I, along with one of my good friends, built a nice wrapper for some common Docker cleanup commands, I would love for you to check it out and see if it helps! https://github.com/ZZROTDesign/docker-clean
thanks that was helpful
Here is the final answer:
docker images | grep " [months|days|weeks]* ago" | awk '{print $3}' | xargs docker rmi
This version is more powerful/dangerous, use with caution: (only use it if you donât care, because itâs likely to remove everything)
docker images -a | grep " [hours|months|days|weeks]* ago" | awk '{print $3}' | xargs docker rmi -f