How remove docker image/tag from Docker Hub

本文介绍了一种利用lumir/remove-dockerhub-tag Docker镜像批量删除DockerHub上特定镜像标签的方法,通过Python脚本调用DockerHub API实现自动化操作,节省了手动删除的时间和精力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qwfys200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值