r/docker•
stepstep

Cleaning up old Docker images

Hey folks, this might be a little niche, but in case it's helpful: I wrote a small program called Docuum (like "Docker" + "Vacuum") that cleans up old Docker images to prevent your hard disk from filling up. I run it on any machine that builds Docker images, such as continuous integration (CI) workers or my laptop. You tell it the maximum amount of space you want to use for storing Docker images (e.g., 10 GB), and it it deletes images as needed to fit in that space.

You might wonder what the point of Docuum is, since Docker has a built-in command for cleaning up old images: docker image prune --filter until=…. The key difference is in how they decide what images to delete. The built-in Docker command will delete the images with the oldest creation date, whereas Docuum will delete the images with the oldest last-use date. That means even if you frequently use an image (e.g., to create containers or build other images), the built-in Docker command will eventually delete it, which is undesirable (especially if it takes a long time to build/pull). It's better to keep an image that was created a long time ago but used every day rather than an image that was created last week and never used since.

Docuum is meant to be run continuously like a daemon or server, rather than as a one-off command like a cron job. Set it up once and forget it. Only Linux and macOS are supported for now. Feel free to ask questions, report bugs, and make feature requests via GitHub issues. Hope this is useful to at least some of you!

Posted Jan 16, 2020, 9:56 AM UTC

5113
Mr_Albal•

Shame you didn’t commit this change upstream.

11
stepstepOP•

I would have, except someone tried to already:

Unfortunately, we don't think this is something we absolutely need right now

4
danksobotka•

that was four years ago though tbf

2
alkrun•

Nice. This is one thing I definitely need to look into for my Kubernetes CI / Development cluster.

A couple feature requests:

  1. Running as a daemon is nice but it would also be nice to be able to do a one-time run so I can schedule it or just run it manually.

  2. I'm sure I'm not alone in wanting this in a docker image I can run. I'd suggest getting the docuum organization on docker hub and creating an image.

docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock docuum/docuum --onetime --limit 10GB

  1. Yaml or a Helm Chart for A Kubernetes daemon set would be awesome
6
systemdad•

I can second the request for running this in docker. I always try to run the absolute bare minimum on the docker hosts, and do what I need in privileged containers. This would be another great addition.

2
stepstepOP•

I hear you! This is now possible. The README has been updated with instructions for how to do this (see also my reply to u/alkrun).

2
stepstepOP•

For (2), this is now possible. To run Docuum in the foreground:

docker run \
  --init \
  --rm \
  --tty \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  --volume docuum:/root \
  stephanmisc/docuum --threshold '15 GB'

And to run it in the background:

docker run \
  --detach \
  --init \
  --rm \
  --volume /var/run/docker.sock:/var/run/docker.sock \
  --volume docuum:/root \
  stephanmisc/docuum --threshold '15 GB'

For (1), unfortunately that won't be possible since Docuum needs to constantly be running to consume Docker events to learn when images are used (since Docker unfortunately doesn't record last-used timestamps for Docker images).

For (3), I don't have an update for you right now, but it's a good idea.

2
[deleted]•

[deleted]

1
nindustries•

Will it skip images that are in use or do you support last-use-tresholds?

2
stepstepOP•

It will skip images that are currently in use by any containers.

2
bobbysteel•

Very useful thanks for sharing

1
NLZ13•

Thanks,

I've always used this one for cleaning up various docker resources https://github.com/meltwater/docker-cleanup

But it seems it isn't maintained anymore, so time to find something new ...

1
kylehqcom•

For my local (Mac) I use docker-clean found here with brew https://formulae.brew.sh/formula/docker-clean

Works well for my use case.

More complete readme at the source https://github.com/ZZROTDesign/docker-clean/blob/master/README.md

1
RedKomrad•

or docker system prune -f

-1