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

AWS CodeBuild构建Docker镜像推ECR遇exit status 127错误求助

问题解决:CodeBuild构建Docker镜像推ECR失败

错误原因

你执行的$(aws ecr get-login-password --region $AWS_DEFAULT_REGION)命令逻辑有误:该命令用于获取ECR登录密码,但你直接执行它时,shell会把返回的长base64密码字符串当成要运行的命令,因此触发「文件名太长」的错误。

修正后的buildspec.yml配置

将pre_build阶段的登录命令替换为正确的Docker登录流程,同时保留原有变量定义逻辑:

version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      # 正确的ECR登录:将密码通过管道传递给docker login
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin {你的ECR仓库URI}
      
      - REPOSITORY_URI={你的ECR仓库URI}
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F":" '{print $2}')

  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $REPOSITORY_URI:latest .
      - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker images...
      - docker push $REPOSITORY_URI:latest
      - docker push $REPOSITORY_URI:$IMAGE_TAG
      - echo Writing image definitions file...
      - printf '[{\"name\":\"{你的容器名称}\",\"imageUri\":\"%s\"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
      - cat imagedefinitions.json

artifacts:
  files: imagedefinitions.json

额外说明

  • 替换配置中的{你的ECR仓库URI}和{你的容器名称}为实际业务值
  • 你已附加的IAM权限(AmazonEC2ContainerRegistryFullAccess等)满足操作需求,无需额外调整

内容的提问来源于stack exchange,提问作者1ups_

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 13:32:43