阶段上下文状态代码:命令执行错误消息:执行命令时出错:Docker推送原因:退出状态1

jdzmm42g  于 2023-03-01  发布在  Docker
关注(0)|答案(2)|浏览(116)

我正在尝试构建一个管道,使用AWS CodeBuild的buildspec构建并将Docker映像推送到ECR。我的项目是多容器微服务,包含Docker组成yaml文件。我已为我的CodeBuild启用特权模式,管道能够登录到AWS,构建并标记映像,但在Docker推送命令时失败,错误如下:

[Container] 2023/02/21 17:45:38 Command did not exit successfully docker push $REPOSITORY_URL/service1:$TAG exit status 1
[Container] 2023/02/21 17:45:38 Phase complete: POST_BUILD State: FAILED
[Container] 2023/02/21 17:45:38 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: docker push $REPOSITORY_URL/service1:$TAG. Reason: exit status 1

我的docker-compose.yaml文件看起来像这样:

version: '3.4'

services:
  service1:
    image: service1
    build:
      context: .
      dockerfile: Service1.API/Dockerfile

  service2:
    image: service2
    build:
      context: .
      dockerfile: service2.API/Dockerfile

我的buildspec文件如下所示:

version: 0.2

phases:
  install:
    runtime-versions:
      docker: latest
  pre_build:
    commands:
      # This Docker Image tag will have date, time and Codecommit version
      - TAG="$(date +%Y-%m-%d.%H.%M.%S).$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"
      # Check AWS CLI Version        
      - echo "Checking AWS CLI Version..."
      - aws --version
      # Login to ECR Registry 
      - echo "Logging in to Amazon ECR..."
      - $(aws ecr get-login --no-include-email --region us-east-1)
  build:
    commands:
      - echo "Docker build started on `date`"
      - echo "Building the Docker images..."
      - docker-compose -f docker-compose.yml build
      - echo Tagging the Docker images...
      - docker tag service1:latest $REPOSITORY_URL/service1:$TAG
      - docker tag service2:latest $REPOSITORY_URL/service2:$TAG
  post_build:
    commands:
      # Push Docker Image to ECR Repository
      - echo "Docker build completed on `date`"
      - echo "Pushing the Docker images to Amazon ECR..."
      - docker push $REPOSITORY_URL/service1:$TAG
      - docker push $REPOSITORY_URL/service2:$TAG
      - echo "Docker Push to ECR Repository Completed -  $REPOSITORY_URL:$TAG"          
      # Create Artifacts which we can use if we want to continue our pipeline for other stages
      - echo "Writing the image details to a file...""
      - echo {\"service1\":\"$REPOSITORY_URL/service1:$TAG\",\"service2\":\"$REPOSITORY_URL/service2:$TAG\"} > build.json
artifacts:
  files:
    - build.json
    - manifests/*

我已经附加了所有必要的策略和权限到我的代码构建服务角色,我的策略看起来像这样:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "ecr:BatchCheckLayerAvailability",
                "ecr:BatchGetImage",
                "ecr:CompleteLayerUpload",
                "ecr:GetDownloadUrlForLayer",
                "ecr:InitiateLayerUpload",
                "ecr:PutImage",
                "ecr:UploadLayerPart",
                "ecr:SetRepositoryPolicy",
                "ecr:DescribeImages",
                "ecr:DescribeRepositories",
                "ecr:ListImages",
                "ecr:DeleteRepositoryPolicy",
                "ecr:GetRepositoryPolicy",
                "ecr:GetAuthorizationToken"
            ],
            "Effect": "Allow",
            "Resource": "arn:aws:ecr:us-east-1:<ACCOUNT_ID>:repository/dev-repo"
        },
        {
            "Action": [
                "ecr:GetAuthorizationToken"
            ],
            "Effect": "Allow",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ecr-public:GetAuthorizationToken",
                "sts:GetServiceBearerToken"
            ],
            "Resource": "*"
        }
    ]
}

日志错误不够清楚,没有给出什么可能是错误的头,我已经加入了一些答案,这些类似的问题,形成我的政策,但它仍然不为我工作。

  1. AWS ECS CodePipeline build error REPOSITORY_URI
  2. docker push with AWS CodeBuild fails with exit status 1
  3. docker push with AWS CodeBuild fails with exit status 1
bhmjp9jg

bhmjp9jg1#

错误消息对我来说似乎很清楚:

Command did not exit successfully docker push $REPOSITORY_URL/service1:$TAG exit status 1

您的命令无效,您正在尝试推送到名为***$REPOSITORY_URL/service 1:$TAG***的目标。
您在预构建阶段设置TAG,但这不是正确的方法,无论如何,正确的语法应该是

export TAG="$(date +%Y-%m-%d.%H.%M.%S).$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"

按照this guide编写一个正确的buildspec。至少,你缺少了带有正确变量设置的env部分。

lxkprmvk

lxkprmvk2#

因为这是一个多容器微服务应用程序,我想我可以为两个映像使用一个ECR存储库,结果发现这就是问题所在,所以我通过确保每个映像都有单独的ECR存储库并且映像名称必须与存储库名称匹配来解决这个问题。
示例:

docker tag service1:latest $REPOSITORY_URL:$TAG

$REPOSITORY_URL应为:

<ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/service1

相关问题