Sub topic 2
Registries and repositories
This chapter covers how Amazon ECR organizes images: one registry per account and Region, and many repositories inside it. It assumes you can build and tag a Docker image, run AWS CLI commands with configured credentials, and read a basic IAM policy. Later chapters cover the access rules and authentication in detail. Here the focus is on the structure you create first and the settings you choose when you create it.
The registry: one per account and Region
A registry is the top-level container for your images. ECR gives every AWS account one private registry in each Region. You don't create it. It exists as soon as you use ECR in that Region.
The registry has two identifiers that you will see often:
- Registry ID: the 12-digit AWS account ID, for example
123456789012. - Registry URL:
<account-id>.dkr.ecr.<region>.amazonaws.com, for example123456789012.dkr.ecr.us-east-1.amazonaws.com.
Docker uses the registry URL as the hostname when it logs in, pushes, and pulls. Because the hostname contains both the account and the Region, the same repository name in us-east-1 and eu-west-1 refers to two unrelated places. Images do not move between Regions on their own. Moving them takes replication, which a later chapter covers.
Registry-level settings apply to every repository in that registry and Region. Examples are the scanning type (basic or enhanced), replication configuration, and the registry permissions policy.
ECR Public is a separate service with its own public registry and its own public.ecr.aws hostname. Everything in this tutorial is about private registries.
The repository: where images live
A repository is a named collection of related images, usually every version of one application or service. You create repositories explicitly. Each one has its own settings, permissions, and lifecycle rules.
The full address of a repository is the registry URL plus the repository name:
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-appThis address is the repository URI. When you add a tag, you get an image reference, such as .../my-app:1.0.0.
Repository names and namespaces
Repository names must be unique within a registry. They may contain lowercase letters, numbers, hyphens, underscores, periods, and forward slashes. The name must start with a letter or number and may be up to 256 characters long.
The forward slash creates a namespace, which is a naming convention and nothing more. ECR has no folder object. team/api and team/worker are two independent repositories that happen to share a prefix. The prefix is still useful:
- You can group repositories by team, project, or environment, for example
payments/apiandpayments/worker. - IAM policies and repository creation templates can match a prefix with a wildcard, such as
arn:aws:ecr:us-east-1:123456789012:repository/payments/*. - Pull through cache uses a prefix to keep upstream images apart from your own.
Pick a naming scheme before you have fifty repositories. Renaming is not supported. To change a name, create a new repository and push the images again.
Creating a repository
You choose several settings when you create a repository. The command below sets the two that matter most for most teams:
aws ecr create-repository \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region us-east-1The response includes the repository ARN, the registry ID, and the repository URI. Keep the URI. You need it for tagging and pushing.
| Setting | Option | Effect |
|---|---|---|
| Tag mutability | `MUTABLE` (default) | A tag can be moved to a different image by pushing again. |
| Tag mutability | `IMMUTABLE` | Pushing an existing tag with different content is rejected. |
| Scan on push | `scanOnPush=true` | Each pushed image is scanned. The default is off. |
| Encryption | `AES256` (default) or `KMS` | Controls the key that encrypts stored images. |
Encryption can only be chosen at creation time. If you want a customer managed KMS key, pass it when you create the repository:
aws ecr create-repository \
--repository-name payments/api \
--encryption-configuration encryptionType=KMS,kmsKey=alias/ecr-payments \
--region us-east-1Tag mutability and scan on push can be changed later with put-image-tag-mutability and put-image-scanning-configuration. The encryption type cannot, so decide it up front. The encryption chapter explains the trade-offs.
Inspecting repositories
List the repositories in a Region and show only the fields you need:
aws ecr describe-repositories \
--region us-east-1 \
--query 'repositories[].{name:repositoryName,uri:repositoryUri,mutability:imageTagMutability,scan:imageScanningConfiguration.scanOnPush}' \
--output tableAdd --repository-names my-app to look at a single repository. If the name does not exist, the call fails with RepositoryNotFoundException.
Repositories are not created on push
Docker Hub creates a repository the first time you push to a new name. ECR does not. Pushing to a repository that doesn't exist fails, and the Docker client reports a message like this:
name unknown: The repository with name 'my-app' does not exist in the registry with id '123456789012'The same message appears when the repository exists but you are pointed at the wrong Region or the wrong account. Check these three things before suspecting permissions:
- The repository name in the image reference matches exactly, including any namespace prefix.
- The Region in the hostname is the Region where you created the repository.
- The account ID in the hostname is the account that owns it.
Teams that want repositories to appear on demand can use repository creation templates. A template defines settings such as tag mutability, encryption, lifecycle policy, and repository policy for repositories that match a prefix. ECR applies it when it creates a repository automatically, which happens with pull through cache and replication. Templates apply only to repositories created that way. They do not change existing repositories, and they do not turn on auto-creation for plain docker push.
Deleting repositories
A repository that still contains images cannot be deleted by default:
aws ecr delete-repository --repository-name my-app --region us-east-1This fails with RepositoryNotEmptyException if any image remains. Adding --force deletes the repository and every image in it. The deletion cannot be undone, and any ECS task definition, Kubernetes manifest, or pipeline that references those images will fail to pull the next time it needs them.
Common mistakes
- Mixing up Regions. A repository in
us-east-1is not visible fromeu-west-1. Set--regionexplicitly in scripts, or setAWS_DEFAULT_REGION. - Using the repository name as the image name without the registry URL. Docker treats a bare
my-app:1.0.0as a Docker Hub reference. Always tag with the full URI. - Expecting one repository to hold many applications. Tags are meant for versions of one image. Putting unrelated images in one repository makes lifecycle rules and permissions hard to write, so create a repository per image.
- Choosing the encryption type late. Recreate the repository and push the images again if you need to change it.
- Leaving defaults in place.
MUTABLEtags and no scanning are the defaults. Set your preferred values at creation, or set them through a creation template.
Quick reference
| Question | Answer |
|---|---|
| How many private registries per account? | One per Region. |
| What is the registry ID? | The 12-digit account ID. |
| Who creates the registry? | AWS, automatically. |
| Who creates repositories? | You, or a creation template on automatic creation. |
| Can a repository be renamed? | No. Create a new one and push again. |
| Can encryption be changed later? | No. |
| Can tag mutability be changed later? | Yes. |
Exercise
You run docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/team/api:2.0.0 and get "name unknown: The repository with name 'team/api' does not exist in the registry with id '123456789012'". You are sure you created team/api yesterday. Name two likely causes and the command you would use to check.
Show answer
Likely causes are that the repository was created in a different Region (or in a different account) than the one in the push hostname. Run aws ecr describe-repositories --repository-names team/api --region us-east-1 and compare the output with other Regions, and run aws sts get-caller-identity to confirm the account. If the repository is found only in another Region, either push to that Region's URI or create the repository in us-east-1.
Exercise
A teammate wants to switch an existing repository from AES-256 to a customer managed KMS key. What do you tell them?
Show answer
The encryption type of a repository cannot be changed after creation. They need to create a new repository with --encryption-configuration encryptionType=KMS,kmsKey=<key>, push (or replicate) the images into it, update the references that point to the old repository, and then delete the old one.