Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

Azure Pipelines Docker task cannot log in to Azure Container Registry

Errorunauthorized: Invalid clientid or client secret.

Short answer

The service connection sends credentials that ACR rejects. Most often the service principal secret has expired, because it is valid for one year by default. Reset it and update the Docker registry service connection. Also check that the Docker ID is the service principal client ID and that the principal has the AcrPush role.

  • Applies to Azure Pipelines, Docker@2 task, Azure CLI 2.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Test the same credentials outside the pipeline. If docker login fails on your machine too, the credentials are wrong; if it works, the service connection holds something different.

    docker login myregistry.azurecr.io --username "$SP_APP_ID" --password-stdin <<< "$SP_PASSWD"
  2. 2Match what you see, then jump to its fix

    1. 1The pipeline worked for months and then started failing without any changeThe service principal secret has expiredFix
    2. 2Fails on the first run after you created the connection by handThe Docker registry service connection has the wrong ID or passwordFix
    3. 3Pulls work, pushes are refusedThe service principal can pull but is not allowed to pushFix
    4. 4containerRegistry points at an ARM service connectionAn Azure Resource Manager connection is used where the Docker task needs a Docker registry oneFix

What does the error look like?

  • unauthorized: Invalid clientid or client secret.

Cause 1 of 4

The service principal secret has expired

1Check

A service principal created for ACR has one year of validity by default. If the secret is older than that, it has expired. A docker login with the stored secret from your own machine fails with the same message.

docker login myregistry.azurecr.io --username "$SP_APP_ID" --password "$OLD_SECRET"

2Fix

Create a new secret with az ad sp credential reset, then paste the new password into the pipeline's service connection. The command can also set a longer expiry.

az ad sp credential reset --id $SP_APP_ID

3Verify

Log in with the new secret and re-run the pipeline.

docker login myregistry.azurecr.io --username "$SP_APP_ID" --password "$NEW_SECRET"

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 4

The Docker registry service connection has the wrong ID or password

1Check

For a Docker registry service connection, the Docker ID must be the service principal's client ID and the password its key (client secret). Use the appId value, not the display name.

az ad sp list --display-name MY_SP_NAME --query "[].appId" --output tsv

2Fix

Edit the service connection: registry URL like https://myacr.azurecr.io, Docker ID is the appId, password is the client secret.

Docker Registry:  https://myacr.azurecr.io
Docker ID:        <service principal client ID>
Password:         <service principal client secret>

3Verify

Run the login step alone.

- task: Docker@2
  inputs:
    command: login
    containerRegistry: dockerRegistryServiceConnection1

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

The service principal can pull but is not allowed to push

1Check

The default role in the Microsoft sample script is acrpull, which only allows pulls; push and pull needs acrpush. Log in and try a pull, then a push, to see which one fails.

docker pull myregistry.azurecr.io/my-app:latest
docker push myregistry.azurecr.io/my-app:latest

2Fix

Assign the AcrPush role (push and pull) to the principal on the registry.

ACR_ID=$(az acr show --name myacr --query id --output tsv)
az role assignment create --assignee $SP_APP_ID --scope $ACR_ID --role acrpush

3Verify

Re-run the pipeline's push step.

docker push myregistry.azurecr.io/my-app:latest

Confirmed by learn.microsoft.com

Did this fix it?

Cause 4 of 4

An Azure Resource Manager connection is used where the Docker task needs a Docker registry one

1Check

Microsoft's Docker@2 troubleshooting says the task supports only the Docker registry service connection, not an ARM one.

grep -n -A4 'Docker@2' azure-pipelines.yml

2Fix

Create a Docker registry service connection of registry type Others, with your service principal's client ID and key as in the second cause, and reference it in containerRegistry.

- task: Docker@2
  inputs:
    command: buildAndPush
    containerRegistry: dockerRegistryServiceConnection1
    repository: my-app

3Verify

The login lines of the task log should show success.

# Pipelines UI: open the Docker task log

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Put the secret's end date in a team calendar, or set a longer expiry when you reset it.
  • Create a separate service principal for CI with AcrPush only on the one registry.
  • Always test new credentials with docker login before saving them in the service connection.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.