Django Docker-Ready Project Structure
Containerized Django with Docker Compose, Gunicorn, and Nginx. Ready for production deployment.
Project Directory
myproject/
manage.py
config/
__init__.py
settings/
__init__.py
base.py
local.py
production.py
Docker-specific settings
urls.py
wsgi.py
asgi.py
apps/
core/
__init__.py
apps.py
models.py
views.py
urls.py
migrations/
__init__.py
__init__.py
docker/
Docker configuration
django/
Dockerfile
Multi-stage build
entrypoint.sh
Wait for DB, migrate
start.sh
Gunicorn startup
nginx/
Dockerfile
nginx.conf
Reverse proxy config
default.conf
Server block
postgres/
init.sql
Initial DB setup
scripts/
Helper scripts
wait-for-it.sh
Wait for service
backup-db.sh
restore-db.sh
templates/
base.html
static/
css/
js/
staticfiles/
collectstatic output (gitignored)
media/
User uploads (volume mount)
requirements/
base.txt
local.txt
production.txt
gunicorn, psycopg2
docker-compose.yml
Development stack
docker-compose.prod.yml
Production overrides
.env.example
.env.docker
Docker-specific vars
.dockerignore
.gitignore
Makefile
Common commands
Why This Structure?
This structure separates Docker configs into docker/ with per-service subdirectories. Multi-stage Dockerfile keeps images small. Separate compose files for dev vs prod. Scripts folder for common operations like DB backups.
Key Directories
- docker/django/-Dockerfile, entrypoint, Gunicorn start script
- docker/nginx/-Nginx reverse proxy configuration
- scripts/-Helper scripts (wait-for-it, backups)
- staticfiles/-collectstatic output, served by Nginx
Docker Services
- django-Gunicorn serving the app on port 8000
- nginx-Reverse proxy, static files, SSL termination
- postgres-PostgreSQL database with persistent volume
- redis-Cache and session store (optional)
Getting Started
cp .env.example .env && cp .env.docker.example .env.dockerdocker-compose builddocker-compose up -ddocker-compose exec django python manage.py migratedocker-compose exec django python manage.py createsuperuser- Visit
http://localhost
Dockerfile Stages
- base-Python, system deps, create user
- builder-Install Python packages to wheels
- production-Copy wheels, minimal runtime image
Production Settings
DEBUG=False,ALLOWED_HOSTSfrom env- Database from
DATABASE_URLenv var - Static files served by Nginx (not Django)
SECRET_KEYfrom environmentSECURE_SSL_REDIRECT,CSRF_COOKIE_SECUREenabled
Trade-offs
- Learning curve-Docker adds complexity for newcomers
- Local dev speed-Container rebuilds slower than native
- Resource usage-Multiple containers need more RAM