Containers on AWS: Docker Basics, Amazon ECS, EKS, Fargate, ECR, Elastic Beanstalk and Lightsail
Virtual Machines vs Containers
| Virtual machine (EC2) | Container (Docker) |
| Includes | Full operating system | App and its libraries only. Shares the host OS kernel |
| Size | Gigabytes | Megabytes |
| Start time | Minutes | Seconds |
| Isolation | Strong (separate OS) | Process level |
| Use | Traditional apps, full control | Microservices, consistent deployments |
A container packages your app with everything it needs, so it runs the same way on a laptop, a test server and production.
Docker in 5 Minutes
bash
# Dockerfile
# FROM python:3.12-slim
# WORKDIR /app
# COPY requirements.txt .
# RUN pip install -r requirements.txt
# COPY . .
# CMD ["python", "app.py"]
docker build -t myapp:1.0 .
docker run -p 8080:8080 myapp:1.0
docker ps
- Image: the read-only template. Container: a running copy of an image
- Registry: where images are stored (Docker Hub or Amazon ECR)
Amazon ECR: Your Image Library
bash
aws ecr create-repository --repository-name myapp
aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin 111111111111.dkr.ecr.ap-south-1.amazonaws.com
docker tag myapp:1.0 111111111111.dkr.ecr.ap-south-1.amazonaws.com/myapp:1.0
docker push 111111111111.dkr.ecr.ap-south-1.amazonaws.com/myapp:1.0
- Amazon ECR is a private, secure registry with image scanning and lifecycle rules
- Integrates with IAM, ECS and EKS
Orchestration: ECS and EKS
Running one container is easy. Running hundreds, keeping them healthy, scaling and updating them needs an orchestrator.
| Service | What it is | Choose it when |
| Amazon ECS (Elastic Container Service) | AWS's own simple container orchestrator | You want the easiest way to run containers on AWS |
| Amazon EKS (Elastic Kubernetes Service) | Managed Kubernetes | You already use Kubernetes or want open-source portability |
| ECS term | Meaning |
| Cluster | Group of capacity to run tasks |
| Task definition | Blueprint: image, CPU, memory, ports, environment |
| Task | A running instance of a task definition |
| Service | Keeps a desired number of tasks running, connects to a load balancer |
Where Do the Containers Run? EC2 or Fargate
| Launch type | You manage | Notes |
| EC2 | The servers that run containers | More control, can be cheaper at scale |
| AWS Fargate | Nothing about servers. Choose CPU and memory per task | Serverless containers. Pay for task resources and time. Great for beginners |
Other Ways to Run Apps
| Service | Good for |
| AWS Elastic Beanstalk | Upload your code (Java, Python, Node.js, PHP, .NET, Docker) and AWS sets up servers, load balancer and scaling. Fast for web apps |
| AWS App Runner | Run a web app or API from source code or a container image with almost no setup |
| Amazon Lightsail | Simple virtual servers, databases and load balancers with fixed monthly prices, for small sites and learners |
| AWS Lambda | Short event-driven code (previous lesson) |
| AWS Batch | Run batch computing jobs at any scale |
| AWS Outposts | AWS infrastructure in your own data center |
Which Compute Service Should I Pick?
| Need | Pick |
| Full control of OS and software | EC2 |
| Short code triggered by events, no servers | Lambda |
| Containers, easiest on AWS | ECS on Fargate |
| Kubernetes | EKS |
| Just upload my web app | Elastic Beanstalk or App Runner |
| Simple low-cost website | Lightsail |
| Big batch jobs | AWS Batch |
Run a Container on ECS Fargate (Outline)
- Push your image to ECR
- In ECS create a cluster (Fargate)
- Create a task definition with your image, 0.5 vCPU and 1 GB memory, and port 80
- Create a service with 2 tasks in two subnets and attach an Application Load Balancer
- Open the load balancer DNS name in the browser
- Update the image tag and let the service do a rolling update
Practice
- Build a small Docker image locally and run it
- Push it to a new ECR repository
- Run it on ECS Fargate behind a load balancer
- Deploy the same app to Elastic Beanstalk and compare the effort
Conclusion
Containers package apps consistently. Store images in ECR, run them with ECS or EKS, and choose Fargate for serverless containers. Beanstalk, App Runner and Lightsail are simpler ways to host apps. Pick the service that matches how much control you need.
Quick Summary: VM vs container; Docker; ECR; ECS and EKS; Fargate; other services; selection; outline.