🚀 **Deploy your Django app with Docker & DockerHub on AWS EC2!** This step-by-step guide will teach you how to **containerize your Django app**, push it to **DockerHub**, and deploy it on an **AWS EC2 Virtual Machine**. By the end of this tutorial, you'll have a fully functional Django app running on a cloud server with **Nginx & Gunicorn** for production. — ## **🔹 What You'll Learn in This Video:** ✅ How to **Dockerize a Django App** ✅ How to **Push a Docker Image to DockerHub** ✅ How to **Set Up an AWS EC2 Virtual Machine** ✅ How to **Pull and Run the Docker Image on EC2** ✅ How to **Set Up Nginx as a Reverse Proxy** ✅ How to **Run Django in Production with Gunicorn** — ## **🔹 Prerequisites:** ✔️ **Basic Knowledge of Django & Docker** ✔️ **AWS Account** ([Sign Up Here](https://aws.amazon.com/free/)) ✔️ **GitHub Repository with Django Code** ✔️ **Docker Installed Locally** ([Get Docker](https://www.docker.com/get-started)) — ## **🔹 Step 1: Create a Dockerfile for Django** Inside your Django project folder, create a file named **`Dockerfile`**: “`dockerfile
# Use an official Python runtime as a base image
FROM python:3.9 # Set the working directory
WORKDIR /app # Copy the project files
COPY . /app/ # Install dependencies
RUN pip install –no-cache-dir -r requirements.txt # Expose the application port
EXPOSE 8000 # Start the Django application using Gunicorn
CMD ["gunicorn", "–bind", "0.0.0.0:8000", "your_project.wsgi:application"]
“` — ## **🔹 Step 2: Create a `docker-compose.yml` (Optional)** To simplify container management, create **`docker-compose.yml`**: “`yaml
version: '3.8' services: web: build: . ports: – "8000:8000" volumes: – .:/app environment: – DEBUG=False depends_on: – db db: image: postgres:13 environment: POSTGRES_USER: django POSTGRES_PASSWORD: django POSTGRES_DB: django_db
“` — ## **🔹 Step 3: Build and Test Your Docker Image Locally** Run the following command in your Django project directory: “`bash
docker build -t your-django-app .
“` Once the build is complete, test it by running: “`bash
docker run -p 8000:8000 your-django-app
“` ✅ Open `http://localhost:8000` to check if the app is running. — ## **🔹 Step 4: Push the Docker Image to DockerHub** 1️⃣ Log in to DockerHub: “`bash
docker login
“`
2️⃣ Tag your image: “`bash
docker tag your-django-app your-dockerhub-username/your-django-app:latest
“`
3️⃣ Push the image to DockerHub: “`bash
docker push your-dockerhub-username/your-django-app:latest
“`
✅ Your image is now stored in DockerHub! — ## **🔹 Step 5: Launch an AWS EC2 Virtual Machine** 1️⃣ **Go to AWS EC2 Console** 2️⃣ Click **Launch Instance** 3️⃣ Choose **Ubuntu 22.04 LTS** 4️⃣ Select **t2.micro (Free Tier)** 5️⃣ Configure **Security Group**: – Allow **SSH (port 22)** – Allow **HTTP (port 80)** – Allow **HTTPS (port 443)** 6️⃣ Click **Launch** and **download the key pair (`.pem` file)** — ## **🔹 Step 6: Connect to AWS EC2 via SSH** 🔹 **For Mac/Linux Users:** “`bash
chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@your-ec2-public-ip
“`
🔹 **For Windows Users (Using PuTTY):** 1️⃣ Convert `.pem` to `.ppk` using **PuTTYgen** 2️⃣ Open **PuTTY**, enter **EC2 Public IP**, and load the `.ppk` key 3️⃣ Click **Open** and log in as `ubuntu` — ## **🔹 Step 7: Install Docker on AWS EC2** Once connected to your EC2 instance, install Docker: “`bash
sudo apt update && sudo apt upgrade -y
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
“` — ## **🔹 Step 8: Pull & Run Your Docker Image on EC2** 1️⃣ Log in to DockerHub: “`bash
docker login
“`
2️⃣ Pull your Docker image: “`bash
docker pull your-dockerhub-username/your-django-app:latest
“`
3️⃣ Run the container: “`bash
docker run -d -p 8000:8000 your-dockerhub-username/your-django-app
“`
✅ Your Django app is now running on **port 8000** on AWS EC2! — ## **🔹 Step 9: Set Up Nginx as a Reverse Proxy** 1️⃣ Install Nginx: “`bash
sudo apt install nginx -y
“`
2️⃣ Configure Nginx: “`bash
sudo nano /etc/nginx/sites-available/django
“`
3️⃣ Add this configuration: “`
server { listen 80; server_name your-ec2-public-ip your-domain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
}
“`
4️⃣ Enable the configuration and restart Nginx: “`bash
sudo ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled
sudo systemctl restart nginx
“`
✅ Your Django app is now **accessible via HTTP on EC2**! — ### **🔹 Hashtags:** #Django #Docker #DockerHub #AWS #EC2 #Nginx #Gunicorn #DjangoDeployment #DevOps #CloudComputing #AWSDeployment