ECR 上的 Docker 登录失败,来自 Jenkins 的 Powershell 出现 400 Bad Request

oll*_*law 4 powershell jenkins docker kubernetes

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

powershell "aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin ****.dkr.ecr.eu-central-1.amazonaws.com"
Run Code Online (Sandbox Code Playgroud)

但它失败并出现以下错误:

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
Run Code Online (Sandbox Code Playgroud)

奇怪的行为是,如果我在容器上(在本地计算机上和集群上)手动运行命令,一切正常并且登录成功。下面是容器的 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
Run Code Online (Sandbox Code Playgroud)

由于容器在 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"
Run Code Online (Sandbox Code Playgroud)

可能是什么问题呢?

oll*_*law 16

我设法解决了它。

问题原因

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

token
Run Code Online (Sandbox Code Playgroud)

命令的第二部分作为输入


token
Run Code Online (Sandbox Code Playgroud)

它可能会像根本没有输入一样处理它,并且该--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'
Run Code Online (Sandbox Code Playgroud)