Amazon Elastic Container Service (ECS) example
Amazon Elastic Container Service (ECS) is a fully managed container orchestration service provided by AWS. It allows you to run and manage Docker containers on a cluster of EC2 instances or with AWS Fargate, a serverless compute engine for containers.
Here’s a step-by-step example to run a container using ECS:
Prerequisites
- AWS Account: Ensure you have an active AWS account.
- AWS CLI: Install and configure the AWS CLI on your local machine.
- Docker: Install Docker on your local machine to build container images.
Steps to Run a Container Using ECS
1. Create a Docker Image
-
Dockerfile: Create a simple Dockerfile for a sample application. For this example, we’ll use a simple Node.js application.
# Use the official Node.js image from the Docker Hub FROM node:14 # Create and change to the app directory WORKDIR /usr/src/app # Copy the application code to the container COPY . . # Install dependencies RUN npm install # Expose the port the app runs on EXPOSE 8080 # Run the app CMD ["node", "app.js"]
-
Application Code: Create a simple
app.js
file.const http = require('http'); const hostname = '0.0.0.0'; const port = 8080; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
-
Build Docker Image:
docker build -t my-node-app .
-
Push Docker Image to Amazon ECR:
-
Create an ECR repository:
aws ecr create-repository --repository-name my-node-app
-
Authenticate Docker to your Amazon ECR registry:
aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com
-
Tag the Docker image:
docker tag my-node-app:latest <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest
-
Push the Docker image:
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest
-
2. Set Up ECS
-
Create an ECS Cluster:
aws ecs create-cluster --cluster-name my-cluster
-
Create a Task Definition:
{ "family": "my-task-def", "containerDefinitions": [ { "name": "my-node-app", "image": "<account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest", "essential": true, "portMappings": [ { "containerPort": 8080, "hostPort": 8080 } ] } ], "requiresCompatibilities": [ "EC2" ], "networkMode": "bridge", "memory": "512", "cpu": "256" }
Save this as
task-def.json
and register it with ECS:aws ecs register-task-definition --cli-input-json file://task-def.json
-
Run the Task:
aws ecs run-task --cluster my-cluster --task-definition my-task-def
3. Set Up Networking
- Create an EC2 Instance: Ensure the instance is part of the ECS cluster and has Docker and ECS agent installed.
- Security Groups: Open port 8080 in the security group associated with the ECS instances.
4. Verify the Deployment
- Check the ECS Console: Verify that the task is running.
- Access the Application: Obtain the public IP of the EC2 instance and navigate to
http://<ec2-instance-public-ip>:8080
in your browser. You should see "Hello World".
Using AWS Fargate
To use AWS Fargate instead of EC2 instances:
-
Modify Task Definition:
{ "family": "my-task-def", "containerDefinitions": [ { "name": "my-node-app", "image": "<account-id>.dkr.ecr.<region>.amazonaws.com/my-node-app:latest", "essential": true, "portMappings": [ { "containerPort": 8080, "hostPort": 8080 } ] } ], "requiresCompatibilities": [ "FARGATE" ], "networkMode": "awsvpc", "memory": "512", "cpu": "256" }
-
Create a Fargate Service:
aws ecs create-service --cluster my-cluster --service-name my-service --task-definition my-task-def --desired-count 1 --launch-type FARGATE --network-configuration "awsvpcConfiguration={subnets=[<subnet-id>],securityGroups=[<security-group-id>],assignPublicIp=ENABLED}"