Deploy a simple dockerized web app using AWS ECR & EC2
In this step-by-step guide, I’ll walk you through building a simple Docker container with an Nginx proxy, pushing it to AWS Elastic Container Registry (ECR), and hosting the application on an EC2 instance. Let’s get started!
Prerequisites
Before we begin, make sure you have the following:
- Docker installed on your local machine.(I’m running docker for windows)
- An AWS account with the necessary permissions to create ECR repositories and launch EC2 instances.
- AWS Command Line Interface (CLI) installed and configured on your local machine.
Step 1: Build the Docker Container with Nginx Proxy
Create a new directory for your Nginx configuration files. Within this directory, create an index.html
file with your desired content, and create an nginx.conf
file to configure Nginx. Here's a simple example:
index.html
<!DOCTYPE html>
<html>
<head>
<title>My Dockerized Nginx Proxy</title>
</head>
<body>
<h1>Hello from Nginx!</h1>
</body>
</html>
nginx.conf:
server {
listen 80;
server_name localhost;location / {
root /usr/share/nginx/html;
index index.html;
}
}
Next, create a Dockerfile
in the same directory:
Dockerfile:
# Use the official Nginx image as the base image
FROM nginx:latest
# Copy the Nginx configuration files to the container
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy the HTML content to the default Nginx web root
COPY index.html /usr/share/nginx/html/
Now, navigate to the directory containing these files in the terminal and build the Docker container (this command will also tag the container by passing in the -t parameter):
docker build -t my-nginx-proxy .
Step 2: Push the Docker Container to AWS ECR
2.1: Configure AWS CLI
Ensure your AWS CLI is properly configured with your AWS credentials. You can set it up by running:
aws configure
This will prompt you for your AWS key ID and secret key. Checkout IAM through the AWS console if you need these values.
2.2: Create an ECR Repository
Let’s create a new ECR repository using the AWS CLI:
aws ecr create-repository --repository-name my-nginx-proxy
This will give you an output containing the ECR repository URI, which we’ll use in the next step. Note that you can also do this through the AWS console UI by searching for ECR.
2.3: Tag and Push the Docker Image
Tag the Docker image locally with the ECR repository URI:
docker tag my-nginx-proxy:latest <ECR_REPOSITORY_URI>:latest
Then, log in to your ECR registry:
aws ecr get-login-password --region <AWS_REGION> | docker login --username AWS --password-stdin <ECR_REGISTRY>
Now, push the Docker image to ECR:
docker push <ECR_REPOSITORY_URI>:latest
Step 3: Host the Application on EC2
3.1: Launch an EC2 Instance
Using the AWS Management Console or AWS CLI, launch an EC2 instance with an appropriate Amazon Machine Image (AMI). For this simple project, I just used the Amazon Linux image with the T2.micro (free tier) sizing. Make sure the instance has internet access and is located in a security group that allows HTTP traffic (port 80).
3.2: Pull and Run the Docker Image on EC2
Connect to your EC2 instance using SSH and then pull the Docker image from ECR:
aws ecr get-login-password --region <AWS_REGION> | docker login --username AWS --password-stdin <ECR_REGISTRY>
docker pull <ECR_REPOSITORY_URI>:latest
Run the Docker container on the EC2 instance:
docker run -d -p 80:80 <ECR_REPOSITORY_URI>:latest
Now, your Nginx proxy application should be up and running on your EC2 instance. You can access it by entering your EC2 instance’s public IP address or domain name in a web browser.
Conclusion
Congratulations! You’ve successfully built a simple Docker container with an Nginx proxy, pushed it to AWS ECR, and hosted the application on an EC2 instance. This example demonstrates a basic setup, but you can extend and customize it according to your specific requirements. Happy containerizing and hosting!