返回博客列表

Docker + Buildx + GHCR 发布踩坑速记(问题 1 / 2 / 3)

2026-01-29
2 min read
服务器运维

> 最近在二开,nextai-draw-nas 时候发布镜像时候,遇到的问题记录。 Docker + Buildx + GHCR 发布踩坑速记(问题 1 / 2 / 3) 问题 1:镜像不匹配(arm64 / amd64) 现象 根因 使用 构建并推送 实际只生成了 当前宿主机架构的单一镜像 在其他架构机器运行时触发不匹配 关键认知 → 单架构(取决于宿主机) → 多架构(Mani...

最近在二开,next-ai-draw-nas 时候发布镜像时候,遇到的问题记录。

Docker + Buildx + GHCR 发布踩坑速记(问题 1 / 2 / 3)

问题 1:镜像不匹配(arm64 / amd64)

现象

text
The requested image's platform (linux/arm64)
does not match the detected host platform (linux/amd64)

根因

  • 使用 docker build 构建并推送
  • 实际只生成了 当前宿主机架构的单一镜像
  • 在其他架构机器运行时触发不匹配

关键认知

  • docker build → 单架构(取决于宿主机)
  • docker buildx build → 多架构(Manifest)

正确方式

bash
docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t ghcr.io/xxx/xxx:latest \
  --push .

镜像没错,错的是把“单架构镜像”当成“多架构镜像”发布。


问题 2:npm ci 构建阶段失败(网络问题)

现象

text
npm ERR! code ECONNRESET
npm ERR! network aborted

失败点固定在 Dockerfile:

dockerfile
RUN npm ci

根因

  • Docker build / buildx 中使用默认 npm registry
  • 容器内网络不稳定,arm64 构建更容易失败
  • 宿主机 npm 配置不会自动生效

解决方案

在 Dockerfile 中显式指定 npm 源:

dockerfile
RUN npm config set registry https://registry.npmmirror.com \
 && npm ci

或使用 nrm 切换源。

  abm@localhost  ~  nrm ls
  npm ---------- https://registry.npmjs.org/
  yarn --------- https://registry.yarnpkg.com/
  tencent ------ https://mirrors.tencent.com/npm/
  cnpm --------- https://r.cnpmjs.org/
  taobao ------- https://registry.npmmirror.com/
  npmMirror ---- https://skimdb.npmjs.com/registry/
* huawei ------- https://repo.huaweicloud.com/repository/npm/

问题 3:推送 GHCR 失败(403 Forbidden)

现象

text
failed to fetch anonymous token
403 Forbidden

发生在:

bash
docker buildx build --push

根因

  • buildx --push 在构建阶段直接推送
  • Docker 未登录 ghcr.io
  • GitHub Token 缺少 write:packages 权限
  • GHCR 不支持匿名 push(即使是 public)

解决方案

1️⃣ 创建 GitHub PAT(Classic),勾选:

text
write:packages
read:packages

2️⃣ 构建前登录 GHCR:

bash
echo TOKEN | docker login ghcr.io \
  -u USERNAME \
  --password-stdin

3️⃣ 再执行 buildx:

bash
docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t ghcr.io/用户名/镜像名:latest \
  --push .

一句话总总结

buildx 会把“架构、网络、鉴权”三个隐藏问题一次性放大暴露,但这正是它的价值。

返回博客列表
最后更新于 2026-01-29
想法或问题?在 GitHub Issue 下方参与讨论
去评论