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

从ECR拉取镜像运行时CodeBuild报错exit status 1问题求助

问题根因

从报错日志可直接定位核心问题:你的AWS CodeBuild运行环境中Docker守护进程未正常启动,所有docker相关命令无法执行,最终返回exit status 1。本地和EC2运行正常是因为这两个环境的Docker服务默认处于运行状态。

排查及解决步骤

    1. 启用CodeBuild特权模式
      这是最常见的触发原因:CodeBuild默认禁止容器内运行Docker服务,你需要进入CodeBuild项目配置页,在「环境」配置项下勾选启用特权模式选项,授予容器访问Docker daemon的权限。
    1. 校验构建镜像配置
      如果你使用的是AWS官方提供的标准CodeBuild构建镜像(Amazon Linux 2/2023版本),预装的Docker会在特权模式开启后自动启动;如果你使用自定义构建镜像,需要在buildspec.yml的pre_build阶段补充Docker启动命令。
    1. 校验ECR权限(可选)
      确认CodeBuild关联的服务角色已附加AmazonEC2ContainerRegistryPowerUser权限策略,保证后续镜像推拉操作可正常执行。

修改后完整buildspec.yml参考(适配自定义镜像场景)

version: 0.2
phases:
  pre_build:
    commands:
      # 新增Docker daemon启动逻辑
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2375 --storage-driver=overlay2 &
      - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
      # 原有逻辑保持不变
      - echo Logging in to Amazon ECR...
      - aws --version
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin 677630154057.dkr.ecr.ap-southeast-1.amazonaws.com
      - REPOSITORY_URI=677630154057.dkr.ecr.ap-southeast-1.amazonaws.com/partnersite
      - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
      - IMAGE_TAG=${COMMIT_HASH:=latest}
  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":"partner-site","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
    files: imagedefinitions.json

内容的提问来源于stack exchange,提问作者not-up

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 21:06:03