ECR 上的 Docker 登录失败,来自 Jenkins 的 Powershell 出现 400 错误请求

问题描述 投票:0回答:4

我在使用 Powershell 针对 AWS ECR 运行

docker login
时遇到问题。
更具体地说,我使用如下
powershell
步骤从 Windows 容器(在 K8S 集群内)上的 Jenkins 管道运行它

powershell "aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin ****.dkr.ecr.eu-central-1.amazonaws.com"

但失败并出现以下错误:

09:24:32  At C:\Jenkins\agent\workspace\test\awsIamRole-Test@tmp\durable-e2ffd0da\powershellWrapper.ps1:3 char:1
09:24:32  + & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
09:24:32  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
09:24:32      + CategoryInfo          : NotSpecified: (Error response ...400 Bad Request    :String) [], RemoteException
09:24:32      + FullyQualifiedErrorId : NativeCommandError

奇怪的行为是,如果我在容器上(在本地计算机上和集群上)运行命令手动,一切都会正常工作并且登录成功。下面是容器的 Dockerfile。

# escape=`
FROM mcr.microsoft.com/windows/servercore:1809

SHELL ["powershell"] 

RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
RUN choco install jq docker -y 
RUN choco install awscli --version=2.1.15 -y

由于容器在 EC2 实例上运行,并且我需要在容器内运行 Docker,因此当在 K8S 上启动容器时,我绑定到底层 EC2 机器的 Docker 套接字,如下所示(它的工作原理是从管道显示的

docker ps
开始)正确的结果)。

  - image: "****.dkr.ecr.eu-central-1.amazonaws.com/jenkins-container-templates/docker-awscli2-windows:latest"
    name: "docker-awscli2-windows"
    tty: true
    volumeMounts:
    - mountPath: "\\\\.\\pipe\\docker_engine"
      name: "docker-pipe"
  volumes:
  - hostPath:
      path: "\\\\.\\pipe\\docker_engine"
    name: "docker-pipe"

可能是什么问题?

docker powershell jenkins kubernetes
4个回答
16
投票

我设法解决了它。

问题原因

问题似乎与在 Jenkins 上运行 powershell 时如何管理管道有关,并且出于某种原因(我还没有弄清楚),在令牌之前添加了一个新行,这样而不是

token

命令的第二部分作为输入


token

它可能会像根本没有输入一样处理它,并且

--password-stdin
标志使其失败并显示
BadRequest
状态。

解决方法

由于我无法删除这条新行,所以我使用了解决方法

powershell 'docker login --username AWS -p $(aws ecr get-login-password --region eu-central-1 ) ****.dkr.ecr.eu-central-1.amazonaws.com'

0
投票

就我而言,我必须在 ECR 存储库中禁用标签不变性才能使其正常工作!


0
投票

您可以在 pwsh 中运行以下命令:

aws ecr get-login-password --profile $PROFILE --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

登录成功


0
投票

cli-auto-prompt 已“打开”?

aws ecr get-login-password --profile $PROFILE --region $AWS_REGION --no-cli-auto-prompt | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
© www.soinside.com 2019 - 2024. All rights reserved.