Search code examples
amazon-web-servicesdockeramazon-ecrcodebuild

ECR docker login issues with AWS Codebuild


I have created a Build Project with all relevant information. I have also enabled the "Priviledged" in the "Environment" configuration screen.

When starting build in my build log I have the following error:


[Container] 2023/07/23 07:43:32 Command did not exit successfully $(aws ecr get-login --no-include-email --region us-west-2) exit status 252
[Container] 2023/07/23 07:43:32 Phase complete: PRE_BUILD State: FAILED
[Container] 2023/07/23 07:43:32 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: $(aws ecr get-login --no-include-email --region us-west-2). Reason: exit status 252

Instead of using the command $(aws ecr get-login --no-include-email --region us-west-2), I have also tried:

$(aws ecr get-login-password | docker login --username AWS --password-stdin 1234567890.dkr.ecr.us-west-2.amazonaws.com/reponame)

Nothing worked!! What am I missing there? Any help would be greatly appreciated.

In my buildspec.yml file I have the following code:

version: 0.2
phases: 
  install:
    runtime-versions:
        docker: 18     
    commands: 
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2&
      - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
  pre_build: 
    commands: 
    - echo Logging in to Amazon ECR.... 
    - aws --version
    # update the following line with your own region
    - $(aws ecr get-login --no-include-email --region us-west-2)
    - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
    - IMAGE_TAG=${COMMIT_HASH:=latest}
    - REPOSITORY_URI=123456789.dkr.ecr.eu-west-1.amazonaws.com/sample/sampleapp 
  build: 
    commands: 
    - echo Build started on `date` 
    - echo Building the Docker image... 
    # update the following line with the name of your own ECR repository
    - docker build -t $REPOSITORY_URI:latest .
    # update the following line with the URI of your own ECR repository (view the Push Commands in the console)
    - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG 
  post_build: 
    commands: 
    - echo Build completed on `date` 
    - echo pushing to repo
    # update the following line with the URI of your own ECR repository
    - docker push $REPOSITORY_URI:latest
    - docker push $REPOSITORY_URI:$IMAGE_TAG
    - echo Writing image definitions file... 
    #- printf '[{"ImageURI":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imageDetail.json
    - printf '{"ImageURI":"%s"}' $REPOSITORY_URI:$IMAGE_TAG > imageDetail.json
artifacts:
  files:
    - imageDetail.json

Solution

  • Based on @MarkB's suggestion I've found the solution in this link post

    Basically, later I have found that as of the CLI documentation get-login is deprecated in version 2.x of the CLI. It does not exist in the most recent versions.

    Besides that there is no need to add the $() like this example.

    Finally I changed my statement from:

    $(aws ecr get-login-password | docker login --username AWS --password-stdin 1234567890.dkr.ecr.us-west-2.amazonaws.com/reponame)

    To - removed $()

    aws ecr get-login-password | docker login --username AWS --password-stdin 1234567890.dkr.ecr.us-west-2.amazonaws.com/reponame

    Hope this help someone else finding the solution.