Docker push doesn't work even docker login succeeded during AWS CodePipeline Build stage

0

Hello,

I'm preparing CI/CD using AWS CodePipeline. Unfortunatelly I have an error during build stage. Below there is content of my buildspec.yml file, where:

AWS_DEFAULT_REGION = eu-central-1

CONTAINER_NAME=cicd-1-app

REPOSITORY_URI = <ACCOUNT_ID>.dkr.ecr.eu-central-1.amazonaws.com/cicd-1-app

version: 0.2
phases:
  install:
    runtime-versions:
      java: corretto11
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws --version
      - TAG="$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"
      - IMAGE_URI=${REPOSITORY_URI}:${TAG}
  build:
    commands:
      - echo Build started on `date`
      - echo $IMAGE_URI
      - mvn clean package -Ddockerfile.skip
      - docker build --tag $IMAGE_URI .
  post_build:
    commands:
      - printenv
      - echo Build completed on `date`
      - echo $(docker images)
      - echo Pushing docker image
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.eu-central-1.amazonaws.com
      - docker push $IMAGE_URI
      - echo push completed
      - printf '[{"name":"%s","imageUri":"%s"}]' $CONTAINER_NAME $IMAGE_URI > imagedefinitions.json
artifacts:
  files:
    - imagedefinitions.json

I got error:

[Container] 2022/01/06 19:57:36 Running command aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin <ACCOUNT_ID>.dkr.ecr.eu-central-1.amazonaws.com
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[Container] 2022/01/06 19:57:37 Running command docker push $IMAGE_URI
The push refers to repository [<ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/cicd-1-app]
37256fb2fd27: Preparing
fe6c1ddaab26: Preparing
d4dfab969171: Preparing
no basic auth credentials

[Container] 2022/01/06 19:57:37 Command did not exit successfully docker push $IMAGE_URI exit status 1
[Container] 2022/01/06 19:57:37 Phase complete: POST_BUILD State: FAILED
[Container] 2022/01/06 19:57:37 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: docker push $IMAGE_URI. Reason: exit status 1

Even docker logged in successfully there is "no basic auth credentials" error. Do you know what could be a problem?

Best regards.

2 Answers
0
Accepted Answer

Is it possible you have authenticated to eu-central-1 and then you try to push to us-east-1?

Authentication requests are tied to specific regions, and cannot be used across regions. For example, if you obtain an authorization token from eu-central-1, you cannot use it to authenticate against your repositories in us-east-1.

RoB
answered 2 years ago
  • You're totally right. Silly mistake. I wrote that REPOSITORY_URI is

    REPOSITORY_URI = <ACCOUNT_ID>.dkr.ecr.eu-central-1.amazonaws.com/cicd-1-app
    

    while in my environments it was:

    REPOSITORY_URI = <ACCOUNT_ID>.dkr.ecr.eu-east-1.amazonaws.com/cicd-1-app
    

    I forgot to update this value. Thanks for your tip. Best regards

0

Everything in the flow looks fine thb.

Maybe there is something wrong with $IMAGE_URI? ECR Repo name or AWS Region?

Is cicd-1-app the ECR repo name?

profile picture
MG
answered 2 years ago
  • Problem solved. My silly mistake with bad region name for REPOSITORY_URI. Check my answer above. Thanks for your message. Best regards.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions