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

私有Docker Registry拉取缓存模式下返回无效授权凭证问题求助

解决Docker拉取缓存Registry的"invalid authorization credential"错误

我之前踩过一模一样的坑,咱们来拆解问题根源,一步步解决:

问题根源

你的Registry同时扮演了两个角色:私有镜像仓库(允许用户登录推拉本地镜像)和上游公有镜像的拉取缓存。当客户端拉取缓存中没有的公有镜像时,会把登录你的私有Registry的认证信息传给Registry,而Registry又把这个信息转发给上游(比如Docker Hub)——上游根本不认识你的本地账号,自然就返回"invalid authorization credential"错误了。

解决方案

1. 调整Registry的配置文件,区分认证范围和代理策略

找到你的Registry配置文件(通常是config.yml,如果是用Docker运行的,可能是挂载的本地文件),修改成类似下面的配置:

version: 0.1
log:
  fields:
    service: registry
storage:
  cache:
    blobdescriptor: inmemory
  filesystem:
    rootdirectory: /var/lib/registry
http:
  addr: :5000
  headers:
    X-Content-Type-Options: [nosniff]
# 配置上游代理,指向Docker Hub官方仓库
proxy:
  remoteurl: https://registry-1.docker.io
  # 可选:如果需要拉取Docker Hub的私有镜像,填写你的Docker Hub账号密码
  # username: your-dockerhub-username
  # password: your-dockerhub-password
# 认证配置,只对非公有镜像路径生效
auth:
  htpasswd:
    realm: basic-realm
    path: /etc/docker/registry/htpasswd # 你的本地认证文件路径
middleware:
  repository:
    - name: auth
      options:
        realms: basic-realm
        service: docker.example.local:5000
      # 关键:只对非library开头的仓库要求认证(library是Docker Hub公有镜像的默认前缀)
      match: "^(?!library/).*$"

这个配置的核心是通过match规则,让认证只作用于你的私有镜像(比如myImage这类非library前缀的仓库),拉取公有镜像时跳过认证,Registry会用自己的代理配置去上游拉取。

2. 重启Registry服务

如果是用Docker运行的Registry,重新启动容器:

docker restart registry-container-name

3. 验证客户端配置和操作

确保客户端的daemon.json配置正确,然后重启Docker服务:

systemctl restart docker
  • 测试拉取公有镜像:docker pull nginx,这时候Registry会自动去Docker Hub拉取并缓存,应该不会再报授权错误。
  • 测试拉取私有镜像:先登录docker login -u username -p secret docker.example.local:5000,然后docker pull docker.example.local:5000/myImage,应该能正常拉取。

额外提示

如果你的Registry使用的是HTTP而不是HTTPS,记得在客户端的daemon.json里添加insecure-registries配置,避免出现证书相关的错误:

{
  "registry-mirrors": ["https://docker.example.local:5000"],
  "insecure-registries": ["docker.example.local:5000"]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:41:55