Deploying a Django Web Application with Nginx and MySQL on Docker
Vatsal Gajjar
??Looking for Summer Internship 2025 | ?? ITNU'26 | ?? DevOps Enthusiast | ?? Cloud Computing | AWS | Docker | Kubernetes
Dockerization is about creating a portable ecosystem for your application. By combining Django, Nginx, and MySQL in a Docker setup, you ensure scalability, consistency, and ease of deployment. This guide will walk you through deploying a Django web application with Nginx, MySQL, and Docker Compose.
Overview of Our Architecture
Here’s what we’ll be deploying:
Step 1: Understanding the Dockerfile
Our Dockerfile creates a Docker image for the Django backend. Let’s break it down step-by-step:
FROM python:3.9
WORKDIR /app/backend
COPY requirements.txt /app/backend
RUN apt-get update \\\\
&& apt-get upgrade -y \\\\
&& apt-get install -y gcc default-libmysqlclient-dev pkg-config \\\\
&& rm -rf /var/lib/apt/lists/*
RUN pip install mysqlclient
RUN pip install --no-cache-dir -r requirements.txt
COPY . /app/backend
EXPOSE 8000
This Dockerfile creates an isolated, dependency-ready environment for running your Django app.
Step 2: Understanding docker-compose.yaml
Docker Compose simplifies multi-container applications. Here’s a detailed breakdown of our docker-compose.yaml:
领英推荐
version: "3.8"
Nginx Service
nginx:
build: ./nginx
image: nginx
container_name: "nginx_cont"
ports:
- "80:80"
restart: always
depends_on:
- django_app
networks:
- notes-app-nw
Django App Service
django_app:
build:
context: .
image: django_app
container_name: "django_cont"
ports:
- "8000:8000"
command: sh -c "python manage.py migrate --noinput && gunicorn notesapp.wsgi --bind 0.0.0.0:8000"
env_file:
- ".env"
depends_on:
- db
restart: always
healthcheck:
test: ["CMD-SHELL", "curl -f <https://localhost:8000/admin> || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
networks:
- notes-app-nw
Database Service
db:
image: mysql
container_name: "db_cont"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=test_db
volumes:
- ./data/mysql/db:/var/lib/mysql
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-uroot", "-proot"]
interval: 10s
timeout: 5s
retries: 5
start_period: 60s
networks:
- notes-app-nw
Network Configuration
networks:
notes-app-nw:
Step 3: Deployment Steps
Step 4: Common Pitfalls and Fixes
GitHub Repository
Access the full source code here.
Conclusion
Creadits to Shubham Londhe's Tutorial
Deploying a Django web application with Nginx and MySQL on Docker doesn’t have to be daunting. With a well-structured approach, you can build a scalable, production-ready stack in no time. If this guide helped you, let me know in the comments or share your deployment success story. Together, let’s build something incredible!
FOLLOW KIYA KYA !!!!!!