DEV Community

Cover image for Deploying a Dockerized Web App on AWS Using ECS and Fargate: A Step-by-Step Guide
Nada Ahmed
Nada Ahmed

Posted on

Deploying a Dockerized Web App on AWS Using ECS and Fargate: A Step-by-Step Guide

In this tutorial, I'll walk you through the process of deploying a simple Dockerized web app on AWS using Elastic Container Service (ECS) and Fargate.

Step 1: Create a Dockerized Web App

  1. Write a Simple HTML Web App Create an index.html file
<html>
<head>
   <t1>This is simple website</t1>

</head>
<body>

    <h1>Hello Users</h1>

    <p>This is a simple website hosted using Docker on AWS ECS</p>

</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Image description

  1. Create a Dockerfile This file tells Docker how to package your web app into a container.
FROM nginx:alpine

COPY ./index.html /usr/share/nginx/html/index.html

EXPOSE 80
Enter fullscreen mode Exit fullscreen mode

Image description

  1. Build the Docker Image Run the following command in your terminal:
   docker build -t my-webapp .
Enter fullscreen mode Exit fullscreen mode

Image description

Step 2: Push Your Docker Image to AWS ECR (Elastic Container Registry)

Requirements:
-Make sure you have AWS CLI installed.
-Configure the AWS CLI with your credentials by running:

aws configure
Enter fullscreen mode Exit fullscreen mode

This command will prompt you to enter your AWS Access Key ID, Secret Access Key, default region, and output format.

  1. Create a Repository in Amazon ECR Go to Amazon ECR in the AWS Console and create a new repository called my-simple-website.You can choose to make this repository private or public based on your needs. Image description
  2. Authenticate Docker to AWS ECR Use the following command to log in:
   aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <ECR_URI>
Enter fullscreen mode Exit fullscreen mode
  1. Tag the Docker Image Tag your image for ECR:
   docker tag my-webapp:latest <ECR_URI>/my-webapp:latest
Enter fullscreen mode Exit fullscreen mode
  1. Push the Docker Image to ECR Finally, push your image:
   docker push <ECR_URI>/my-simple-website:latest
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Step 3: Set Up ECS and Fargate

  1. Create an ECS Cluster Go to the Amazon ECS Console and create a new Fargate cluster where your Docker containers will run.

Image description

Image description

Image description

  1. Create a Task Definition In ECS, create a Task Definition for Fargate using the image you just pushed to ECR (<ECR_URI>/my-simple-website:latest). Set memory and CPU configurations as needed.

Image description

Image description

Image description

Image description

Image description

  1. Create an ECS Service After defining the task, create an ECS Service to manage the running tasks (containers).

Image description
Image description

Image description
You can configure the ALB with a target group for your ECS service.
Make sure that the security group associated with your ALB has a rule that allows inbound traffic on the desired ports (e.g., HTTP port 80 or HTTPS port 443).
Image description


Step 4: Access Your Web App

  1. Get the Public URL or dns of the Load Balancer

    This URL is how you will access your web app.

  2. Open the URL in Your Browser

    When you navigate to the URL, you should see your web app running with the message "Hello World!" displayed.

Image description

Conclusion

Deploying a Dockerized web app on AWS using Elastic Container Service (ECS) and Fargate is an efficient way to manage your applications. This tutorial walked you through creating a simple HTML web app, building a Docker image, pushing it to Amazon Elastic Container Registry (ECR), and setting up an Application Load Balancer (ALB) for internet access. With ECS and Fargate, you can easily scale and manage your services, allowing you to focus on development without the hassle of server management.

Top comments (0)