12

Is there an elegant way or a best practice when it comes to deleting old images from the Docker registry?

I see a lot of requests/issues here: https://github.com/docker/docker-registry/labels/delete, but didn't find a good/popular solution for it.

So, is there a tool or a technique which would help me do that?

Also, is there any best practices which you follow while doing it?

  • 2
    Are you asking how to clean the registry? (Which is the link and question you provide), but the accepted answer is not cleaning the registry, but cleaning your local docker. – dalore Apr 19 '18 at 10:54
9

I've had good luck with Spotify/docker-gc. It can be run from cron or as a docker container.

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.
  • 2
    At first glance, it seems to be, behind all the bash stuff, more or less a subset of the one-liners from @rombob 's answer. Is that true, or is there some "intelligence" in docker-gc that I am missing? – AnoE Nov 16 '17 at 14:39
  • 1
    Op links and asks a question on how to clean from the registry, but docker-gc doesn't clean from the registry, it just cleans from your local machine. I guess OP was really asking how to clean up locally – dalore Apr 19 '18 at 10:53
  • Docker-gc's page now recommends to use docker system prune instead. But I didn't find how to exclude some images from removing with Docker's prune. It there a way to exclude some images with docker system prune? – Alexander Artemenko Sep 4 at 9:24
8

Can't call it's the best practice but this is what we use triggered by cron, happy to see better suggestions.

echo "safely removing untagged images"
docker rmi $(docker images | awk '/<none>/{print $3}')

echo "safely removing stopped containers"
docker rm $(docker ps -a -q)

echo "safely removing old containers"
docker ps -a | awk '/weeks ago|months ago|days ago/{print $1}' | xargs --no-run-if-empty docker rm

echo "safely removing old images"
docker images | awk '/weeks ago|months ago|days ago/{print $3}' | xargs --no-run-if-empty docker rmi

echo "safely removing old volumes, custom rebuild of martin/docker-cleanup-volumes image"
docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(readlink -f /var/lib/docker):/var/lib/docker --rm example/docker-cleanup-volumes

echo "native cleanup Docker => 12"
docker system prune -f
  • Please don't chain grep and awk, do the same filter just before the awk command ` regex { command }` – Tensibai Mar 27 '17 at 19:14
  • 1
    @Tensibai anything to make AWK wizards happy :) – rombob Mar 27 '17 at 20:32
  • Just in case, that's not just pedantic , you save process spawn and a large bunch of context changes :) It doesn't change much here, but any occasion to advocate for it is good :) – Tensibai Mar 27 '17 at 20:53
  • @Tensibai definitely, good habits pay off with time, spawns cost us time and money :) – rombob Mar 27 '17 at 20:57
  • Specially when you parse/format billions lines log files to prepare for R input :) Here I don't expect more than a few dozen lines. – Tensibai Mar 27 '17 at 20:59
3

On my local machine (mac) I have a little script I found that I run periodically which cleans all the excess images up clean-docker-for-mac.sh

For my servers I run meltwater/docker-cleanup which periodically cleans up exited containers and removes images and volumes that aren't in use.

I use a lot of different Docker images for my work on Codemason that scripts like these are necessity for me. Enjoy!

0

docker system prune --volumes

Is the way to do this since v17. Docs here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.