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

如何通过Registry API获取容器镜像最后拉取时间?

如何获取Docker镜像tag的最后拉取时间

标准Docker Registry V2 API的局限

标准的Docker Registry V2 API不提供获取镜像最后拉取时间的接口——拉取属于访问行为,默认不会被持久化到Registry的镜像元数据中。你之前调用的/v2/_catalog、/manifests、/blobs接口,都是获取镜像本身的存储元数据,和拉取记录无关。

可行解决方案

1. 解析Registry访问日志

Registry的拉取请求会被记录到容器日志中,你可以通过解析日志提取镜像tag的最后拉取时间:

  • 首先确保Registry容器的日志驱动为json-file(默认配置),拉取请求的日志条目会包含请求路径(含镜像名和tag)、时间戳。
  • 日志示例:
    172.17.0.1 - - [15/Oct/2024:12:34:56 +0000] "GET /v2/my-image/manifests/latest HTTP/1.1" 200 1234 "" "docker/24.0.6 go/go1.20.10 git-commit/1a79698 kernel/5.15.0-88-generic os/linux arch/amd64 UpstreamClient(Docker-Client/24.0.6 \\(linux\\))"
    
  • Python解析脚本示例:
    import json
    from datetime import datetime
    from collections import defaultdict
    
    # 替换为你的Registry容器日志文件路径
    log_path = "/var/lib/docker/containers/<registry-container-id>/<registry-container-id>-json.log"
    
    last_pull_records = defaultdict(lambda: datetime.min)
    
    with open(log_path, "r", encoding="utf-8") as log_file:
        for line in log_file:
            try:
                log_entry = json.loads(line.strip())
                msg = log_entry.get("message", "")
                # 匹配拉取镜像清单的请求(拉取镜像的核心步骤)
                if "GET /v2/" in msg and "/manifests/" in msg:
                    # 提取镜像名和tag
                    path_part = msg.split("GET /v2/")[1].split("HTTP/1.1")[0].strip()
                    image_tag = path_part.replace("/manifests/", ":")
                    # 解析时间戳
                    time_str = log_entry.get("time")
                    if time_str:
                        pull_time = datetime.fromisoformat(time_str.replace("Z", "+00:00"))
                        if pull_time > last_pull_records[image_tag]:
                            last_pull_records[image_tag] = pull_time
            except json.JSONDecodeError:
                continue
    
    # 输出结果
    for image_tag, pull_time in last_pull_records.items():
        if pull_time != datetime.min:
            print(f"镜像 {image_tag} 最后拉取时间: {pull_time.strftime('%Y-%m-%d %H:%M:%S UTC')}")
    
    如果是远程Registry,可通过docker logs <registry-container-name>导出日志内容,再传入脚本处理。

2. 使用带统计功能的Registry实现

如果使用Harbor、Quay这类增强型Registry,它们自带镜像访问统计功能,提供对应的API接口可直接查询最后拉取时间。比如Harbor的API可以查询镜像的拉取历史记录,从中提取最新时间。

3. 自定义Registry中间件

如果你控制Registry部署,可以添加第三方中间件(或自行开发),在每次拉取请求时将镜像tag、时间戳存入数据库(如Redis、PostgreSQL),再编写简单的查询接口获取最后拉取时间。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 23:40:30