Trivy 镜像漏洞扫描器拉取 DB 频繁触发 GitHub API Rate Limit 排查
针对 CI/CD 中 Trivy 扫描容器漏洞时频发 fatal error: download error: failed to download vulnerability DB: rate limit exceeded 导致构建失败故障治理。
故障现象
流水线每天执行数百次容器镜像安全扫描,突然所有构建同时报错 failed to download vulnerability DB: API rate limit exceeded for 203.0.113.5 (But here's the good news: Authenticated requests get a higher rate limit) 并直接终止作业。
排查过程与诊断路径
检查 Trivy 命令行参数;查看 Trivy 下载 vulnerability database(trivy-db)的来源地址是否为 GitHub Releases 或 GitHub Packages(ghcr.io);检查当前 IP 是否为共享的出公网 NAT 代理 IP;检查 Trivy 缓存目录(~/.cache/trivy/db)是否在每次 CI 作业中被无脑清空。
根因分析
Trivy 默认每次运行都会检查并拉取最新的 CVE 漏洞数据库。其底层依赖 GitHub Container Registry(ghcr.io)或 GitHub API。未经认证的匿名请求在单个共有 IP 下共享严格的每小时 60 次 API 配额。若团队 CI 机器没有缓存机制且未配置 GitHub Token,高并发构建会在数分钟内将公共配额耗尽,导致所有流水线全部瘫痪。
解决方案与排障步骤
步骤 1: 注入 GitHub 认证 Token 提高配额
在 CI 环境变量中注入个人或组织 Token:
export GITHUB_TOKEN="ghp_xxxxxxxxxxxxxxxxxxxx"
trivy image --severity HIGH,CRITICAL myimage:latest
将每小时请求配额由 60 次提升至 5000 次。
步骤 2: 配置 CI 共享持久化缓存目录
在 GitLab CI/GitHub Actions 中缓存 trivy 数据目录:
cache:
paths:
- .trivycache/
script:
- trivy image --cache-dir .trivycache/ myimage:latest
避免每次构建都从公网全量下载数百兆的 SQLite 数据库。
步骤 3: 搭建内网 Trivy DB 镜像代理源
在企业私有制品库中配置定时镜像同步:
trivy image --db-repository registry.internal.com/aquasecurity/trivy-db myimage:latest
让全公司构建流量全部走内网高速通道。
验证方法与效果评估
Trivy 扫描耗时由 3 分钟缩短至 8 秒,彻底消除 API Rate Limit 报错,CI/CD 镜像漏洞扫描流水线实现 100% 稳定性。


