我尝试登录我的 AWS ECR 存储库。这是我的 powershell 脚本:
param(
[string]$profile,
[string]$ecr_name
)
$accountId = (aws sts get-caller-identity --query "Account" --output text --profile $profile)
$region = (aws configure get region --profile $profile)
$TAG = "latest"
$AccountUrl = "${accountId}.dkr.ecr.${region}.amazonaws.com"
Write-Host "LoginAttempt 1"
docker login --username AWS --password $(aws ecr get-login-password --region $region --profile $profile) $AccountUrl
Write-Host "LoginAttempt 2"
aws ecr get-login-password --region $region --profile $profile | docker login --username AWS --password-stdin $AccountUrl
Write-Host "LoginAttempt 3"
docker login --username AWS --password $(aws ecr get-login-password --region $region --profile $profile) $AccountUrl
将代码复制粘贴到 powershell 终端时,一切正常。使用此命令直接运行代码时:
powershell.exe -文件 file_location.ps1 --ecr_name yyy --profile xxx
我发现了这种奇怪的行为:
LoginAttempt 1
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
LoginAttempt 2
Error response from daemon: login attempt to https://1234567890.dkr.ecr.eu-west-1.amazonaws.com/v2/ failed with status: 400 Bad Request
LoginAttempt 3
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Login Succeeded
我在Windows机器上工作,脚本存储在带有LF的utf8中。出于明显的安全原因,我更愿意使用标准输入传递密码。
你自己的尝试:
aws ecr get-login-password --region $region --profile $profile |
docker login --username AWS --password-stdin $AccountUrl
应该在 PowerShell(核心)v7.4+ 中按原样工作,其中在外部(本机)程序之间传输的数据现在按原样传递,即作为原始字节数据。
在 v7.3.x- 以及 Windows PowerShell 中,需要 解决方法,因为在这些版本中:
不仅将
aws
输出解码为.NET字符串,并根据管道中存储在$OutputEncoding
首选项变量中的编码重新编码到docker
至关重要的是,在原始文本中总是会添加换行符 - 这就是导致登录失败的原因。
最简单的解决方法是使用
cmd /c
,因为 cmd.exe
的管道始终是原始字节管道,因此不会出现问题:
# Workaround for PowerShell 7.3.x- and Windows PowerShell
cmd /c "aws ecr get-login-password --region $region --profile $profile | docker login --username AWS --password-stdin `"$AccountUrl`""
注:
$region
和 $profile
扩展为不需要在 cmd.exe
中使用双引号的值;如果是这样,请使用嵌入式双引号,就像我对上面的 $AccountUrl
所做的那样(滚动到视图之外,因为 - 与 PowerShell 不同 - cmd /c
仅支持 单行命令)。