Sub topic 1
From container image to running workload
This chapter follows an image from your machine to a registry that ECS can pull from. It covers two outline parts: how an image becomes a running workload, and how to store images in Amazon ECR.
Prerequisites
This chapter assumes you already have:
- Basic Docker knowledge: you can build an image and run a container.
- Familiarity with IAM, VPC, subnets, and security groups.
- The AWS CLI installed and configured, and comfort reading JSON.
- A basic understanding of HTTP services and health endpoints.
From container image to running workload
A container image is a read-only package that holds your application, its dependencies, and its start command. A container is a running instance of that image. ECS does not build images. It only pulls an image by name and starts containers from it.
The path to a running workload has four steps:
- Build the image with Docker.
- Push the image to a registry.
- Reference the image in a task definition.
- Ask ECS to run it, either as a one-off task or as a long-lived service.
Later chapters cover steps 3 and 4. Here you handle steps 1 and 2.
Storing images in Amazon ECR
Amazon ECR (Elastic Container Registry) is AWS's managed registry for container images. A registry holds many repositories. A repository holds the versions of one image, each identified by a tag such as 1.0.0 or by a content digest. Fargate tasks pull from ECR over the network, and access is controlled with IAM.
An image URI has this shape:
123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0That is the account ID, the region, the repository name, and the tag. This is the same URI the task definition uses later in the image field.
Create a repository and push an image
Run these commands from the directory that holds your Dockerfile. Replace the account ID and region with your own.
aws ecr create-repository \
--repository-name web-app \
--image-scanning-configuration scanOnPush=true \
--image-tag-mutability IMMUTABLE
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin \
123456789012.dkr.ecr.us-east-1.amazonaws.com
docker build -t web-app:1.0.0 .
docker tag web-app:1.0.0 \
123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0
docker push \
123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0The login token from get-login-password expires after 12 hours, so scripts must log in again on each run.
Common errors
| Symptom | Likely cause |
|---|---|
| `no basic auth credentials` on push | Docker login expired or used the wrong registry host |
| `repository does not exist` | The repository was not created, or the region differs from the URI |
| Task fails with `CannotPullContainerError` | Wrong image URI or tag, or the execution role lacks ECR permissions |
| Image pull times out | The task has no route to ECR, for example private subnets with no NAT gateway or VPC endpoints |
Exercise
A teammate pushes a fix and reuses the tag 1.0.0 in a repository with immutable tags. What happens, and what should they do?
Show answer
The push is rejected because immutable tags cannot be overwritten. They should build the fix under a new tag, such as 1.0.1, push it, and reference that tag in a new task definition revision.