Sub topic 13
Caching upstream registries with pull through cache
This chapter assumes you can push and pull images with Docker, authenticate to ECR, and read IAM policies. It builds on the earlier chapters on authentication and repository policies.
What pull through cache does
Pull through cache lets your private ECR registry act as a cache for an upstream registry such as Docker Hub, Amazon ECR Public, Quay, or the Kubernetes registry. You don't push anything. You pull from a path in your own registry, and ECR fetches the image upstream and stores a copy.
This gives you three benefits:
- Pulls come from your own registry, so they are not affected by upstream rate limits or outages.
- The cached copy lives in your account, so you can scan it, replicate it, and apply lifecycle policies to it.
- Your IAM and network controls apply to the images you run.
The working model
A pull through cache rule maps a repository prefix in your registry to an upstream registry URL. Any pull whose repository name starts with that prefix is served through the rule.
On the first pull, ECR creates the repository docker-hub/library/nginx and imports the image. On later pulls, ECR serves the cached copy. It checks the upstream registry for a newer image behind the same tag at most once every 24 hours.
Creating a rule
Docker Hub requires credentials, so store them in Secrets Manager first. The secret name must start with ecr-pullthroughcache/. It must contain username and accessToken keys. Replace the placeholders with your own values.
aws secretsmanager create-secret \
--name ecr-pullthroughcache/docker-hub \
--secret-string '{"username":"my-user","accessToken":"<access-token>"}' \
--region us-east-1
aws ecr create-pull-through-cache-rule \
--ecr-repository-prefix docker-hub \
--upstream-registry-url registry-1.docker.io \
--credential-arn arn:aws:secretsmanager:us-east-1:123456789012:secret:ecr-pullthroughcache/docker-hub-AbCdEf \
--region us-east-1ECR Public (public.ecr.aws), Quay (quay.io), and the Kubernetes registry (registry.k8s.io) don't need credentials. For those, omit --credential-arn.
Pulling through the cache
Log in to your private registry as usual, then pull using your registry's hostname and the prefix:
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
docker pull 123456789012.dkr.ecr.us-east-1.amazonaws.com/docker-hub/library/nginx:latestDocker Hub official images live under the library/ namespace, so the path includes it.
Permissions
The principal that pulls needs the normal pull permissions. The first pull also needs two more actions, because it creates a repository and imports an image:
ecr:CreateRepositoryecr:BatchImportUpstreamImage
Later pulls of an already cached image need only the pull permissions.
Common errors
| Symptom | Likely cause |
|---|---|
| Access denied on the first pull only | The principal lacks `ecr:CreateRepository` or `ecr:BatchImportUpstreamImage`. |
| Not found for a Docker Hub image | The path is missing `library/`, or the prefix doesn't match the rule. |
| Rule creation fails for Docker Hub | The secret name doesn't start with `ecr-pullthroughcache/`, or the secret lacks `username` or `accessToken`. |
| Cached repository has no lifecycle or scan settings | The repository was created automatically with defaults. |
For the last case, create a repository creation template for your prefix. ECR then applies your settings, such as tag immutability, scanning, and encryption, to repositories it creates for that prefix.
Exercise
A pull of 123456789012.dkr.ecr.us-east-1.amazonaws.com/docker-hub/library/nginx:latest succeeds for an administrator but fails with access denied for a new CI role that has only pull permissions. No cached repository exists yet. What is missing, and why?
Show answer
The CI role needs ecr:CreateRepository and ecr:BatchImportUpstreamImage. The first pull must create the docker-hub/library/nginx repository and import the image from upstream. Pull-only permissions don't allow either action. After the image is cached, the pull permissions alone would be enough.