diff --git a/optifit backend/.dockerignore b/optifit backend/.dockerignore new file mode 100644 index 0000000..11a0d07 --- /dev/null +++ b/optifit backend/.dockerignore @@ -0,0 +1,83 @@ +# Python +__pycache__/ +*.py[cod] +*$py.class +*.so +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# Virtual environments +venv/ +env/ +ENV/ +env.bak/ +venv.bak/ + +# IDE +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# OS +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +# Git +.git/ +.gitignore + +# Docker +docker-compose.yml + +# Documentation +README*.md +*.md +LICENSE +CONTRIBUTING.md +CODE_OF_CONDUCT.md + +# Test files +test_*.py +tests/ +*_test.py + +# Logs +*.log +logs/ + +# Temporary files +tmp/ +temp/ +.tmp/ + +# Uploaded files (these will be created at runtime) +uploads/* +!uploads/.gitkeep + +# Processed files (these will be created at runtime) +processed/* +!processed/.gitkeep + + diff --git a/optifit backend/DOCKER_DEPLOYMENT.md b/optifit backend/DOCKER_DEPLOYMENT.md new file mode 100644 index 0000000..bb87033 --- /dev/null +++ b/optifit backend/DOCKER_DEPLOYMENT.md @@ -0,0 +1,297 @@ +# Docker Deployment Guide + +This guide explains how to deploy the OptiFit Flask backend using Docker and Docker Compose. + +## Prerequisites + +- Docker installed on your system +- Docker Compose (included with Docker Desktop) +- At least 2GB of available RAM for video processing + +## Quick Start + +### Using Docker Compose (Recommended) + +1. **Navigate to the backend directory:** + ```bash + cd "optifit backend" + ``` + +2. **Start the service:** + ```bash + docker-compose up -d + ``` + +3. **Check service status:** + ```bash + docker-compose ps + ``` + +4. **Test the API:** + ```bash + curl http://localhost:5000/ping + ``` + +### Using Docker Commands + +1. **Build the image:** + ```bash + docker build -t optifit-backend . + ``` + +2. **Run the container:** + ```bash + docker run -d --name optifit-backend -p 5000:5000 optifit-backend + ``` + +3. **Check container status:** + ```bash + docker ps + ``` + +## API Endpoints + +Once the container is running, the following endpoints are available: + +- **GET /** - Server information and available routes +- **GET /ping** - Health check endpoint +- **POST /upload** - Upload video for squat detection +- **GET /result/** - Get processing results +- **GET /processed/** - Download processed videos + +## Configuration + +### Environment Variables + +The following environment variables can be configured: + +- `FLASK_ENV` - Flask environment (default: production) +- `PYTHONUNBUFFERED` - Python output buffering (default: 1) + +### Resource Limits + +The Docker Compose configuration includes resource limits: +- Memory limit: 2GB +- Memory reservation: 512MB + +### Volume Mounts + +The following directories are mounted for persistence: +- `./uploads` - Uploaded video files +- `./processed` - Processed video files + +## Health Checks + +The container includes a health check that: +- Runs every 30 seconds +- Times out after 10 seconds +- Retries 3 times +- Has a 40-second start period + +## Troubleshooting + +### Container Won't Start + +1. **Check logs:** + ```bash + docker-compose logs optifit-backend + ``` + +2. **Check resource usage:** + ```bash + docker stats + ``` + +3. **Verify port availability:** + ```bash + netstat -tulpn | grep :5000 + ``` + +### API Not Responding + +1. **Check container health:** + ```bash + docker ps + ``` + +2. **Test health check manually:** + ```bash + docker exec optifit-backend curl -f http://localhost:5000/ping + ``` + +3. **Check container logs:** + ```bash + docker logs optifit-backend + ``` + +### Video Processing Issues + +1. **Check ffmpeg installation:** + ```bash + docker exec optifit-backend ffmpeg -version + ``` + +2. **Verify OpenCV and MediaPipe:** + ```bash + docker exec optifit-backend python -c "import cv2, mediapipe; print('Dependencies OK')" + ``` + +## Production Deployment + +### Using Docker Compose + +For production deployment, consider: + +1. **Update docker-compose.yml:** + ```yaml + services: + optifit-backend: + restart: always + environment: + - FLASK_ENV=production + deploy: + resources: + limits: + memory: 4G + ``` + +2. **Use external volumes:** + ```yaml + volumes: + - /opt/optifit/uploads:/app/uploads + - /opt/optifit/processed:/app/processed + ``` + +3. **Add reverse proxy (nginx):** + ```yaml + nginx: + image: nginx:alpine + ports: + - "80:80" + volumes: + - ./nginx.conf:/etc/nginx/nginx.conf + ``` + +### Security Considerations + +1. **Run as non-root user:** + ```dockerfile + RUN adduser --disabled-password --gecos '' appuser + USER appuser + ``` + +2. **Use secrets for sensitive data:** + ```yaml + secrets: + - db_password + ``` + +3. **Limit container capabilities:** + ```yaml + cap_drop: + - ALL + cap_add: + - NET_BIND_SERVICE + ``` + +## Monitoring + +### Health Monitoring + +The container includes built-in health checks. Monitor using: + +```bash +# Check container health +docker inspect --format='{{.State.Health.Status}}' optifit-backend + +# View health check logs +docker inspect --format='{{range .State.Health.Log}}{{.Output}}{{end}}' optifit-backend +``` + +### Log Monitoring + +```bash +# Follow logs in real-time +docker-compose logs -f optifit-backend + +# View recent logs +docker-compose logs --tail=100 optifit-backend +``` + +### Resource Monitoring + +```bash +# Monitor resource usage +docker stats optifit-backend + +# Check disk usage +docker exec optifit-backend df -h +``` + +## Scaling + +### Horizontal Scaling + +To scale the service: + +```bash +# Scale to 3 instances +docker-compose up -d --scale optifit-backend=3 +``` + +### Load Balancing + +Use a load balancer (nginx, traefik) to distribute requests across multiple instances. + +## Backup and Recovery + +### Backup Data + +```bash +# Backup uploaded files +tar -czf uploads-backup.tar.gz uploads/ + +# Backup processed files +tar -czf processed-backup.tar.gz processed/ +``` + +### Restore Data + +```bash +# Restore uploaded files +tar -xzf uploads-backup.tar.gz + +# Restore processed files +tar -xzf processed-backup.tar.gz +``` + +## Cleanup + +### Stop Services + +```bash +# Stop and remove containers +docker-compose down + +# Stop and remove containers with volumes +docker-compose down -v +``` + +### Remove Images + +```bash +# Remove unused images +docker image prune + +# Remove specific image +docker rmi optifit-backend +``` + +## Support + +For issues related to Docker deployment: + +1. Check the logs: `docker-compose logs` +2. Verify system resources: `docker stats` +3. Test API endpoints: `curl http://localhost:5000/ping` +4. Check container health: `docker ps` diff --git a/optifit backend/Dockerfile b/optifit backend/Dockerfile new file mode 100644 index 0000000..e63c15e --- /dev/null +++ b/optifit backend/Dockerfile @@ -0,0 +1,57 @@ +# ---- Builder Stage ---- +FROM python:3.11-slim as builder + +WORKDIR /app + +# Install build-time dependencies for Python packages +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + libxrender-dev \ + libavcodec-dev \ + libavformat-dev \ + libswscale-dev \ + && rm -rf /var/lib/apt/lists/* + +# Install Python dependencies +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# ---- Final Stage ---- +FROM python:3.11-slim + +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 +ENV FLASK_APP=app.py +ENV FLASK_ENV=production + +WORKDIR /app + +# Install runtime dependencies +RUN apt-get update && apt-get install -y --no-install-recommends \ + ffmpeg \ + curl \ + libglib2.0-0 \ + libsm6 \ + libxext6 \ + libxrender1 \ + libgomp1 \ + libgtk-3-0 \ + && rm -rf /var/lib/apt/lists/* + +# Create a non-root user +RUN addgroup --system app && adduser --system --ingroup app app +USER app + +# Copy installed packages and application code +COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages +COPY --chown=app:app . . + +RUN mkdir -p uploads processed + +EXPOSE 5000 + +HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ + CMD curl -f http://localhost:5000/ping || exit 1 + +CMD ["python", "app.py"] + diff --git a/optifit backend/README_BACKEND.md b/optifit backend/README_BACKEND.md index 00ba7fc..493ad3c 100644 --- a/optifit backend/README_BACKEND.md +++ b/optifit backend/README_BACKEND.md @@ -19,6 +19,22 @@ The backend is built using the Flask framework and follows a RESTful API design. ## Getting Started -For detailed instructions on how to set up and run the backend, please refer to the backend setup guide: +### Docker Deployment (Recommended) +For quick deployment using Docker: + +```bash +# Using Docker Compose +docker-compose up -d + +# Test the API +curl http://localhost:5000/ping +``` + +For detailed Docker deployment instructions, see: +- [**Docker Deployment Guide**](./deployment/DOCKER_DEPLOYMENT.md) + +### Manual Setup + +For manual setup and development, please refer to: - [**Backend Setup Guide**](./SETUP_BACKEND.md) \ No newline at end of file diff --git a/optifit backend/docker-compose.yml b/optifit backend/docker-compose.yml new file mode 100644 index 0000000..8d0b1c2 --- /dev/null +++ b/optifit backend/docker-compose.yml @@ -0,0 +1,35 @@ +services: + optifit-backend: + build: + context: . + dockerfile: Dockerfile + container_name: optifit-backend + ports: + - "5000:5000" + volumes: + - ./uploads:/app/uploads + - ./processed:/app/processed + environment: + - FLASK_ENV=production + - PYTHONUNBUFFERED=1 + restart: unless-stopped + healthcheck: + test: [ "CMD", "curl", "-f", "http://localhost:5000/ping" ] + interval: 30s + timeout: 10s + retries: 3 + start_period: 40s + # Optional: Redis for caching (if needed in future) + # redis: + # image: redis:7-alpine + # container_name: optifit-redis + # ports: + # - "6379:6379" + # volumes: + # - redis_data:/data + # restart: unless-stopped + + # volumes: + # redis_data: + +