How to Deploy a Docker app to ECS
Deploy a Docker app to Amazon ECS by using EC2.
Prerequisites
- AWS account with appropriate permissions
- Docker image ready for deployment
- AWS CLI installed and configured
- Docker installed locally
Step 1: Create ECR Repository
aws ecr create-repository --repository-name my-app --region us-east-1
Step 2: Build and Push Docker Image
<h1 id="heading-4">Get login token</h1>
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account-id>.dkr.ecr.us-east-1.amazonaws.com
<h1 id="heading-5">Build image</h1> docker build -t my-app .
<h1 id="heading-6">Tag image</h1> docker tag my-app:latest <account-id>.dkr.ecr.us-east-1.amazonaws.com/my-app:latest
<h1 id="heading-7">Push image</h1> docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/my-app:latest
Step 3: Create ECS Cluster
aws ecs create-cluster --cluster-name my-cluster --region us-east-1
Step 4: Create Task Definition
Create task-definition.json:
{
"family": "my-app", "networkMode": "awsvpc", "requiresCompatibilities": ["EC2"], "containerDefinitions": [ { "name": "my-app", "image": "<account-id>.dkr.ecr.us-east-1.amazonaws.com/my-app:latest", "portMappings": [ { "containerPort": 80, "protocol": "tcp" } ], "memory": 512, "cpu": 256 } ] }
Register the task definition:
aws ecs register-task-definition --cli-input-json file://task-definition.json --region us-east-1
Step 5: Create EC2 Instances
Launch EC2 instances with the ECS-optimized AMI and configure them to join your cluster.
Step 6: Create Service
aws ecs create-service \
--cluster my-cluster \ --service-name my-app-service \ --task-definition my-app \ --desired-count 1 \ --launch-type EC2 \ --region us-east-1
Your Docker app is now deployed to ECS!
