如何使用curl从GitHub容器注册表下载离线安装包
从GHCR离线下载Homebrew wget包并安装的方法
前提
你已通过curl获取wget包的OCI索引manifest文件(manifest.json),且持有有效的GitHub Bearer Token。
步骤1:解析主manifest,获取对应架构的子manifest
GHCR上的Homebrew包为多架构镜像,主manifest(OCI index)包含不同操作系统/架构的子manifest。需根据目标离线机器的架构筛选对应子manifest:
- 用
jq工具解析主manifest(若未安装jq,可手动查看JSON内容定位对应字段):
替换jq '.manifests[] | select(.platform.os=="linux" and .platform.architecture=="amd64")' manifest.jsonlinux和amd64为目标机器的系统(如darwin)和架构(如arm64)。 - 从输出中提取
digest字段(格式为sha256:<哈希值>),这是子manifest的唯一标识。
步骤2:下载子manifest文件
使用curl获取对应子manifest,替换<子manifest digest>为上一步得到的值:
curl -o sub-manifest.json -H "Authorization: Bearer QQ==" -H 'Accept: application/vnd.oci.image.manifest.v1+json' https://ghcr.io/v2/homebrew/core/wget/manifests/<子manifest digest>
步骤3:解析子manifest,获取所有blob文件的标识
子manifest中的layers字段包含镜像的所有blob(即包的实际内容分片),提取每个blob的digest:
jq '.layers[] | .digest' sub-manifest.json
记录所有输出的sha256:<哈希值>,每个值对应一个需要下载的blob文件。
步骤4:下载所有blob文件
对每个blob的digest,执行以下curl命令下载(替换<blob digest>为实际值):
curl -o <blob digest>.tar.gz -H "Authorization: Bearer QQ==" https://ghcr.io/v2/homebrew/core/wget/blobs/<blob digest>
建议用digest作为文件名,避免文件混淆。
步骤5:离线安装准备与操作
- 将下载的
manifest.json、sub-manifest.json和所有blob文件打包,传输到离线机器。 - 在离线机器上,用容器工具(如podman或docker)加载镜像:
# 创建临时目录存放文件 mkdir temp-wget && cd temp-wget # 用skopeo组装镜像(无skopeo时,可手动构造镜像目录结构后用podman load) skopeo copy oci-archive:../wget-offline.tar docker-daemon:ghcr.io/homebrew/core/wget:1.24.5 - 启动临时容器,提取Homebrew bottle内容:
podman run --rm -v /tmp/wget-cellar:/mnt ghcr.io/homebrew/core/wget:1.24.5 cp -r /usr/local/Cellar/wget /mnt/ - 将提取的
wget目录复制到离线机器的Homebrew Cellar目录(通常为/usr/local/Cellar/),并更新Homebrew链接:cp -r /tmp/wget-cellar/wget /usr/local/Cellar/ brew link wget
注意事项
- Bearer Token可能过期,若下载时返回401错误,重新获取token:
用新token替换命令中的curl -s "https://ghcr.io/token?scope=repository:homebrew/core/wget:pull" | jq -r '.token'QQ==。 - 若目标机器无容器工具,可直接解压blob文件(blob为tar.gz格式),手动提取其中的Cellar目录内容。
内容的提问来源于stack exchange,提问作者Michael Altfield
相关产品推荐
相关产品推荐

