This project demonstrates a simple three-tier architecture deployed using Docker and Docker Compose on an AWS EC2 instance. The architecture consists of:
- Frontend: React application
- Backend: Node.js API
- Database: MySQL
- Dockerized frontend, backend, and database services.
- Deployment using Docker Compose.
- Nginx as a reverse proxy to expose the frontend.
- AWS account with a free-tier EC2 instance.
- Basic understanding of Docker, Docker Compose, and Nginx.
- SSH access to the EC2 instance.
git clone <repository-url>
cd <repository-folder>
Navigate to the frontend
directory and review the React app:
cd frontend
Dockerfile for Frontend:
FROM node:16
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
Navigate to the backend
directory and review the Node.js server:
cd backend
Dockerfile for Backend:
FROM node:16
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD ["node", "server.js"]
Uses a standard MySQL image:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: myapp
In the project root, the docker-compose.yml
file orchestrates the services:
version: '3'
services:
frontend:
build: ./frontend
ports:
- "3000:3000"
depends_on:
- backend
backend:
build: ./backend
ports:
- "4000:4000"
depends_on:
- db
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: myapp
ports:
- "3306:3306"
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
-
Launch an EC2 Instance:
- Use an Ubuntu AMI (free-tier eligible).
- Ensure the security group allows HTTP (port 80), and SSH (port 22).
-
Install Docker and Docker Compose:
sudo apt update sudo apt install -y docker.io sudo curl -L "https://github.com/docker/compose/releases/download/v2.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
-
Install Nginx:
sudo apt install -y nginx
-
Run
docker-compose
to deploy all services:sudo docker-compose up -d
-
Verify running containers:
sudo docker ps
Edit the default Nginx config:
sudo nano /etc/nginx/sites-available/default
Set up reverse proxy for the frontend:
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Restart Nginx:
sudo systemctl restart nginx
- Frontend: Visit
http://<EC2-public-IP>
. - Backend: Verify connectivity from the frontend.
- Database: Pre-configured to store data for the backend.
This project showcases how to deploy a three-tier architecture using Docker, Docker Compose, and Nginx on AWS EC2.
MIT License.
Feel free to modify this README.md
to suit your project.