You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS CLI和Docker CLI操作ECR时执行Bash脚本报错求助

AWS ECR镜像构建推送脚本执行错误修复

问题场景

我尝试执行Bash脚本完成AWS登录、Docker镜像构建、打标签并推送到AWS ECR,脚本内容:

aws ecr get-login-password --region us-east-2 --profile weather_ecr_agent | docker login --username AWS --password-stdin <LOGIN>
docker build -f ./Dockerfile -t cloud-weather-temperature: latest .
docker tag cloud-weather-temperature: latest <LOGIN>/cloud-weather-temperature: latest 
docker push <LOGIN>/cloud-weather-temperature:latest

执行命令:bash build_and_push_ecr_image.sh,出现以下错误:

build_and_push_ecr_image.sh: /mnt/c/Users/USER/AppData/Local/Programs/Python/Python39/Scripts/aws: C:\Users\USER\AppData\Local\Programs\Python\Python39\python.exe: bad interpreter: No such file or directory
Error: Cannot perform an interactive login from a non TTY device
ERROR: "docker buildx build" requires exactly 1 argument.
See 'docker buildx build --help'.

Usage:  docker buildx build [OPTIONS] PATH | URL | -

Start a build
"docker tag" requires exactly 2 arguments.
See 'docker tag --help'.

Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
invalid reference format

AWS CLI版本信息(pip安装):

aws-cli/1.34.22 Python/3.9.7 Windows/10 botocore/1.35.22

错误原因及修复步骤

1. AWS CLI跨环境调用不兼容

错误提示找不到Python解释器,是因为在WSL中直接调用了Windows版AWS CLI,路径格式不匹配WSL的Linux环境。

  • 修复:在WSL终端中安装Linux版AWS CLI,不要使用Windows pip安装的版本。安装完成后执行aws --version验证,确保显示Linux环境的版本信息。

2. 镜像标签格式错误

脚本中镜像标签的冒号后添加了空格(如cloud-weather-temperature: latest),Docker会将其识别为两个独立参数,导致构建和打标签命令参数数量错误。

  • 修复:去掉标签冒号后的空格,统一格式为镜像名:标签。

3. ECR仓库地址占位符未替换

脚本中的<LOGIN>是占位符,未替换为实际的ECR仓库完整地址(格式为123456789012.dkr.ecr.us-east-2.amazonaws.com),导致登录和推送命令无效。

  • 修复:将<LOGIN>替换为你的ECR仓库地址,建议用变量统一管理,避免重复修改。

修正后的完整脚本

# 替换为你的ECR仓库完整地址
ECR_REPO="123456789012.dkr.ecr.us-east-2.amazonaws.com"

# AWS ECR登录
aws ecr get-login-password --region us-east-2 --profile weather_ecr_agent | docker login --username AWS --password-stdin $ECR_REPO

# 构建镜像
docker build -f ./Dockerfile -t cloud-weather-temperature:latest .

# 打标签
docker tag cloud-weather-temperature:latest $ECR_REPO/cloud-weather-temperature:latest

# 推送到ECR
docker push $ECR_REPO/cloud-weather-temperature:latest

额外注意事项

  • 确保WSL环境中安装了Docker,且与Windows Docker Desktop的WSL集成已开启,避免跨环境调用问题。
  • 执行脚本前,先验证AWS CLI配置:aws ecr describe-repositories --region us-east-2 --profile weather_ecr_agent,确认能正常访问ECR。

内容的提问来源于stack exchange,提问作者Talented Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 04:21:02