Sub topic 2
Building, Tagging, Pushing, and Pulling an Image
Assume you have Docker installed, the AWS CLI configured, and permission to use Amazon Elastic Container Registry (ECR). ECR is a private registry for storing container images. A repository is a named collection of related images.
Build, Tag, Push, and Pull
Create a repository with immutable tags and scan-on-push enabled:
aws ecr create-repository \
--repository-name my-app \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region us-east-1Authenticate Docker to the registry. The password is temporary and should not be stored in a script.
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin \
123456789012.dkr.ecr.us-east-1.amazonaws.comBuild the image, apply its full ECR name, and push it:
docker build -t my-app:1.0.0 .
docker tag my-app:1.0.0 \
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0
docker push \
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0To pull it, use the same full repository URI:
docker pull \
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app:1.0.0Tags Versus Digests
A tag, such as 1.0.0, is a human-readable label. A digest is a content-based identifier that changes when the image content changes. Tags are convenient, but a mutable tag can point to different content later. Use a digest for reproducible deployments:
aws ecr describe-images \
--repository-name my-app \
--region us-east-1 \
--query 'imageDetails[].{tags:imageTags,digest:imageDigest}'
docker pull \
123456789012.dkr.ecr.us-east-1.amazonaws.com/my-app@sha256:<digest>Tag Immutability and Sharing
With IMMUTABLE tags, ECR rejects a later push that reuses an existing tag. Publish a new tag for every release. This prevents an unnoticed replacement of 1.0.0.
A repository policy can allow another AWS account to pull images. The other account also needs IAM permission for ecr:GetAuthorizationToken:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"AWS": "arn:aws:iam::111122223333:root"},
"Action": ["ecr:BatchGetImage", "ecr:GetDownloadUrlForLayer"]
}]
}Grant only the actions and accounts required.
Exercise
Why is a digest safer than a tag for a production deployment?
Show answer
A digest identifies exact image content, while a tag can be moved to different content unless tag immutability prevents overwrites.