docker-distribution API接口
API概述
docker-distribution提供了完整的RESTful API接口,用于管理镜像仓库。通过这些API,可以实现镜像的上传、下载、删除等操作,方便与其他系统集成。
API基础URL:http://registry.example.com:5000/v2/
常用API端点
1. 查看API版本
bash
# 检查registry是否支持v2 API
curl -i http://localhost:5000/v2/2. 列出所有仓库
bash
# 获取所有仓库列表
curl -i http://localhost:5000/v2/_catalog3. 列出仓库标签
bash
# 获取指定仓库的所有标签
curl -i http://localhost:5000/v2/<repo>/tags/list示例:
bash
curl -i http://localhost:5000/v2/nginx/tags/list4. 获取镜像manifest
bash
# 获取指定镜像的manifest信息
curl -i \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
http://localhost:5000/v2/<repo>/manifests/<tag>示例:
bash
curl -i \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
http://localhost:5000/v2/nginx/manifests/latest5. 删除镜像标签
bash
# 1. 先获取镜像的digest
DIGEST=$(curl -s -D - \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
http://localhost:5000/v2/nginx/manifests/latest \
| grep -i docker-content-digest | awk '{print $2}' | tr -d '\r')
# 2. 使用digest删除镜像
curl -i -X DELETE \
http://localhost:5000/v2/nginx/manifests/${DIGEST}6. 获取镜像层信息
bash
# 获取指定镜像层的信息
curl -i http://localhost:5000/v2/<repo>/blobs/<digest>7. 检查镜像层是否存在
bash
# 检查指定digest的镜像层是否存在
curl -i -X HEAD http://localhost:5000/v2/<repo>/blobs/<digest>API认证
如果registry配置了认证,需要先获取认证token:
bash
# 1. 获取认证挑战
CHALLENGE=$(curl -i http://localhost:5000/v2/nginx/manifests/latest 2>&1 | grep -i www-authenticate)
# 2. 解析认证信息(示例,实际需要根据挑战内容调整)
REALM=$(echo $CHALLENGE | grep -o 'realm="[^"]*"' | cut -d '"' -f 2)
SERVICE=$(echo $CHALLENGE | grep -o 'service="[^"]*"' | cut -d '"' -f 2)
SCOPE=$(echo $CHALLENGE | grep -o 'scope="[^"]*"' | cut -d '"' -f 2)
# 3. 获取认证token
TOKEN=$(curl -s -u "admin:password" \
"${REALM}?service=${SERVICE}&scope=${SCOPE}" | jq -r .token)
# 4. 使用token访问API
curl -i -H "Authorization: Bearer ${TOKEN}" \
http://localhost:5000/v2/nginx/manifests/latestAPI示例:使用Python脚本管理镜像
python
import requests
import json
# 配置
REGISTRY_URL = "http://localhost:5000"
REPO_NAME = "nginx"
# 列出所有仓库
def list_repos():
url = f"{REGISTRY_URL}/v2/_catalog"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return f"Error: {response.status_code}"
# 列出仓库标签
def list_tags(repo):
url = f"{REGISTRY_URL}/v2/{repo}/tags/list"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return f"Error: {response.status_code}"
# 获取镜像digest
def get_digest(repo, tag):
url = f"{REGISTRY_URL}/v2/{repo}/manifests/{tag}"
headers = {
"Accept": "application/vnd.docker.distribution.manifest.v2+json"
}
response = requests.head(url, headers=headers)
if response.status_code == 200:
return response.headers.get("Docker-Content-Digest")
else:
return f"Error: {response.status_code}"
# 删除镜像
def delete_image(repo, digest):
url = f"{REGISTRY_URL}/v2/{repo}/manifests/{digest}"
response = requests.delete(url)
return response.status_code
# 测试示例
if __name__ == "__main__":
print("列出所有仓库:")
print(json.dumps(list_repos(), indent=2))
print(f"\n列出{REPO_NAME}的标签:")
print(json.dumps(list_tags(REPO_NAME), indent=2))
digest = get_digest(REPO_NAME, "latest")
print(f"\n{REPO_NAME}:latest的digest:")
print(digest)API注意事项
- 认证要求:如果registry配置了认证,所有API请求都需要携带有效的认证token
- 镜像删除:删除镜像需要先获取镜像的digest,然后使用digest进行删除
- API版本:确保使用的是v2 API,v1 API已经被弃用
- 权限控制:根据配置的访问控制策略,某些API可能需要特定权限
- 速率限制:生产环境中建议配置API速率限制,防止滥用
