registry仓库管理

Docker Registry HTTP API V2

获取镜像列表

$ curl http://10.0.20.112:5000/v2/_catalog
{"repositories":["myflask","nginx"]}

获取镜像tags

$ curl http://10.0.20.112:5000/v2/nginx/tags/list
{"name":"nginx","tags":["1.15.7"]}

获取镜像Etag

$ curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I http://10.0.20.112:5000/v2/nginx/manifests/1.15.7
HTTP/1.1 200 OK
Content-Length: 948
Content-Type: application/vnd.docker.distribution.manifest.v2+json
Docker-Content-Digest: sha256:87e9b6904b4286b8d41bba4461c0b736835fcc218f7ecbe5544b53fdd467189f
Docker-Distribution-Api-Version: registry/2.0
Etag: "sha256:87e9b6904b4286b8d41bba4461c0b736835fcc218f7ecbe5544b53fdd467189f"
X-Content-Type-Options: nosniff
Date: Mon, 03 Dec 2018 04:44:48 GMT

删除镜像

curl -X DELETE 'http://10.0.20.112:5000/v2/nginx/manifests/sha256:87e9b6904b4286b8d41bba4461c0b736835fcc218f7ecbe5544b53fdd467189f'
[root@swarm1 register]# curl http://10.0.20.112:5000/v2/nginx/tags/list
{"name":"nginx","tags":null}

获取仓库所有镜像脚本

# coding:utf-8

import requests
import json

repo_ip = '10.0.20.112'
repo_port = 5000


def get_images_names(repo_ip, repo_port):
    docker_images = []
    url = "http://" + repo_ip + ":" + str(repo_port) + "/v2/_catalog"
    res = requests.get(url).content.strip()
    res_dic = json.loads(res)
    images_type = res_dic['repositories']
    for i in images_type:
        url2 = "http://" + repo_ip + ":" + str(repo_port) + "/v2/" + str(i) + "/tags/list"
        res2 = requests.get(url2).content.strip()
        res_dic2 = json.loads(res2)
        name = res_dic2['name']
        tags = res_dic2['tags']
        if not isinstance(tags, list):
            continue
        for tag in tags:
            docker_name = str(repo_ip) + ":" + str(repo_port) + "/" + name + ":" + tag
            docker_images.append(docker_name)
            print docker_name
    return docker_images


get_images_names(repo_ip, repo_port)

删除仓库镜像脚本

# coding:utf-8
import requests
import sys
import json

host = '10.0.20.112'
port = 5000

if len(sys.argv) < 2:
    print("参数错误: None")
    sys.exit(1)


def get_images_names(repo_ip, repo_port):
    docker_images = []
    url = "http://" + repo_ip + ":" + str(repo_port) + "/v2/_catalog"
    res = requests.get(url).content.strip()
    res_dic = json.loads(res)
    images_type = res_dic['repositories']
    for i in images_type:
        url2 = "http://" + repo_ip + ":" + str(repo_port) + "/v2/" + str(i) + "/tags/list"
        res2 = requests.get(url2).content.strip()
        res_dic2 = json.loads(res2)
        name = res_dic2['name']
        tags = res_dic2['tags']
        if not isinstance(tags, list):
            continue
        for tag in tags:
            docker_name = str(repo_ip) + ":" + str(repo_port) + "/" + name + ":" + tag
            docker_images.append(docker_name)
            # print docker_name
    return docker_images


images_list = get_images_names(repo_ip=host, repo_port=port)
delete_images_list = sys.argv[1:]

for image in delete_images_list:
    if image not in images_list:
        print("{}: image not found".format(image))
        continue

    imgs = image.split('/')[-1]
    img, ver = imgs.split(':')[0], imgs.split(':')[1]
    headers = {
        "Accept": "application/vnd.docker.distribution.manifest.v2+json"
    }
    etag_url = "http://{}:{}/v2/{}/manifests/{}".format(host, port, img, ver)
    ret = requests.get(url=etag_url, headers=headers)
    if ret.status_code != requests.codes.ok:
        print("{}:{} 删除失败".format(img, ver))
        continue

    etag = ret.headers.get("Etag").replace("\"", "")
    delete_images_url = "http://{}:{}/v2/{}/manifests/{}".format(host, port, img, etag)
    del_ret = requests.delete(url=delete_images_url)
    if del_ret.ok:
        print("{}:{} 删除成功".format(img, ver))
    else:
        print("{}:{} 删除失败".format(img, ver))

results matching ""

    No results matching ""