docker 推送时的权限问题

Permission issues while docker push

我正在尝试将我的 docker 映像推送到 google 容器映像注册表,但收到一条错误消息,提示我没有执行此操作所需的权限。

我已经尝试过 gcloud auth configure-docker 但它对我不起作用。

我首先使用以下方法构建图像: docker build -t gcr.io/trynew/hello-world-image:v1 .

然后我尝试附加一个标签并推送它: docker 推送 gcr.io/trynew/hello-world-image:v1

这是我的输出:

    The push refers to repository [gcr.io/trynew/hello-world-image]

    e62774cdb1c2: Preparing 
    0f6265b750f3: Preparing 
    f82351274ce3: Preparing 
    31a16430afc8: Preparing 
    67298499a3ed: Preparing 
    62d5f39c8fe4: Waiting 
    9f8566ee5135: Waiting 
    unauthorized: You don't have the needed permissions to perform this
    operation, and you may have invalid credentials. 
    To authenticate your request, follow the steps in: 
https://cloud.google.com/container-registry/docs/advanced-authentication

Google 云服务有具体的信息如何给docker 推送权限,这是你应该首先看看我认为https://cloud.google.com/container-registry/docs/access-control

检查您是否有足够的权限后,您应该继续进行身份验证,例如:

gcloud auth configure-docker

在此处查看更多内容:https://cloud.google.com/container-registry/docs/pushing-and-pulling

为了能够将图像推送到私有注册表,您需要做两件事:API 访问范围使用注册表

对于API Access Scopes (https://cloud.google.com/container-registry/docs/using-with-google-cloud-platform)我们可以在官方文档中阅读:

对于 GKE:

By default, new Google Kubernetes Engine clusters are created with read-only permissions for Storage buckets. To set the read-write storage scope when creating a Google Kubernetes Engine cluster, use the --scopes option.

GCE:

By default, a Compute Engine VM has the read-only access scope configured for storage buckets. To push private Docker images, your instance must have read-write storage access scope configured as described in Access scopes.

因此,首先,验证您的 GKE 集群或 GCE 实例是否确实设置了正确的范围。

接下来是向注册表进行身份验证:

a) 如果你使用的是基于Linux的镜像,你需要使用“gcloud auth configure-docker”(https://cloud.google.com/container-registry/docs/advanced-authentication).

b) 对于 Container-Optimized OS (COS),命令是“docker-credential-gcr configure-docker ” (https://cloud.google.com/container-optimized-os/docs/how-to/run-container-instance#accessing_private_google_container_registry)

如前所述1 there appears to be a bug in the Linux version of cloud sdk where authentication fails using the standard authentication method (gcloud auth configure-docker). Instead, create a JSON keyfile per this,这很有效。

为了防止其他人用头撞墙,我的 PIA VPN 导致了这种行为。

"unauthorized: You don't have the needed permissions to perform this operation, and you may have invalid credentials. To authenticate your request, follow the steps in: https://cloud.google.com/container-registry/docs/advanced-authentication"

关闭我的 VPN,它工作正常。重新打开它,它又坏了。

如果您 运行宁 docker 为根用户(即使用 sudo docker),请确保将身份验证配置为根用户。您可以 运行 例如:

sudo -s
gcloud auth login
gcloud auth configure-docker

...将在 /root/.docker/config.json.

下创建(或更新)一个文件

gcloud auth loginroot 有任何安全隐患吗?请在评论中告诉我。)

Windows / 功率shell

我在 Windows 上遇到这个错误,当我在 google cloud shell 是我安装SDK时打开的

解决方法很简单:

开始新的powershell window to 运行 docker push 运行ning gcloud auth configure-docker 命令后.

确保您也激活了注册表:

gcloud services enable containerregistry.googleapis.com

而且 Google 有跳转到默认帐户的倾向,这可能是也可能不是您想要的。如果您在浏览器中打开任何链接,请确保您使用的是正确的 Google 帐户。

我还不确定发生了什么,因为我是 docker 的新手,但在启动新的 Powershell 实例时,有些东西得到了刷新。

我仍然无法让 gcloud auth configure-docker 助手工作。所做的是使用 access token 进行身份验证,就像这样

gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://HOSTNAME 

其中主机名是 gcr.ious.gcr.ioeu.gcr.ioasia.gcr.io。 (一定要包含https://,否则无效)

您可以查看 print-access-token here 的选项。

这是唯一对我有用的方法。我在 kubernetes/kompose Github 问题中找到它。

  1. Remove the credsStore key in ~/.docker/config.json

This will force docker to write the auth into the json when you use docker login. You can't untick Securely store Docker logins in macOS keychain in the docker desktop any more -- and the current credStore is no longer macOS keychain, it's desktop.

  1. gcloud auth login Auth with gcloud (just to be explicit)
  2. gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://eu.gcr.io

You should see this:

WARNING! Your password will be stored unencrypted in /Users/andrew/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

来源:https://github.com/kubernetes/kompose/issues/1043#issuecomment-609019141

修复如下:运行 gcloud auth login(浏览器将打开并允许您进行身份验证)然后 运行 gcloud auth configure-docker 和 select Y - 然后重做推送。它应该像魅力一样工作。

我在 Linux 环境中也遇到了同样的问题。所以我只是将 Docker 设置为 运行 作为 non-root 用户,(https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user),并且它有效。

在我的例子中,DOCKER_CONFIG env 变量定义了一个无效值(未指向 docker 配置 json)。