Home > Net >  ##[error]denied: Not Authorized when pushing docker image to AWS ECR
##[error]denied: Not Authorized when pushing docker image to AWS ECR

Time:02-23

I'm trying to push my Docker image to AWS ECR and I'm getting Not Authorized when trying to do so.

I have all of the required variables set as variables in Azure DevOps, which is what I'm using. So I'm not sure why it's not getting proper authentication.

Here's my YAML code:

trigger:
- main

pool:
  vmImage: ubuntu-latest
strategy:
  matrix:
    Python38:
      python.version: '3.8'

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(python.version)'
  displayName: 'Use Python $(python.version)'

- script: |
    python -m pip install --upgrade pip
    python -m pip install --upgrade pip requests os smtplib datetime
    pip install -r requirements.txt
  displayName: 'Install dependencies'

- task: CopyFiles@2
  inputs:
    SourceFolder: 
    Contents: '*'
    TargetFolder: $(build.artifactstagingdirectory)

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(build.artifactstagingdirectory)'
    includeRootFolder: true
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/project.zip'
    replaceExistingArchive: true

- task: S3Upload@1
  inputs:
    awsCredentials: 'weather'
    regionName: 'us-west-2'
    bucketName: 'weather-update-project-bucket'
    sourceFolder: '$(build.artifactstagingdirectory)'
    globExpressions: '*project.zip*'
    targetFolder: 'python'
    createBucket: true

- script: |
    aws ecr get-login-password --region $(AWS_REGION) | docker login --username AWS --password-stdin $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
  displayName: 'Login to AWS'
  env:
    AWS_ACCESS_KEY_ID: $(AWS_ACCESS_KEY_ID)
    AWS_SECRET_ACCESS_KEY: $(AWS_SECRET_ACCESS_KEY)

- task: Docker@2
  inputs:
    repository: 'public.ecr.aws/u1c1h9j4/weather-update-project'
    command: 'buildAndPush'
    Dockerfile: '**/Dockerfile'
    tags: 'latest'

CodePudding user response:

It's better to use the Amazon ECR Push task instead of the regular Docker push.

First, build the image with Docker@2:

- task: Docker@2
  displayName: Build an image
  inputs:
    command: build
    dockerfile: '**/Dockerfile'
    buildContext: '$(Build.SourcesDirectory)'
    tags: 'latest'

After that, push with ECRPushImage@1:

- task: ECRPushImage@1
  inputs:
    awsCredentials: 'weather'
    regionName: us-west-2
    imageSource: 'imagename'
    sourceImageName: 'YOUR-IAMGE-NAME'
    sourceImageTag: 'latest'
    pushTag: 'latest'
    repositoryName: 'YOUR-AWS-ECR-REPO'
  • Related