Giter Site home page Giter Site logo

burnettk / delete-docker-registry-image Goto Github PK

View Code? Open in Web Editor NEW
713.0 713.0 192.0 59 KB

If you are running a private v2 docker registry, and you are storing your data on disk, running this script from the machine where the data lives will let you fully delete an image or tag

License: MIT License

Shell 32.20% Python 67.80%

delete-docker-registry-image's People

Contributors

abulimov avatar burnettk avatar charnet1019 avatar emtezogaf avatar rewiko avatar rtino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

delete-docker-registry-image's Issues

Delete orphan manifests and content

I would like to delete all unreferenced manifests -- those without a tag.

90% of my disk space is eaten up by such things, because the continuous build system pushes artifacts to the latest tag (continuous delivery). This means that stale content has no tag at all, and no way to delete it by tag name. The GC feature in the repo is useless as well, as it does not consider such content an orphan.

Usage of --untagged results in error (docker-registry 2.3)

Is this supposed to be compatible with docker-registry 2.3? I've just updated to 2.3 and then discovered the --untagged option, so I gave it a try, but it terminates with an exception:

Traceback (most recent call last):
  File "/usr/local/bin/delete_docker_registry_image", line 339, in <module>
    main()
  File "/usr/local/bin/delete_docker_registry_image", line 325, in main
    cleaner.delete_untagged(image)
  File "/usr/local/bin/delete_docker_registry_image", line 243, in delete_untagged
    layers_to_protect.extend(self._get_layers_from_blob(link))
  File "/usr/local/bin/delete_docker_registry_image", line 88, in _get_layers_from_blob
    return get_layers_from_blob(data_path)
  File "/usr/local/bin/delete_docker_registry_image", line 43, in get_layers_from_blob
    return set([entry["blobSum"].split(":")[1] for entry in data["fsLayers"]])
KeyError: 'fsLayers'

Deletion of complete repositories seem to work fine.
This is an example of the data-files on which the script is failing:

{
   "schemaVersion": 2,
   "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
   "config": {
      "mediaType": "application/octet-stream",
      "size": 4075,
      "digest": "sha256:13578f6a9b9ced5bf075bc7a8a68d1953af824642b8e07a5a48b8a3c8b536d5e"
   },
   "layers": [
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 51367715,
         "digest": "sha256:fdd5d7827f33ef075f45262a0f74ac96ec8a5e687faeb40135319764963dcb42"
      },
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 32,
         "digest": "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"
      },
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 32,
         "digest": "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"
      },
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 32,
         "digest": "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"
      },
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 4089549,
         "digest": "sha256:e04488adab39a4de114388aaf9aa6d911612d77e2fa5b0e64377a7c04a18c597"
      },
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 195,
         "digest": "sha256:2af76486f8b8752f4d7a3e1257811e68be22fd930f28c1697eb725c564d538e8"
      },
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 32,
         "digest": "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"
      },
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 32,
         "digest": "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"
      }
   ]
}

Maybe this is caused by some 2.3 layout change?

Registry with login/password

Hi,

First of all thanks for these tools !

I setup my local registry to use a password ( see below )
Does this mean I ca not use the delete options ?

docker run -d --name registry
-v /opt/registry:/opt/registry
-v /home/hn/auth:/auth
-e "REGISTRY_AUTH=htpasswd"
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm"
-e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd"
-p 443:5000 --restart always
--env-file /opt/registry/config/registry.env
registry:2

License

There is no license specified. Under what terms is this code released?

Remove unreferenced images

Maybe it is already possible, but it seems to me that only specific images can be deleted. I'd like to remove images that aren't tagged. This would allow me to "garbage collect" the registry.

KeyError: 'repositories'

Hey there,

i'm getting this error when running against a remote registry (version 2.5.1).

Traceback (most recent call last):
  File "clean_old_versions.py", line 182, in <module>
    main()
  File "clean_old_versions.py", line 114, in main
    repositories = response.json()["repositories"]
KeyError: 'repositories'

Maybe you can point me the right way?

This is my call:
python2.7 clean_old_versions.py --image '^testdummy/' --include '^feature-' -l 2 --registry-url https://fancy.url:8080 -U 'xxxx' -P 'yyyy' --dry-run --no_check_certificate

Regards

'No module named' request executing clean_old_versions.py

Hi,

I am executing the file ./clean_old_versions.py but, it can not import 'request' module.

Any suggestion?

Traceback (most recent call last):
  File "./clean_old_versions.py", line 7, in <module>
    import requests
ImportError: No module named requests

ValueError

[root@188 script]# ./clean_old_versions.py -i '^itlsatest/essql_' -l 2
Traceback (most recent call last):
File "./clean_old_versions.py", line 90, in
main()
File "./clean_old_versions.py", line 57, in main
repositories = response.json()["repositories"]
File "/usr/lib/python2.7/site-packages/requests/models.py", line 802, in json
return json.loads(self.text, *_kwargs)
File "/usr/lib64/python2.7/json/init.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Deleting everything

I went through and deleted all the docker images in my registry but after checking the file structure, there are still a bunch of files in the blob directory that don't seem to be tied to anything. I am wondering if i can just delete those, perhaps they are orphans? The problem is the orphan files take up around 27 GB of space and i need the space back. the files taking up space are all called "data".

Unable to set the "REGISTRY_DATA_DIR"

Hi,

I have a machine hosting the running docker registry and it is working fine except for deleting. So, I downloaded your script and gave it the proper permission.

Based on the configuration of my registry inside the docker-compose.yaml file, the volume in my container is set as follow:
REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /data

