This project outlines the process of containerizing a simple HTML page using Nginx as a web server, pushing the Docker image to AWS ECR, and deploying it on AWS ECS (Elastic Container Service). This approach leverages the scalability and management features of AWS services while utilizing Docker for containerization.
This repository contains a basic Docker project that serves a static HTML page using Nginx. The workflow includes:
- Creating a simple HTML page (
index.html) - Configuring Nginx to serve the page (
nginx.conf) - Writing a
Dockerfileto containerize the Nginx setup - Pushing the image to AWS ECR (Elastic Container Registry)
- Deploying the Docker container using AWS ECS
Before starting, make sure you have the following set up:
- An AWS account
- AWS CLI installed and configured
- Docker installed on your local machine
- Nginx (used as the web server)
- Docker (for containerization)
- AWS CLI (for interaction with AWS services)
- AWS ECS and ECR (for container orchestration and image storage)
-
Launch an EC2 instance: Choose a suitable AMI and instance type. For development purposes, a t2.micro can often suffice.
-
Install Docker and AWS CLI on the instance:
Install Docker:
sudo yum update -y sudo yum install docker -y sudo service docker start sudo usermod -a -G docker ec2-user
Install AWS CLI:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip awscliv2.zip sudo ./aws/install aws configure
Ensure Docker is running:
sudo service docker start
Create a simple HTML file named index.html with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Hello, Docker!</title>
</head>
<body>
<h1>Welcome to Dockerized Nginx!</h1>
</body>
</html>Configure Nginx to serve the index.html page by creating the following nginx.conf file:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
}
}Write a Dockerfile that builds from the nginx:alpine image for minimal footprint, copies configuration and content files, and sets the default command to run Nginx.
Here is an example Dockerfile:
# Use the official Nginx base image
FROM nginx:alpine
# Copy the custom Nginx configuration file
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy the HTML page
COPY index.html /usr/share/nginx/html/index.html
# Expose port 80
EXPOSE 80
# Start Nginx when the container starts
CMD ["nginx", "-g", "daemon off;"]Build the Docker image locally and test by running the container to ensure the setup works as expected. To build the image, run the following command in your terminal:
docker build -t my-nginx-app .Login to Docker Hub and push the image:
docker login
docker tag my-nginx-app arpit1605/my-nginx-app:latest
docker push arpit1605/my-nginx-app:latest-
Create an ECR repository via the AWS Management Console or CLI.
-
Login to ECR from your EC2 instance:
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin 975050024946.dkr.ecr.us-west-2.amazonaws.com -
Tag and Push the Image to ECR using the commands outlined
docker tag my-nginx-app:latest 975050024946.dkr.ecr.us-west-2.amazonaws.com/arpit/nginx:latest docker push 975050024946.dkr.ecr.us-west-2.amazonaws.com/arpit/nginx:latest
-
Create a Cluster in AWS ECS:
- Use the AWS Management Console to create a cluster.
-
Create a Task Definition:
- Define your task to use the image you pushed to ECR.
-
Create a Service:
- Deploy the task to your ECS cluster by creating a service.
-
Access the Service:
- Attach a load balancer to the service if necessary and access the Nginx page via the Load Balancer DNS.
Once deployed, you can access the application via the Load Balancer DNS (if an ALB is used) or directly through the public IP address of the ECS service.
This guide provides a detailed pathway from development to deployment for a static web page using Nginx, Docker, and AWS's container services. The process ensures scalability, reliability, and ease of deployment in a production environment, suitable for both small projects and larger, more dynamic deployments.