You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用JFrog Rest API复制Docker镜像遇405错误求解决

解决Artifactory Docker镜像复制405 Method Not Allowed错误

问题原因

你使用的/api/docker/{repo}/{image}/{version}/promote端点不支持POST请求,从响应头的Allow: HEAD,GET,OPTIONS可以明确看出,这个接口仅允许这三种请求方法,因此POST请求会返回405错误。

正确解决方案

Artifactory复制Docker镜像需要使用官方通用的复制REST API,路径为/api/copy,请求方法为POST。以下是修正后的代码:

import json
import urllib3
import logging

URL = "https://repo.url/artifactory"
REPOSITORY = "repo-with-all-images"
IMAGE_NAME = "image-name"
VERSION = "latest"
TARGET_REPOSITORY = "repo-to-paste-images"

# 构造Docker镜像在Artifactory中的完整存储路径
# Docker镜像在Artifactory的存储格式为:docker/{仓库名}/v2/{镜像名}/manifests/{版本}
source_path = f"docker/{REPOSITORY}/v2/{IMAGE_NAME}/manifests/{VERSION}"
target_path = f"docker/{TARGET_REPOSITORY}/v2/{IMAGE_NAME}/manifests/{VERSION}"

url = f"{URL}/api/copy"

payload = {
    "from": source_path,
    "to": target_path,
    "copy": True,
    # 开启递归复制,确保镜像关联的所有层都被复制
    "recursive": True
}

# 必须添加Content-Type头,否则Artifactory无法解析JSON请求体
headers = {
    "Authorization": token,
    "Content-Type": "application/json"
}

http = urllib3.PoolManager()
try:
    response = http.request(
        method="POST",
        url=url,
        body=json.dumps(payload),
        headers=headers
    )
    if response.status == 200:
        logging.info(f"镜像{IMAGE_NAME}:{VERSION}复制成功")
    else:
        logging.error(f"复制失败,状态码:{response.status}")
        logging.error(f"错误详情:{response.data.decode('utf-8')}")
except Exception as e:
    logging.error(f"请求异常:{str(e)}")

关键注意事项

  • api/copy是Artifactory官方推荐的跨仓库资源复制接口,支持完整复制Docker镜像(包括manifest文件和所有关联镜像层)
  • 必须指定Content-Type: application/json请求头,否则Artifactory无法正确解析JSON格式的请求体
  • recursive: True参数是必要的,仅复制manifest文件会导致目标仓库中的镜像无法正常使用
  • 确保你的授权令牌拥有源仓库的读取权限和目标仓库的写入权限

内容的提问来源于stack exchange,提问作者Fabian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 14:45:31