How to cleanup your docker registry

3 minute read

After running our own private docker registry for a while, we identified the problem that it never forgets its images.

Especially when facing release deadlines, our CI/CD pipelines produce lots of images that at the end of the day claim a lot of disk space on our registry server.

Since we found no standard solution to clean up the docker registry in a flexible way, we wrote our own docker-registry cleanup script a couple of years ago.

One of our main goals was to clean up as much as possible, but making sure to not delete important images or images which are currently in use.

Therefore, the script is highly configurable, using pattern matching to identify deletion candidates baed on image names and tags.

Here is an example configuration:

registry: http://localhost:5000
keep_count: 5
repositories:
    my-docker-image:
        cleanup:
            - "b-.*"

This would inspect the image my-docker-image on the docker repository http://localhost:5000.

Tags matching the regular expression /b-.*/ are considered for cleanup. We always keep all images pointed to by the last five build tags, as well as any images pointed to by other tags not listed in the cleanup section.

This enables us to keep any images that are currently in test or production by tagging those images with additinal tags, e.g., test-#{stage} or prod-#{servername}.

The cleanup is triggered every night in the housekeeping sction of our jenkins installation with a small wrapper script that triggers the registry garbage collector after cleaning up the registry.

We’re glad to share the code with the community at Vacuum Docker Registry on Github.