Sub topic 4
Logging Docker in to ECR
This chapter assumes you can run docker build and docker push, have the AWS CLI configured with working credentials, and know what an IAM policy is. By the end you will be able to authenticate Docker to a private ECR registry and diagnose the common login failures.
Why Docker needs a separate login
Docker does not use your AWS credentials directly. It speaks the standard registry protocol, which expects a username and password. ECR bridges the two. You use your IAM identity to request a temporary password, called an authorization token. You then give that token to Docker.
The token is tied to one registry, which is your account and Region. It is valid for 12 hours. After that, pushes and pulls fail until you log in again.
The login command
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.comThe command has two parts:
aws ecr get-login-passwordcalls the ECRGetAuthorizationTokenAPI with your current credentials and prints the token.docker loginreads that token from standard input. The username is always the literal stringAWS.
Passing the token with --password-stdin keeps it out of your shell history and the process list. Avoid putting it on the command line with --password.
The registry address has the form <account-id>.dkr.ecr.<region>.amazonaws.com. Do not add the repository name to it. One login covers every repository in that registry.
On success, Docker prints Login Succeeded.
<br>
Permissions the login needs
The login itself needs one IAM permission: ecr:GetAuthorizationToken. This action does not apply to a single repository, so its policy statement must use "Resource": "*".
The token only proves who you are. It grants nothing by itself. Each later push or pull is checked against your IAM permissions and the repository policy. A successful login followed by denied on a push usually means the layer-upload permissions are missing, not that the login is broken.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no basic auth credentials` | You have not logged in to this registry, or you logged in to a different Region. | Run the login command again with the correct Region and account ID. |
| `Your authorization token has expired` | More than 12 hours have passed. | Log in again. |
| `AccessDeniedException` on `get-login-password` | Your identity lacks `ecr:GetAuthorizationToken`. | Add the permission with `"Resource": "*"`. |
| Login succeeds, push is `denied` | Missing push permissions on the repository. | Check the IAM policy and repository policy. |
Avoiding repeated logins
On a developer machine, the Amazon ECR Docker credential helper (docker-credential-ecr-login) requests tokens on demand using your AWS credentials, so you never run the login command. You configure it in ~/.docker/config.json:
{
"credHelpers": {
"123456789012.dkr.ecr.us-east-1.amazonaws.com": "ecr-login"
}
}In CI pipelines, a login at the start of each job is simpler, since jobs are short and run well inside the 12-hour limit.
Exercise
Your build job logs in to ECR successfully and then pushes an image to a repository in us-west-2, but the push fails with "no basic auth credentials". The login used the registry 123456789012.dkr.ecr.us-east-1.amazonaws.com. What went wrong, and how do you fix it?
Show answer
The token is scoped to one registry, and the registry address includes the Region. The job authenticated to the us-east-1 registry but pushed to the us-west-2 registry, so Docker had no credentials for it. Run aws ecr get-login-password --region us-west-2 and log in to 123456789012.dkr.ecr.us-west-2.amazonaws.com, or log in to both registries.