Docker Registry 2.0 - how to delete unused images?

前端 未结 8 649
难免孤独
难免孤独 2020-12-12 11:11

We updated our private docker registry to the official Registry 2.0. This version can now delete docker images identified by a hashtag (see https://docs.docker.com/registry/

相关标签:
8条回答
  • 2020-12-12 11:51

    This is doable, although ugly. You need to be running (I think) registry 2.3 or greater, and have enabled deleting (REGISTRY_STORAGE_DELETE_ENABLED=True env var or equivalent). The example commands below assume a local filestore in /srv/docker-registry, but I'd be surprised if something equivalent couldn't be cooked up for other storage backends.

    For each repository you wish to tidy up, you need to enumerate the digest references that are no longer required. The easiest way to do this is per-tag, using latest as an example in this case, you'd do something like:

    ls -1tr /srv/docker-registry/v2/repositories/<repo>/_manifests/tags/latest/index/sha256 \
    | tail -n +3
    

    This will list all but the three most recent digests pushed to the latest tag. Alternately, if you don't care about tags so much, but just want to keep the last few references pushed, you can do:

    ls -1t /srv/docker-registry/v2/repositories/<repo>/_manifests/revisions/sha256 \
    | tail -n +3
    

    Then, you just delete the references you don't need:

    for hash in $(ls -1t /srv/docker-registry/v2/repositories/<repo>/_manifests/tags/latest/index/sha256 | tail -n +3); do
      curl -X DELETE https://registry:5000/v2/<repo>/manifests/sha256:$hash
    done
    

    Finally, you need to do a GC run, because the registry implements "soft deletes", which doesn't actually delete anything, it just makes it unavailable:

    docker exec docker-registry /bin/registry \
      garbage-collect /path/to/config.yml
    

    Yes, this is all messy as hell, grovelling around in the backend storage, because there's no API method to enumerate all digests associated with a given tag, but that's the way the cookie crumbles.

    0 讨论(0)
  • 2020-12-12 11:55

    I was looking for the same functionality in the registry v2 api but only found soft deleting which is not what I was looking for. While researching I found the Github project delete-docker-registry-image which removes the actual files from the mounted volume via a bash script. Not tested it maybe useful...

    0 讨论(0)
提交回复
热议问题