🐳 How to Dockerize a Django App (Even If You’re New to Django or Docker)
“Docker + Django = too complex?”
Nope — not when broken down into easy steps. Here’s your friendly guide to build your app and run it in Docker.
🚀 What You’ll Learn
- Create a basic Django app from scratch
- Write a Dockerfile (no mystery!)
- Use Docker Compose for simplicity
- Run your Django app inside Docker — on Windows or Mac/Linux
🧱 Step 1: Create Your Django App
Open your terminal and type:
mkdir docker_django_demo
cd docker_django_demo
# Set up a virtual environment
python -m venv venv
# On Windows:
venv\Scripts\activate
# On Mac/Linux:
# source venv/bin/activate
# Install Django
pip install django
# Start a Django project in the current folder
django-admin startproject mysite .
Then test it:
python manage.py runserver
Go to http://127.0.0.1:8000 — you should see the Django welcome page! 🎉
📦 Step 2: Save Your Dependencies
pip freeze > requirements.txt
This file ensures Docker will install exactly what you need.
🐳 Step 3: Create Your Dockerfile
Create a file named Dockerfile in the same folder:
FROM python:3.11-slim
# Avoids writing .pyc files and Python buffering
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Copy and install dependencies
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
# Copy the Django project files
COPY . /app/
# Expose port 8000 inside Docker
EXPOSE 8000
# Command to run Django server
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
🧬 Step 4: Add Docker Compose
Create docker-compose.yml:
version: '3.9'
services:
web:
build: .
ports:
- "8000:8000"
✅ Tip (Windows users): Leave out volumes: to avoid file-mounting issues.
🛠️ Step 5: Build & Run with Docker
Build the image
docker-compose build
➡️ This reads the Dockerfile and packages your app.
Start the container
docker-compose up
Now head to http://localhost:8000 — your Django app is running inside Docker!
🧽 Step 6: Stop the Container
docker-compose down
This cleans up the running container when you're done.
🧠 Bonus Tips
Use a .dockerignore file to skip unnecessary files (e.g. venv/, pycache/)
Consider using .env files for secret keys and environment configs
🎉 Final Thoughts
Congratulations! You’ve successfully containerized a Django app — portable, consistent, and ready to run anywhere.
If this helped, leave a comment or reaction below.
✍️ Written by Anusha Kuppili
📺 Also explained in my YouTube video: Dockerize Django App