17

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?

Dawny33
  • 2,816
  • 3
  • 24
  • 62

4 Answers4

10

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.
Ahmed Elsabbahy
  • 471
  • 2
  • 4
9

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
rombob
  • 657
  • 5
  • 16
4

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!

bmagg
  • 236
  • 1
  • 2
0

docker system prune --volumes

Is the way to do this since v17. Docs here

Mbrevda
  • 101