How remove docker image/tag from Docker Hub

In daily development work, we often hope to quickly clean the problematic image from the docker hub, but manual operation is very time-consuming and laborious, so is there a good way? With this purpose, I checked a lot of information, and later found that a docker image named lumir/remove-dockerhub-tag can help us handle this work. Next, let’s show our friends how to use this tool.

e.g.

lwk@qwfys:~$ docker run --rm lumir/remove-dockerhub-tag --user qwfys --password ***** lanzhou/user-business:0.0.1.release
Unable to find image 'lumir/remove-dockerhub-tag:latest' locally
latest: Pulling from lumir/remove-dockerhub-tag
000eee12ec04: Pull complete 
ddc2d83f8229: Pull complete 
3ae1660fa0d9: Pull complete 
ef709117d3d3: Pull complete 
487a0421e8fa: Pull complete 
0567bf350aaf: Pull complete 
bd35eb6f3933: Pull complete 
0b4e338b89ca: Pull complete 
56ac5d14ba19: Pull complete 
Digest: sha256:9d5a167f76969cb22059f6accd191329856891cac2dcac5b2bfae84f746f1b4b
Status: Downloaded newer image for lumir/remove-dockerhub-tag:latest
removed: lanzhou/user-business:0.0.1.release
lwk@qwfys:~$

After an actual review, we found that the relevant work has indeed been completed. So, how does this tool work? With this question, let’s take a closer look.First, let’s go to its official website. On the docker hub, we found its official website.

在這裏插入圖片描述
在這裏插入圖片描述
Here, we see that it is done through a python script, so what does this python script do, we find it on the official website of github through the github link given on the official website.
在這裏插入圖片描述
On its github official website, we found this python script file named remove-dockerhub-tag.py, whose contents are as follows:

#!/usr/bin/python3.6
import requests
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument('--user', required=True, help='dockerhub username')
parser.add_argument('--password', required=True, help='dockerhub password')
parser.add_argument('image', nargs='+', help='org/image:tag')
args = parser.parse_args()

login = {"username": args.user, "password": args.password}
token = requests.post("https://hub.docker.com/v2/users/login/", data = login).json()['token']

p = re.compile('(.+)\/(.+):(.+)')
for image in args.image:
    m = p.match(image)
    if not m:
        print('invalid image indetifier "{}"'.format(image))
    else:
        org = m.group(1)
        name = m.group(2)
        tag = m.group(3)
        r = requests.delete(f'https://hub.docker.com/v2/repositories/{org}/{name}/tags/{tag}/', headers={"Authorization": f'JWT {token}'})
        if r.status_code == 204:
            print(f'removed: {image}')
        else:
            print(f'failed to remove "{image}": {r.text}')

Through the python script, we found that this gadget completed the corresponding work through the http api provided by docker hub.We also found that there are many such gadgets, and their principles are similar. For example, the two docker images named gliderlabs/dockerhub-tag and lazyfrosch/registry-cleanup are quite good. If you are interested, you can play with them.

Reference

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章