Sub topic 1
From container image to running workload
This chapter follows one container from your laptop to a running process on Amazon ECS with AWS Fargate. It covers what an image is, what a running container adds to it, and which pieces ECS needs before it can start anything. Later chapters cover each piece in detail.
Prerequisites
This chapter assumes you already have the following:
- Basic Docker knowledge: you can write a Dockerfile, build an image, and run a container locally.
- Familiarity with core AWS concepts: IAM roles, VPCs, subnets, and security groups.
- Comfort using the AWS CLI and reading JSON.
- A basic understanding of HTTP services and health endpoints.
You do not need any ECS experience.
Images and containers
A container image is a read-only package. It holds a filesystem (your application code, its runtime, and its libraries) plus metadata such as the default command, the working directory, and the ports the application listens on. An image does nothing by itself.
A container is a running instance of an image. The container runtime starts an isolated process from the image, gives it a writable layer on top, and applies limits for CPU and memory. You can start many containers from the same image, and each one gets its own writable layer.
This split matters on ECS because the two halves are managed differently:
| Concept | What it is | Where it lives on AWS | Changes how often |
|---|---|---|---|
| Image | Immutable package of files and metadata | A registry, usually Amazon ECR | Once per build |
| Container | A running process created from an image | A Fargate task | Created and destroyed constantly |
| Configuration | Environment variables, secrets, CPU, memory, ports | A task definition | Once per release or setting change |
Treat the image as a build artifact and keep environment-specific values out of it. The same image should run in staging and production, and only the configuration changes.
Tags and digests
An image reference has three parts: the registry host, the repository name, and a tag or digest.
123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0A tag such as 1.0.0 or latest is a mutable label. Someone can push a different image under the same tag. A digest, which looks like sha256: followed by a hash, identifies exact content and never changes.
For ECS this has a practical consequence. When a task starts, ECS pulls whatever the tag points to at that moment. If you reuse latest, two tasks in the same service can run different code, depending on when each one started. Use a unique tag for each build, such as a version number or a Git commit hash, so that each task definition revision refers to one specific image.
The path from build to running task
The whole journey has five steps.
- Build the image from your Dockerfile.
- Push the image to a registry that ECS can reach. On AWS this is usually Amazon ECR.
- Register a task definition. It is a JSON document that names the image and describes how to run it.
- Run the task definition, either as a one-off task or as part of a long-lived service.
- Fargate finds capacity, pulls the image, starts the container, and reports its state.
Notice the loop between Fargate and ECR. The registry is not only a place you push to. Fargate pulls from it every time it starts a task, so the task needs network access to the registry and permission to read from it.
Building for the right architecture
Fargate runs tasks on either x86-64 or ARM64 (Graviton) processors. The image must match the architecture you request in the task definition. An image built on an ARM laptop, such as an Apple silicon Mac, is an ARM64 image by default. If the task definition asks for X86_64, the container fails at startup with an exec format error.
You can avoid the mismatch by building for the platform you intend to run:
docker buildx build \
--platform linux/amd64 \
-t 123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0 \
--load .The task definition declares the matching value in its runtimePlatform block:
{
"runtimePlatform": {
"cpuArchitecture": "X86_64",
"operatingSystemFamily": "LINUX"
}
}Use ARM64 in both places if you want Graviton, which has lower per-hour pricing. A later chapter covers that choice.
What ECS needs before it can start a container
Having an image in a registry is not enough. ECS needs answers to several questions, and each one maps to a field you will configure later.
| Question | Where you answer it |
|---|---|
| Which image, and how should it start? | `containerDefinitions` in the task definition |
| How much CPU and memory does it get? | Task-level `cpu` and `memory` |
| Who is allowed to pull the image and write logs? | The task execution role |
| What can the application itself call in AWS? | The task role |
| Which network will it join? | Subnets and security groups on the service or task |
| Where do logs go? | `logConfiguration` in the container definition |
| How is the container judged healthy? | `healthCheck`, plus the load balancer's check |
If any of these is missing or wrong, the task fails before your code runs, or soon after it starts. Most first-time failures fall into a few categories:
- Image cannot be pulled. The execution role lacks ECR permissions, the image tag does not exist, or the task has no route to ECR. A task in a private subnet needs a NAT gateway or VPC endpoints.
- Wrong architecture. The image and
runtimePlatformdisagree, as described above. - Container exits immediately. The process crashes or finishes. ECS sees the essential container stop and marks the task stopped.
- Health checks fail. The application starts but does not answer on the expected port and path, so ECS replaces it.
One-off tasks and services
Once a task definition exists, ECS offers two ways to run it.
A standalone task runs once and stops when its container exits. This suits batch jobs, migrations, and scripts. You start it with aws ecs run-task, and nothing restarts it if it fails.
A service keeps a desired number of copies of the task running. If a task crashes or its health check fails, the service starts a replacement. A service can also register tasks with a load balancer and roll out new versions. Web applications and APIs normally run as services.
The task definition is the same in both cases. The difference is whether you want a process that finishes or a process that stays up.
A quick end-to-end check
This sequence pushes an image to ECR. It assumes the repository web-app already exists and Docker is running.
aws ecr get-login-password --region us-east-1 \
| docker login --username AWS --password-stdin \
123456789012.dkr.ecr.us-east-1.amazonaws.com
docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/web-app:1.0.0
aws ecr describe-images \
--repository-name web-app \
--image-ids imageTag=1.0.0 \
--region us-east-1The first command exchanges your AWS credentials for a short-lived registry password. The last command confirms that the tag exists and shows its digest, which is a good habit before you register a task definition that refers to it.
Common mistakes
- Reusing a mutable tag such as
latest, which makes releases impossible to trace and roll back. - Building on an ARM machine and deploying to an x86-64 task.
- Baking secrets or environment-specific settings into the image.
- Expecting a standalone task to restart itself.
- Forgetting that the image pull happens from inside your VPC, so network and permissions both matter.
Exercise
You build an image on an Apple silicon laptop and push it to ECR as web-app:1.0.0. The task definition sets cpuArchitecture to X86_64. The task stops almost immediately and the logs show "exec format error". What went wrong, and how do you fix it?
Show answer
The image was built for ARM64, which is the default on Apple silicon, but the task definition requests X86_64. The container runtime cannot execute the ARM binaries on an x86-64 host. Fix it either by rebuilding with docker buildx build --platform linux/amd64 and pushing a new tag, or by changing runtimePlatform to ARM64 so it matches the image. Then register a new task definition revision and update the service.
Exercise
Two tasks in the same service are running different versions of your code, although the service uses a single task definition revision that names web-app:latest. Why can this happen, and what should you change?
Show answer
The tag latest is mutable. Each task pulls whatever latest points to when that task starts, so tasks started at different times can get different images. Use a unique tag for every build, such as a version number or commit hash, and reference that tag in the task definition. Each revision then points to exactly one image.