|
1 |
| -# django_tutorial using DRF and DRF Spectacular OpenAPI |
2 |
| -Django DRF with SQLite |
3 |
| -Basic CRUD API for Employees and Department using Django DRF. |
4 |
| -Deployed using Gunicorn Server |
| 1 | +# Django Tutorial using DRF and DRF Spectacular OpenAPI |
5 | 2 |
|
6 |
| -# Docker Compose Commands |
| 3 | +Django DRF with SQLite, NGINX load balancing, and production-grade deployment setup. |
7 | 4 |
|
8 |
| -### Get running containers: |
9 |
| -**docker ps** |
| 5 | +Features: |
10 | 6 |
|
11 |
| -### Build and Run in detached mode: |
12 |
| -**docker compose up --build -d** |
| 7 | +- Basic CRUD API for Employees and Department using Django DRF |
| 8 | +- Load balanced using NGINX |
| 9 | +- Deployed using Gunicorn Server |
| 10 | +- Multi-stage Docker builds for optimized images |
| 11 | +- Zstandard and Gzip compression |
| 12 | +- Health checks and automatic failover |
13 | 13 |
|
14 |
| -### To enter Bash Shell for migrating db changes or static files |
15 |
| -**docker exec -it <container id from docker ps> bash** |
| 14 | +## Architecture |
16 | 15 |
|
17 |
| -### To view logs of container. You can pass head to view first N lines or tail to view last N lines specified by argument after head/tail |
| 16 | +- NGINX load balancer with 2 Django application replicas |
| 17 | +- Gunicorn as the WSGI server |
| 18 | +- Static files served by NGINX |
| 19 | +- Health monitoring and automatic failover |
| 20 | +- Session persistence using IP hash |
18 | 21 |
|
19 |
| -**docker logs --tail 1000 -f <container id from docker ps>** |
| 22 | +## Docker Compose Commands |
| 23 | + |
| 24 | +### Build and Run |
| 25 | + |
| 26 | +```powershell |
| 27 | +# Enable BuildKit for optimized builds |
| 28 | +$env:DOCKER_BUILDKIT=1 |
| 29 | +
|
| 30 | +# Build and start all services |
| 31 | +docker compose up --build -d |
| 32 | +
|
| 33 | +# Scale only web service |
| 34 | +docker compose up -d --scale web=2 |
| 35 | +``` |
| 36 | + |
| 37 | +### Container Management |
| 38 | + |
| 39 | +```powershell |
| 40 | +# List running containers |
| 41 | +docker compose ps |
| 42 | +
|
| 43 | +# View logs for specific service |
| 44 | +docker compose logs -f web # Django app logs |
| 45 | +docker compose logs -f nginx # NGINX logs |
| 46 | +
|
| 47 | +# Execute commands in container |
| 48 | +docker compose exec web python manage.py migrate |
| 49 | +docker compose exec web python manage.py collectstatic --noinput |
| 50 | +
|
| 51 | +# View logs with timestamps |
| 52 | +docker compose logs --timestamps --tail=100 |
| 53 | +``` |
| 54 | + |
| 55 | +### Health and Status |
| 56 | + |
| 57 | +```powershell |
| 58 | +# Check health endpoint |
| 59 | +curl http://localhost/health/ |
| 60 | +
|
| 61 | +# Check NGINX status |
| 62 | +docker compose exec nginx nginx -t |
| 63 | +
|
| 64 | +# View real-time container stats |
| 65 | +docker compose top |
| 66 | +``` |
| 67 | + |
| 68 | +### Maintenance |
| 69 | + |
| 70 | +```powershell |
| 71 | +# Stop all services |
| 72 | +docker compose down |
| 73 | +
|
| 74 | +# Remove volumes (careful - this deletes data!) |
| 75 | +docker compose down -v |
| 76 | +
|
| 77 | +# View container resource usage |
| 78 | +docker stats |
| 79 | +``` |
| 80 | + |
| 81 | +## Configuration |
| 82 | + |
| 83 | +### Environment Variables |
| 84 | + |
| 85 | +- `GUNICORN_WORKERS`: Number of worker processes (default: 4) |
| 86 | +- `GUNICORN_THREADS`: Threads per worker (default: 2) |
| 87 | +- `GUNICORN_MAX_REQUESTS`: Max requests per worker (default: 1000) |
| 88 | + |
| 89 | +### NGINX Features |
| 90 | + |
| 91 | +- Load balancing (least connections + IP hash) |
| 92 | +- Zstandard and Gzip compression |
| 93 | +- Static file serving |
| 94 | +- Health checks |
| 95 | +- Rate limiting |
| 96 | +- Security headers |
| 97 | + |
| 98 | +### Security |
| 99 | + |
| 100 | +- Non-root container execution |
| 101 | +- Rate limiting |
| 102 | +- HTTP security headers |
| 103 | +- No build tools in production image |
| 104 | + |
| 105 | +## API Documentation |
| 106 | + |
| 107 | +Access the API documentation at these endpoints: |
| 108 | + |
| 109 | +- Swagger UI: [http://localhost/apidocs_swagger/](http://localhost/apidocs_swagger/) |
| 110 | +- ReDoc: [http://localhost/apidocs_redoc/](http://localhost/apidocs_redoc/) |
| 111 | +- OpenAPI Schema: [http://localhost/apidocs/](http://localhost/apidocs/) |
| 112 | + |
| 113 | +## Development |
| 114 | + |
| 115 | +For local development without Docker: |
| 116 | + |
| 117 | +```powershell |
| 118 | +# Install pipenv if you haven't already |
| 119 | +pip install --user pipenv |
| 120 | +
|
| 121 | +# Install dependencies using Pipenv |
| 122 | +pipenv install |
| 123 | +
|
| 124 | +# Activate the virtual environment |
| 125 | +pipenv shell |
| 126 | +
|
| 127 | +# Run migrations |
| 128 | +python manage.py migrate |
| 129 | +
|
| 130 | +# Start the development server |
| 131 | +python manage.py runserver |
| 132 | +``` |
| 133 | + |
| 134 | +### Additional Development Commands |
| 135 | + |
| 136 | +```powershell |
| 137 | +# Install a new package |
| 138 | +pipenv install package_name |
| 139 | +
|
| 140 | +# Install development dependencies |
| 141 | +pipenv install --dev |
| 142 | +
|
| 143 | +# Update all dependencies |
| 144 | +pipenv update |
| 145 | +
|
| 146 | +# Generate requirements.txt (for Docker build) |
| 147 | +pipenv requirements > requirements.txt |
| 148 | +
|
| 149 | +# Check security vulnerabilities |
| 150 | +pipenv check |
| 151 | +``` |
| 152 | + |
| 153 | +The project uses Pipfile and Pipfile.lock for dependency management, ensuring consistent environments across development machines. |
0 commit comments