After running the registry, the above volume corresponds to the following path in my local machine /var/lib/docker/volumes/registry_registrydata/_data/docker/registry/v2/repositories
which contains the following directories:

  • blobs
  • repositories

Then, following your instructions, I set the REGISTRY_DATA_DIR to the above local path. i.e.:
/var/lib/docker/volumes/registry_registrydata/_data/docker/registry/v2/repositories

Now, when I run the following command:
delete_docker_registry_image --image project/ubuntu --dry-run

I get the following error:
No repositories directory found inside REGISTRY_DATA_DIR '/var/lib/docker/volumes/registry_registrydata/_data/docker/registry/v2/repositories'
what am I doing wrong?

Thank you

jq: error (at <stdin>:110): Cannot iterate over null (null)

Hello,
I have a registry v2 and docker engine v1.10.

Removing only a tag I recieve this error:
[root@dregistry01 v2]# delete_docker_registry_image --image gitlab:latest
jq: error (at <stdin>:110): Cannot iterate over null (null)

Looking the code I see this jq command
jq -r '.fsLayers | map(.blobSum)[]'

But the file where is trying to filter the data have this format:
.....
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 65687630,
"digest": "sha256:a64038a0eeaa782fedb5762a37742005751e88406836d67375c1d7a438f5940e" },

I tried changing the line with this and now seems that is working
jq -r '.layers | map(.digest)[]'

It may be possible that has changed the format of the blob files?
data.txt

Slow to delete a tag

Thanks for creating this tool to allow remove images from registry.

We have a 45gb large docker distribution v2 registry and delete a tag takes about 5 mins, this particular image only has about 150 tags.

Is anyone else also experiencing the similar performance issue?

Max retries exceeded with url: /v2/_catalog (Caused by <class 'socket.error'>: [Errno 111] Connection refused)

Hi,
I'm using Docker Version: 1.11.2.
I'm using the two scripts you offer. The first one (delete_docker_registry_image) works fine. After doing the export I'm able to delete repo's.
But when I try to execute the second script (clean_old_versions) it does not work.

I've my registry (secured with a selfsigned cert) and I pushed:

myreg.com/busybox/busy:1
myreg.com/busybox/busy:2
myreg.com/busybox/busy:3
myreg.com/busybox/busy:4
myreg.com/busybox/busy:latest

Now I try to delete all images except the 4 newest:
./clean_old_versions --image 'busybox/busy' --include '*' -l 4

But this gave me:

Traceback (most recent call last):
  File "./clean_old_versions", line 86, in <module>
    main()
  File "./clean_old_versions", line 52, in main
    auth=auth, verify=args.no_check_certificate)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 455, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 558, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 378, in send
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: /v2/_catalog (Caused by <class 'socket.error'>: [Errno 111] Connection refused)

Any help on this issue? Thanks

deletion error

./delete_docker_registry_image --image XXX/YYY:1.2.0-rc.1 -v

It works without error messages.

./delete_docker_registry_image --image XXX/YYY:1.1.0-rc.7 -v

And I see error

Traceback (most recent call last):
File "./delete_docker_registry_image", line 336, in
main()
File "./delete_docker_registry_image", line 325, in main
cleaner.delete_repository_tag(image, tag, force=args.force)
File "./delete_docker_registry_image", line 213, in delete_repository_tag
if self._layer_in_same_repo(repo, tag, layer):
File "./delete_docker_registry_image", line 165, in _layer_in_same_repo
layers = self._get_layers_from_blob(manifest)
File "./delete_docker_registry_image", line 85, in _get_layers_from_blob
return get_layers_from_blob(data_path)
File "./delete_docker_registry_image", line 38, in get_layers_from_blob
with open(path, "r") as blob:
IOError: [Errno 2] No such file or directory: '/mnt/registry-data/docker/registry/v2/blobs/sha256/9f/9fd95e11750dd5b53d8de7c2f5f77f122baf96fddba470f9085918fbb2bf5376/data'

Where 9fd95e11750dd5b53d8de7c2f5f77f122baf96fddba470f9085918fbb2bf5376 is a hash related to first tag 1.2.0-rc.1 -v

I think it happens because first command did't clean properly. And XX/YYY/_manifests/tags/1.2.0-rc.1 still not empty

root@ip-172-31-128-219:/mnt# grep -r 9fd95e11750dd5b53d8de7c2f5f77f122baf96fddba470f9085918fbb2bf5376 *
registry-data/docker/registry/v2/repositories/XXX/YYY/_manifests/tags/1.2.0-rc.1/current/link:sha256:9fd95e11750dd5b53d8de7c2f5f77f122baf96fddba470f9085918fbb2bf5376

Complete output can be found
https://gist.github.com/iaon/16e467e25b3c78234142

This issue exist only for python version of script, so I have to rollback to BASH revision.

Enable TLS verification

Since the script complains about missing TLS verification: Do you have any plans ideas on how to enable verfication ( i noticed that you explicitly turned verification off in requests lib).
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:791: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
As suggested by urllib3 there are some ways to achieve this.
=> Instead of forking this repo and implement this on our own, i thought maybe we find a solution with upstream (here) ;-)

Support deleting individual tags

I would love to use this to clean up old tags, but it doesn't look like there is a way to delete individual tags, only the entire image.

How to delete?can`t find REGISTRY_DATA_DIR.

Hi,
delete_docker_registry_image --image localhost:5000/cs/hello-world:8.0
CRITICAL [2016-03-02 21:26:01,938] No repository 'localhost:5000/cs/hello-world:8.0' found in repositories directory /var/lib/docker/repositories
I can`t find REGISTRY_DATA_DIR,pls help...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.