Sub topic 4
Logging Docker in to ECR
This chapter assumes you can build and tag images with Docker. It also assumes the AWS CLI (version 2 is used here) has working credentials, and that you know what an IAM principal is. By the end you should understand what docker login does against ECR, why the credential expires, and how to diagnose the common failures.
Why ECR needs its own login
Docker Hub uses a long-lived username and password or an access token. ECR does not have such accounts. Access is decided by IAM, and Docker only knows how to send a username and password to a registry. ECR bridges the two with a short-lived token:
- Your IAM principal (a user or role) asks ECR for an authorization token.
- ECR returns a password that is valid for 12 hours.
- You give that password to Docker with the fixed username
AWS. - Docker sends the credentials to the registry on every push or pull.
The token carries the permissions of the principal that requested it. A token from a read-only role can pull but cannot push, even though Docker logged in successfully.
The standard login command
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.comEach part has a job:
aws ecr get-login-passwordcalls theGetAuthorizationTokenAPI and prints only the decoded password. The--regionoption selects the Region whose registry you are logging in to.--username AWSis a fixed literal. It is not your IAM user name.--password-stdinmakes Docker read the password from the pipe. This keeps the token out of your shell history and out of the process list, which is why you should avoid-p.- The last argument is the registry URL, which has the form
<account-id>.dkr.ecr.<region>.amazonaws.com. Do not add a repository name or a path. Docker stores one credential per registry host.
On success, Docker prints Login Succeeded.
What happens to the token
Docker writes the credential to ~/.docker/config.json under the auths key. Without a credential store it saves the value base64-encoded, which is not encryption. Docker warns you about this when you log in. Because the token expires after 12 hours, the exposure is limited. Even so, treat that file as sensitive.
You can see what ECR actually returns with the lower-level command:
aws ecr get-authorization-token --region us-east-1 \
--query 'authorizationData[0].{endpoint:proxyEndpoint,expires:expiresAt}'The full response also includes authorizationToken, which is the base64 encoding of AWS:<password>. get-login-password decodes it and removes the AWS: prefix for you.
Registry-specific and Region-specific
The URL has two variable parts, and both matter.
| Part of the URL | Meaning | Typical mistake |
|---|---|---|
| Account ID | The AWS account that owns the registry | Using your own account ID when pulling from a shared account |
| Region | The Region where the repository lives | Logging in to `us-east-1` and pushing to a repository in `eu-west-1` |
Logging in to one registry URL does not authenticate you to another. If you work with repositories in two Regions, or in two accounts, you need one login per registry host. Docker keeps separate entries for each.
IAM permissions the login needs
Two layers of permission apply, and they fail in different places.
- Getting the token requires
ecr:GetAuthorizationToken. This action does not apply to a specific repository, so in an identity policy it must use"Resource": "*". If it is missing,get-login-passwordfails withAccessDeniedException. You never reach Docker. - Using the token requires repository-level actions, such as
ecr:BatchGetImagefor pulls orecr:PutImagefor pushes. If those are missing, the login succeeds butdocker pushordocker pullfails with a denied error.
This split explains a common confusion: Login Succeeded only proves that you got a token. It does not prove that you can use any repository.
A minimal identity policy for a build role that logs in and pushes to one repository looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "GetToken",
"Effect": "Allow",
"Action": "ecr:GetAuthorizationToken",
"Resource": "*"
},
{
"Sid": "PushToMyApp",
"Effect": "Allow",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:InitiateLayerUpload",
"ecr:UploadLayerPart",
"ecr:CompleteLayerUpload",
"ecr:PutImage"
],
"Resource": "arn:aws:ecr:us-east-1:123456789012:repository/my-app"
}
]
}Add ecr:BatchGetImage and ecr:GetDownloadUrlForLayer if the same role also pulls.
Credential helpers: no manual re-login
Running the login command every 12 hours is tedious in a development environment. The Amazon ECR credential helper (docker-credential-ecr-login) removes the step. Docker calls the helper each time it contacts an ECR registry, and the helper requests a fresh token using the standard AWS credential chain. That chain checks environment variables, the shared config and credentials files, and instance or container roles.
After installing the helper binary and putting it on your PATH, configure Docker in ~/.docker/config.json:
{
"credHelpers": {
"123456789012.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
}
}The key is the registry host and the value is the helper name, which is the executable name without the docker-credential- prefix. Once this is set, you can run docker push or docker pull without a separate docker login.
If you use many registries, "credsStore": "ecr-login" applies the helper to every registry, which can interfere with Docker Hub logins. Per-host credHelpers entries are safer.
The trade-off: the helper needs valid AWS credentials at the moment of each Docker operation, and the binary must be installed wherever Docker runs. In CI systems that give each job a fresh environment, the explicit get-login-password pipe is usually simpler.
Using a named profile or role
get-login-password uses the same credentials as any CLI command. To log in as a specific profile:
aws ecr get-login-password --region us-east-1 --profile build-role | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.comTwo details matter here. First, the token lifetime is 12 hours even if the role session that created it expires sooner. Second, the permissions are fixed from the identity at the time of the request. If you change the IAM policy afterward, the new rules apply on the next registry request, because the registry evaluates IAM at request time. But a token from a different principal must be re-requested.
Other clients
The docker login flow works with any tool that speaks the registry protocol. Podman and Finch accept the same --username AWS --password-stdin arguments. In PowerShell, the same pipe works with docker login. Build tools such as Kaniko or BuildKit read the same config.json or use their own ECR helper.
Amazon ECR Public is a separate service. Its registry is public.ecr.aws, and its token comes from aws ecr-public get-login-password --region us-east-1. That command only works in us-east-1. Do not mix it up with the private registry command shown above.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| `no basic auth credentials` on push or pull | No login for that exact registry host, or the Region differs | Log in again to the host in the image name |
| `denied: Your authorization token has expired. Reauthenticate and try again.` | The 12-hour token ran out | Run the login pipe again |
| `AccessDeniedException` from `get-login-password` | The principal lacks `ecr:GetAuthorizationToken` | Add the action with `"Resource": "*"` |
| `Login Succeeded`, then `denied: User ... is not authorized to perform: ecr:InitiateLayerUpload` | The login worked but repository permissions are missing | Add push actions on the repository ARN |
| `Error saving credentials` or `error storing credentials` | Docker's credential store (a keychain or helper) is missing or broken | Fix `credsStore` in `config.json`, or remove it |
| `Cannot perform an interactive login from a non TTY device` | `--password-stdin` received no input because the CLI call failed | Run the `aws ecr get-login-password` part alone and read its error |
| `Unable to locate credentials` | The CLI has no AWS credentials in this shell | Configure a profile, environment variables, or a role |
A fast way to check which identity you are using before you login is:
aws sts get-caller-identityIf the account shown is not the one in the registry URL, you may still be able to log in, but you will need a repository policy that grants that principal access. That topic comes in the chapter on repository policies.
Check your understanding
Exercise
A teammate says "docker login printed Login Succeeded, so push will work." Give two reasons this can be wrong, and name the IAM action that docker login depends on.
Show answer
First, login only proves that a token was issued. The token carries the principal's permissions, so a push still fails with a denied error if the principal lacks repository actions such as ecr:InitiateLayerUpload and ecr:PutImage, or if a repository policy blocks access. Second, the login covers one registry host, meaning one account and Region. Pushing to a repository in another Region or account needs its own login. The action needed to get the token is ecr:GetAuthorizationToken, which must use "Resource": "*".
Exercise
Your CI job logged in to 123456789012.dkr.ecr.us-east-1.amazonaws.com and then ran docker push 123456789012.dkr.ecr.eu-west-1.amazonaws.com/my-app:1.0.0. It failed with "no basic auth credentials". Why, and how do you fix it?
Show answer
Docker stores credentials per registry host, and the eu-west-1 host has no entry. Run aws ecr get-login-password --region eu-west-1 and pipe it to docker login --username AWS --password-stdin against the eu-west-1 registry URL, then push again.
Exercise
What is the trade-off between the manual get-login-password pipe and the Amazon ECR credential helper?
Show answer
The pipe needs no extra software and works well in short-lived CI jobs, but you must repeat it every 12 hours and Docker stores the token in config.json. The helper fetches a fresh token on every operation, so there is no expiry to manage and no stored token. However, the binary must be installed and configured, and AWS credentials must be available whenever Docker runs.