π’ Docker Orchestration with AWS ECS β Full Guide (with Node.js Production Testing & Tips)
π§ What is Docker Orchestration?
Docker Orchestration means managing the lifecycle of containers:
- π¦ Deploying
- π Scaling (up/down)
- π₯ Load balancing
- π οΈ Updating
- π₯ Handling failures
Popular Orchestration Tools:
- Docker Swarm π
- Kubernetes (K8s) βΈοΈ
- Amazon ECS π
- Amazon EKS (for Kubernetes) βοΈ
We'll focus on AWS ECS using Fargate (serverless) and EC2 launch type.
π οΈ Step 1: Setting Up AWS Account
- Go to π https://aws.amazon.com/
- Sign up for a free tier account (needs credit/debit card π³).
- Enable MFA for security π.
- Set region (e.g.,
us-east-1
,ap-south-1
) π. - Create an IAM user with AdministratorAccess if not using root.
π§΄ Step 2: Setting up Amazon ECR (Elastic Container Registry)
ECR is AWS's private Docker registry.
πͺ Steps:
- AWS Console β Search
ECR
β Create repository π - Configure:
-
Name:
my-app
- Visibility:
Private
π - Tag immutability: Enabled β
- Push Docker Image to ECR:
# Authenticate Docker to ECR
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <your-account-id>.dkr.ecr.us-east-1.amazonaws.com
# Build and Tag image
docker build -t my-app .
# Tag with ECR repo URI
docker tag my-app:latest <your-account-id>.dkr.ecr.us-east-1.amazonaws.com/my-app:latest
# Push image
docker push <your-account-id>.dkr.ecr.us-east-1.amazonaws.com/my-app:latest
π§° Step 3: Setting up ECS Cluster
An ECS Cluster is where your containers run.
πͺ Steps:
- AWS Console β ECS β Create Cluster
- Choose:
-
Networking only
β Fargate -
EC2 + Networking
β EC2- Cluster Name:
my-app-cluster
- Proceed β ECS will create VPC and subnets π°οΈ
- Cluster Name:
π Step 4: ECS Task Definition Setup
A Task Definition = Docker container blueprint.
π Key Components:
- Task Role (IAM)
- Docker Image from ECR
- Port mappings (e.g.,
80:3000
) - CPU & Memory: 256 CPU, 512 MiB RAM
- Log configuration: AWS CloudWatch
- Environment variables, secrets π
- Health checks:
/health
or/
πͺ Steps:
- ECS β Task Definitions β Create new
- Launch Type: Fargate
- Add container:
- Name:
my-app
- Image:
<ECR Image URL>
- Port:
3000
- Logging: awslogs, with group:
/ecs/my-app
βοΈ Step 5: ECS Service Setup with Load Balancer
The Service handles:
- Keeping tasks running
- Restarting failed containers
- Auto-scaling
πͺ Steps:
ECS β Your Cluster β Create Service
Launch Type: Fargate
Choose Task Definition
Desired tasks:
1
or moreAttach Load Balancer:
- ALB β New or existing
- Create Target Group β port
3000
- Health Check path:
/health
- Listener on port
80
β forward to Target Group
- Enable Auto Scaling (optional)
π§ͺ Step 6: Testing Our Service (π₯ with Node.js Tips!)
β Basic Test
- Go to EC2 > Load Balancers β Copy DNS URL π
- Visit in browser β You should see your app π
- Confirm task is running: ECS > Cluster > Tasks
- Logs: CloudWatch >
/ecs/my-app
π
β Deep Testing (For Node.js Applications)
πΉ 1. Check container logs:
aws logs get-log-events \
--log-group-name "/ecs/my-app" \
--log-stream-name "<your-log-stream>"
Or via CloudWatch Console.
πΉ 2. CURL/HTTP test:
curl http://<load-balancer-dns>/health
β Ensure response is
200 OK
. If not, ECS will kill and restart your task.
πΉ 3. Test environment variables:
Add this in your Node.js app:
console.log('ENV:', process.env.NODE_ENV);
Set "NODE_ENV": "production"
in Task Definition.
πΉ 4. Debug failing deployments:
Check:
- Task status (
Stopped?
) - View Reason (
StoppedReason
) - Logs (
CloudWatch
) - Health Check (endpoint must return 2xx)
πΉ 5. Enable ECS Exec:
Run shell commands inside the running container:
aws ecs execute-command \
--cluster my-app-cluster \
--task <task-id> \
--container my-app \
--interactive \
--command "/bin/sh"
Requires enabling ECS Exec & permissions.
π§Ή Step 7: Clean up Resources (π§½ Avoid Charges!)
β Checklist:
- Stop ECS Service
- Delete Tasks
- Delete Load Balancer
- Delete Target Groups
- Delete ECR Repository (optional)
- Delete Cluster
- Delete VPC (if created)
- Delete CloudWatch logs
aws ecr delete-repository --repository-name my-app --force
βοΈ Manual vs Automatic Orchestration
Feature | Manual | Automatic |
---|---|---|
Deploy new container π | CLI or Console | CI/CD + ECS Service Updates |
Scale app π | You change task count | Auto Scaling based on CPU/Memory/Requests |
Monitor and Heal π©Ί | Manual restart | ECS restarts crashed tasks |
Load Balancing π | Manually configure ELB | ECS auto-registers tasks to Target Groups |
Image Updates π | Push new tag and update task def | Use CodePipeline / GitHub Actions + Blue/Green |
β Real-World Production Tips (π‘ Especially for Node.js)
-
Use
.env.production
withdotenv
and pass via Task Definition - Reverse proxy with NGINX (optional for advanced setups)
-
Health Check Endpoint (
/health
): return200 OK
JSON, no DB calls -
Use
pm2
inside container for better process management (optional) -
Avoid console.log in production β Use
winston
orpino
- Enable Structured Logging β Send logs to CloudWatch
- Monitor memory & CPU metrics via CloudWatch
- Enable ECS Exec to debug running container
- Use HTTPS via ACM with Load Balancer
-
Auto-Deploy via GitHub Actions +
AWS CLI
orCodePipeline
- Set container limits (soft/hard memory) to avoid OOM crashes
- Use Secrets Manager for DB/API credentials π
- Test locally with
docker run -p 3000:3000 my-app
before push -
Set
NODE_ENV=production
for optimized performance -
Use a lightweight base image like
node:18-alpine
π§Ύ Summary Cheatsheet
Step | Task | Tool/Service |
---|---|---|
1οΈβ£ | Create AWS Account | AWS Console |
2οΈβ£ | Push Image to ECR | ECR, Docker CLI |
3οΈβ£ | Setup ECS Cluster | ECS |
4οΈβ£ | Define Task Definition | ECS |
5οΈβ£ | Create Service + Load Balancer | ECS + ALB |
6οΈβ£ | Test Your Application | Load Balancer URL |
7οΈβ£ | Cleanup | Console / CLI |
Top comments (0)