我正在尝试将docker映像推送到Amazon ECR注册表。我使用docker客户端docker版本1.9.1,构建a34a1d5。我使用aws ecr get-login -region us-east-1来获得docker登录信用。然后我成功登录这些学分如下:

docker login -u AWS -p XXXX -e none https://####.dkr.ecr.us-east-1.amazonaws.com
WARNING: login credentials saved in /Users/ar/.docker/config.json
Login Succeeded

但当我试图推动我的图像,我得到以下错误:

$ docker push ####.dkr.ecr.us-east-1.amazonaws.com/image:latest
The push refers to a repository [####.dkr.ecr.us-east-1.amazonaws.com/image] (len: 1)
bcff5e7e3c7c: Preparing 
Post https://####.dkr.ecr.us-east-1.amazonaws.com/v2/image/blobs/uploads/: no basic auth credentials

我确保aws用户具有正确的权限。我还确保存储库允许用户推送到它。为了确保这不是一个问题,我将注册表设置为允许所有用户完全访问。没有什么可以改变“no basic auth credentials”错误。我不知道如何开始调试,因为所有的流量都是加密的。

更新

So I had a bit of Homer Simpson D'Oh moment when I realized the root cause of my problem. I have access to multiple AWS accounts. Even though I was using aws configure to set my credentials for the account where I had setup my repository the aws cli was actually using the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. So when I did aws ecr get-login it was returning a login for the wrong account. I failed to notice that the account numbers were different until I just went back now to try some of the proposed answers. When I remove the environment variables everything works correctly. I guess the motto of the story is if you hit this error, make sure that the repository you are logging into matches the tag you have applied to the image.


当前回答

如果为了CI/CD目的隔离AWS帐户,并且在多个AWS帐户之间共享一个ECR存储库,则可能需要更改~/.docker/配置。手动json。

假设你有这些设置:

ECR由AWS帐户ID 00000000000000拥有 CI服务器属于AWS帐户ID 99999999999999

如果您在CI服务器中调用aws ecr get-login——region us-west-2 | bash, docker将在~/.docker/config.json中生成临时凭证。

{
  "auths": {
    "https://99999999999999.dkr.ecr.us-west-2.amazonaws.com": {
      "auth": "long-token.."
    }
  }
}

但是您希望指向ECR的帐户,因此需要更改主机名。

{
  "auths": {
    "https://00000000000000.dkr.ecr.us-west-2.amazonaws.com": {
      "auth": "long-token.."
    }
  }
}

注意,这种情况取决于您如何形成IAM用户/策略以允许ECR访问。

其他回答

Docker CLI不支持原生IAM鉴权方法。要验证和授权Docker推拉请求,请遵循此步骤。

步骤- 1

检查aws凭证是否正确配置。要配置AWS凭据,运行以下命令并提供AWS凭据。

aws configure

步骤- 2

你可以使用get-login-password将Docker认证到Amazon ECR私有注册表(推荐)

Linux和MSC

aws ecr get-login-password --region <your region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<your region>.amazonaws.com

对于windows

(Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.<your region>.amazonaws.com

or

您也可以使用——get-login方法(但是暴露凭据)(不推荐)。

Linux和MAC

$(aws ecr get-login --region <your region> --no-include-email)

对于windows

Invoke-Expression -Command (Get-ECRLoginCommand -Region <your region>).Command

如果你的登录成功,那么你就可以开始了。否则,请参考aws文档查看错误

步骤- 3

将您的映像推到回购

标记你的图像 Docker标签<aws_account_id>.dkr.ecr..amazonaws.com/my-web-app 使用以下命令推送图像。 Docker push <aws_account_id>.dkr.ecr..amazonaws.com/my-web-app

注意:这是基于令牌的登录和生成的授权令牌 仅12H有效

更新

由于AWS CLI版本2 - AWS ecr get-login已弃用,正确的方法是AWS ecr get-login-password。

因此,正确的最新答案如下: docker login -u AWS -p $(AWS ecr get-login-password——region us-east-1) xxxxxxxx.dkr.ecr.us-east-1.amazonaws.com

我在OSX上运行时也遇到了这个问题。我看到了Oliver Salzburg的响应,并检查了我的~/.docker/config.json。它里面有来自我拥有的不同AWS帐户的多个授权凭证。我删除了该文件,再次运行get-login后,它工作了。

aws ecr get-login --region us-west-1 --no-include-email

这个命令给了我正确的登录命令。如果你不使用“——no-include-email”,它将抛出另一个错误。上面命令的输出看起来像这个docker login -u AWS -p **********************very big******。复制并执行它。现在会显示“Login Succeeded”。现在您可以将映像推到ECR。

确保您的AMI规则具有您尝试登录的用户的权限。

确保在aws ecr get-login中使用正确的区域,它必须与创建存储库的区域匹配。