Containers: ECR, ECS and Fargate
A container packs your app with everything it needs (runtime, libraries) into one image that runs the same everywhere. You describe it in a Dockerfile.
Containers solve the "it works on my machine" problem: the app and everything it needs are packaged into one image that runs identically on a laptop and in the cloud. AWS offers a registry to store images (ECR) and services to run them (ECS, with Fargate so you don't manage the servers underneath). Many companies run their Node.js, Java and Python services this way.
Example
# Dockerfile for a small Node.js API
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]The AWS pieces
- ECR (Elastic Container Registry) - a private store for your images.
- ECS (Elastic Container Service) - runs and restarts containers for you.
- Fargate - runs ECS containers without you managing any servers (you choose CPU and memory per task).
- EKS - managed Kubernetes, for teams that already use Kubernetes.
Push an image to ECR
Example
aws ecr create-repository --repository-name orders-api
aws ecr get-login-password --region ap-south-1 | \
docker login --username AWS --password-stdin 123456789012.dkr.ecr.ap-south-1.amazonaws.com
docker build -t orders-api .
docker tag orders-api:latest 123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders-api:latest
docker push 123456789012.dkr.ecr.ap-south-1.amazonaws.com/orders-api:latestRun it on ECS with Fargate
- A task definition says which image, how much CPU/memory, which port, environment variables and secrets.
- A service keeps N copies of the task running and replaces any that crash.
- Put the service behind an Application Load Balancer, and add service auto scaling on CPU or requests.
- New version? Push a new image tag and update the service - ECS replaces tasks one by one with no downtime.
Lambda or containers? Lambda is simplest for short, event-driven jobs (up to 15 minutes each). Containers suit long-running apps, websockets, and code that already runs in Docker.
Key points
- Images live in ECR; ECS runs containers; Fargate removes server management.
- A task definition describes the container; a service keeps copies running.
- Lambda for short event jobs, containers for long-running apps.
📝 Test yourself
1. Which service stores your Docker images?
2. What does Fargate remove?
3. Fill in the ECS part that keeps a number of tasks running:
ECS
