This project is a Django application configured with Docker for both development and production environments. It includes PostgreSQL for the database, Memcached for caching, and Nginx as a reverse proxy in production.
.
├── core/ # Django project core
│ ├── settings-dev.py # Development settings
│ ├── settings-prod.py # Production settings
│ └── urls.py # URL configurations
├── nginx/ # Nginx configuration
│ ├── Dockerfile # Nginx Dockerfile
│ └── nginx.conf # Nginx server configuration
├── templates/ # HTML templates
│ └── home.html # Homepage template
├── user/ # Django app
├── docker-compose.dev.yml # Docker Compose for development
├── docker-compose.prod.yml # Docker Compose for production
├── Dockerfile.dev # Development Dockerfile
├── Dockerfile.prod # Production Dockerfile
├── manage.py # Django management script
└── requirements.txt # Python dependencies
- Docker
- Docker Compose
-
Clone the repository:
git clone <repository-url> cd <project-directory>
-
Start the development environment:
docker compose -f docker-compose.dev.yml up --build
-
Access the development server:
- Application: http://localhost:8000
- Admin interface: http://localhost:8000/admin
- Hot-reload enabled (code changes reflect immediately)
- Debug mode enabled
- PostgreSQL database (not exposed to host)
- Memcached for caching
- All logs visible in console
- Django: 8000 (exposed)
- PostgreSQL: 5432 (internal only)
- Memcached: 11211 (internal only)
- Docker
- Docker Compose
- Ubuntu Server
-
Clone the repository on your production server:
git clone <repository-url> cd <project-directory>
-
Configure environment variables: Create a
.env
file in the project root with the following variables:DJANGO_SECRET_KEY=your-secret-key-here POSTGRES_DB=django_db_prod POSTGRES_USER=django_user_prod POSTGRES_PASSWORD=your-secure-password EMAIL_HOST_USER=[email protected] EMAIL_HOST_PASSWORD=your-email-password
-
Start the production environment:
docker compose -f docker-compose.prod.yml up --build -d
-
Access the production server:
- Application: http://localhost
- Admin interface: http://localhost/admin
- Nginx as reverse proxy
- Gunicorn as WSGI server
- PostgreSQL database (not exposed)
- Memcached for caching
- Static/Media files served through Nginx
- SMTP email configuration
- Debug mode disabled
- Optimized for performance
- Nginx: 80 (exposed)
- Gunicorn: 8000 (internal only)
- PostgreSQL: 5432 (internal only)
- Memcached: 11211 (internal only)
# Development
docker compose -f docker-compose.dev.yml exec web python manage.py makemigrations
# Production
docker compose -f docker-compose.prod.yml exec web python manage.py makemigrations
# Development
docker compose -f docker-compose.dev.yml exec web python manage.py migrate
# Production
docker compose -f docker-compose.prod.yml exec web python manage.py migrate
# Development
docker compose -f docker-compose.dev.yml exec web python manage.py createsuperuser
# Production
docker compose -f docker-compose.prod.yml exec web python manage.py createsuperuser
- Static files are automatically served by Django's development server
- Media files are stored in the
media
directory
- Static files are collected to the
static
directory and served by Nginx - Media files are stored in the
media
directory and served by Nginx - Run collectstatic manually if needed:
docker compose -f docker-compose.prod.yml exec web python manage.py collectstatic --noinput
# All services
docker compose -f docker-compose.prod.yml logs
# Specific service (e.g., web, nginx, db)
docker compose -f docker-compose.prod.yml logs web
docker compose -f docker-compose.prod.yml exec db pg_dump -U django_user_prod django_db_prod > backup.sql
cat backup.sql | docker compose -f docker-compose.prod.yml exec -T db psql -U django_user_prod -d django_db_prod
-
In production:
- Database port is not exposed outside Docker network
- All services communicate through Docker's internal network
- Debug mode is disabled
- Secure passwords are used for database and email
-
Environment Variables:
- Never commit
.env
files - Use strong, unique passwords
- Regularly rotate secrets
- Never commit
-
If static files are not serving:
docker compose -f docker-compose.prod.yml exec web python manage.py collectstatic --noinput
-
If database connections fail:
- Check if PostgreSQL container is running
- Verify database credentials in settings
- Ensure migrations are applied
-
If Nginx returns 502 Bad Gateway:
- Check if Gunicorn is running
- Verify Nginx configuration
- Check logs for both services
- Create a new branch for your feature
- Make your changes
- Test in development environment
- Submit a pull request
[Your License Here]