Sub topic 2
Registries and repositories
This chapter assumes you know what a container image is, can build and tag one with Docker, and have the AWS CLI configured with credentials. It covers how ECR organizes images, so that later chapters on authentication, pushing, and policies make sense.
Registry versus repository
An ECR private registry is the account-level container for your images. AWS creates one for every account in every Region, and you don't create it yourself. Its address has this form:
<account-id>.dkr.ecr.<region>.amazonaws.comFor account 123456789012 in us-east-1, the registry URL is 123456789012.dkr.ecr.us-east-1.amazonaws.com.
A repository lives inside a registry and holds the images for one application or component. A repository's name is usually the image name, such as my-app. Each repository has its own permissions, tag settings, scan settings, and lifecycle rules.
The full address of an image combines the registry URL, the repository name, and a tag or digest:
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0Because a registry is tied to an account and a Region, an image pushed to us-east-1 is not visible in eu-west-1 unless you set up replication (covered in a later chapter).
Creating a repository
Create one repository per image name. This command also sets two protections you will use throughout the tutorial:
aws ecr create-repository \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region us-east-1--image-tag-mutability IMMUTABLErejects a push that reuses an existing tag. The default isMUTABLE.scanOnPush=truescans each image for known vulnerabilities when it is pushed.
The response includes the repositoryUri, which is the string you will use when you tag and push images:
{
"repository": {
"repositoryArn": "arn:aws:ecr:us-east-1:123456789012:repository/my-app",
"registryId": "123456789012",
"repositoryName": "my-app",
"repositoryUri": "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app",
"imageTagMutability": "IMMUTABLE",
"imageScanningConfiguration": { "scanOnPush": true }
}
}Settings such as mutability and scanning can be changed later, but the repository name cannot. Pick a name you can live with.
Naming and namespaces
Repository names can contain lowercase letters, digits, and the characters -, _, ., and /. The slash lets you group repositories into namespaces:
team-a/web
team-a/worker
team-b/apiThe full name team-a/web is a single repository name. ECR does not create a "team-a" object. Namespaces are only a naming convention, but they pay off later: permission statements and lifecycle rules can target a name prefix such as team-a/*.
Listing and inspecting
List the repositories in a Region:
aws ecr describe-repositories \
--region us-east-1 \
--query 'repositories[].{name:repositoryName,uri:repositoryUri}'Because registries are per Region, this lists only us-east-1. Run it again with another --region to see the others.
Common errors
| Symptom | Likely cause |
|---|---|
| `RepositoryNotFoundException` on push | The repository does not exist yet. ECR does not create repositories on push by default. |
| `no basic auth credentials` | Docker is not logged in to this registry. See the authentication chapter. |
| Repository is empty in the console | You are viewing a different Region than the one you pushed to. |
| Push to a tag fails with `ImageTagAlreadyExistsException` | The repository is `IMMUTABLE` and the tag is already taken. |
Exercise
An image is tagged 123456789012.dkr.ecr.us-east-1.amazonaws.com/team-a/web:2.1. Name the registry, the repository, and the tag. Would this image appear if you listed repositories in eu-west-1?
Show answer
The registry is 123456789012.dkr.ecr.us-east-1.amazonaws.com, the repository is team-a/web (the slash is part of the name), and the tag is 2.1. It would not appear in eu-west-1, because a registry is specific to an account and Region.