Docker Image Push and Deploy on AWS ECS, ECR, and Fargate

1. Clone the Repository

First, clone the Flask Todo App repository from GitHub:

git clone https://github.com/nahidkishore/flask-todo-app.git

Navigate to the project directory:

cd flask-todo-app/

2. Build the Docker Image

Build your Docker image from the Dockerfile:

docker build -t flask-test-app .

3. Tag the Docker Image

Tag the Docker image to your AWS ECR repository. Replace <aws_account_id> and <region> with your actual AWS account ID and region.

docker tag flask-test-app:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/flask-test-app:latest

Example:

docker tag flask-test-app:latest 730335230104.dkr.ecr.us-east-1.amazonaws.com/flask-test-app:latest

4. Log in to AWS ECR

Log in to your AWS Elastic Container Registry (ECR):

aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.<region>.amazonaws.com

Example:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 730335230104.dkr.ecr.us-east-1.amazonaws.com

5. Push the Docker Image to AWS ECR

After logging in, push the Docker image to AWS ECR:

docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/flask-test-app:latest

Example:

docker push 730335230104.dkr.ecr.us-east-1.amazonaws.com/flask-test-app:latest

6. Create an ECS Cluster

Create an ECS cluster to run the container with AWS Fargate:

aws ecs create-cluster --cluster-name flask-cluster

7. Register a Fargate-Compatible Task Definition

Create a task-def.json file for your Fargate-compatible task definition, for example:

{
  "family": "flask-task",
  "requiresCompatibilities": ["FARGATE"],
  "networkMode": "awsvpc",
  "cpu": "256",
  "memory": "512",
  "containerDefinitions": [
    {
      "name": "flask-container",
      "image": "<aws_account_id>.dkr.ecr.<region>.amazonaws.com/flask-test-app:latest",
      "portMappings": [
        {
          "containerPort": 80,
          "protocol": "tcp"
        }
      ],
      "essential": true
    }
  ]
}

Register the task definition with ECS:

aws ecs register-task-definition --cli-input-json file://task-def.json

8. Create ECS Fargate Service

Create an ECS service with Fargate to run the task. Replace <subnet_id> and <security_group_id> with your actual values:

aws ecs create-service \
  --cluster flask-cluster \
  --service-name flask-service \
  --task-definition flask-task \
  --desired-count 1 \
  --launch-type FARGATE \
  --network-configuration "awsvpcConfiguration={subnets=[<subnet_id>],securityGroups=[<security_group_id>],assignPublicIp=ENABLED}"

Example:

aws ecs create-service \
  --cluster flask-cluster \
  --service-name flask-service \
  --task-definition flask-task \
  --desired-count 1 \
  --launch-type FARGATE \
  --network-configuration "awsvpcConfiguration={subnets=[subnet-0550bbaf9a86edbe5],securityGroups=[sg-0c8ff0a36ca22cefd],assignPublicIp=ENABLED}"

9. Verify the ECS Service

Check the running tasks in your cluster to verify the deployment:

aws ecs list-tasks --cluster flask-cluster

10. Access the Application

Once your task is running, the service will have a public IP if you have configured the network properly. You can access your application through the public IP assigned to your Fargate task.


This guide helps in pushing the Docker image to AWS ECR and deploying it on AWS ECS using Fargate as the launch type, ensuring that you have a fully managed container solution with AWS Fargate.