New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docker rmi --unused #9054
Comments
Docker rmi with dangling=true unsafe while images are being pulled #8926 |
I think |
@cpuguy83 sort of, we do it but it feels risky, hoping for a warning-less solution. |
I am in a situation where I can't remove a certain layer, because docker claims it's a dependency of some other layers, but refused to remove it, because it's not there. No running containers...
Remove dangling...
Check a failed ID:
Remove that one...
OK failed, check the dependency - it's not there?!
Force remove now...
Force remove the dependency...
Stuck here since the last both forced commands don't work. |
@schmunk42
|
Thanks, but I always get stuck on the image mentioned above
|
@schmunk42 this sounds similar to #9056 (which was solve by #9204) and should be in the upcoming 1.4 release |
Yeah, looks the same. PS: Is it possible to test master, I looked through the docs, but didn't find instructions on first sight. |
Yes, you can get master builds at https://master.dockerproject.com saves having to build yourself :) Note that the master builds are not what will be in 1.4. To get those, look here #9345 |
Hi,
I know it is not perfect (I’m ignoring images dependencies) , but does it have some value? Another thing we can add, is look at the events of past X time, parse from it all image names, |
I suppose you mean that Then a How does that sound? |
I was part of an effort to impl it as an external project in the end, you can see a short blog on that here : https://www.digitalocean.com/community/tutorials/how-to-clean-up-your-docker-environment-using-cloudslang-on-a-coreos-cluster-2 hope it helps... |
filters could be extended to support unused |
This is how I remove unused images, maybe it is of help to others: #!/bin/bash
# this script requires bash version 4
#delete all stopped containers
docker rm $(docker ps -f status=exited -q)
# get list of (tagged) images that are used by running containers
declare -A usedimages
for i in $(docker ps | awk '{ print $2; }' | grep -v "^ID$") ; do ID=$(docker inspect --format="{{.Id}}" $i) ; usedimages[$ID]=$i ; done
for i in "${!usedimages[@]}"
do
echo "key : $i"
echo "value: ${usedimages[$i]}"
done
# delete all unused (tagged) images
for i in $(docker images --no-trunc -q | sort -u)
do
if [[ ${usedimages[$i]}x != "x" ]] ; then
echo "image is used: $i"
echo "by ${usedimages[$i]}"
else
echo "image is not used: $i"
docker rmi $i
fi
done |
@wgerlach Thanks for the tip. Don´t you get an "value too great for base" error within the |
@w0mbat Are you using bash v4 ? |
@quyen91 please don't leave random comments on all issues that are slightly related, please read the discussion, and you'll see that alternatives are already mentioned. Also, this is not related to docker-machine |
@thaJeztah i tried so many solution but not woking, this works for me so i only want share it. |
I do it as follows based on the prev. rec. in the thread:
|
This would be very useful, after several years of running thousands of docker images since 2013 - cleaning up is still a massive pain in the ass, usually we find formatting the whole filesystem and pulling everything again is by far the easiest way but absolutely not ideal. |
@sammcj Agreed. Especially for swarm, where nodes often pull images they don't use at all. My two most used docker-docker-cleanup tools: |
Possible inspiration: rkt has a "garbage collection" system that can be called from a cron job and clean stuff that hasn't been used for a while (with a configurable grace period): https://coreos.com/rkt/docs/latest/devel/pod-lifecycle.html |
👍 for better tooling support here. I use this command currently (in case anyone is interested) |
I've gone a little deeper... Here is my 1-liner for removing containers which have not actively been used in more than 13 days:
It DOES have a dependencies on the
Meaning delete all images that have not actively been run in more than 14 days. Or perhaps:
Which would delete all containers which haven't been used since the specified timestamp. |
Relates to #22871 |
Data management features are merged in #26108. Note that it's the first incarnation; additional enhancements can be added in future. |
@Mickep76 I disagree with your solution. The first part of your script will remove data containers, which are basically in an "Exited" status but are needed. |
You're right considering if you use data containers in a state that is not running. I believe the correct approach is what is proposed by @sungardas-cloud-devops --last-used=-14. |
I raised this discussion here
https://forums.docker.com/t/command-to-remove-all-unused-images/20
To recap we often find ourselves with systems saturated with unused images, it would be super awesome if we had a simple way of cleaning up.
Eg:
This is tricky to script as you need to walk through dependencies, the workaround is just to ignore the errors from
rmi
, but it feels unclean.The text was updated successfully, but these errors were encountered: