Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions optifit backend/.dockerignore
Original file line number Diff line number Diff line change
@@ -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


297 changes: 297 additions & 0 deletions optifit backend/DOCKER_DEPLOYMENT.md
Original file line number Diff line number Diff line change
@@ -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/<job_id>** - Get processing results
- **GET /processed/<filename>** - 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.
Comment on lines +231 to +244
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Scaling section needs to address configuration prerequisites.

The scaling guidance doesn't acknowledge critical constraints that prevent the provided command from working:

  1. The hardcoded container_name: optifit-backend in docker-compose.yml prevents scaling (Docker requires unique container names for multiple instances).
  2. Fixed port mapping (5000:5000) conflicts with scaling—each instance needs a unique host port.

Before scaling can work, the docker-compose.yml must be updated to remove or make container_name configurable and change ports to a range (e.g., "5000-5010:5000").

Consider updating the scaling section to include these prerequisites:

 ## Scaling
 
 ### Horizontal Scaling
 
+**Prerequisites for scaling:**
+1. Remove or make the `container_name` configurable in docker-compose.yml (currently hardcoded as "optifit-backend")
+2. Change port mapping to a range (e.g., `"5000-5010:5000"`) instead of a fixed port
+3. Configure a load balancer (nginx, traefik) to distribute requests
+
 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.
+You must then configure a load balancer (nginx, traefik) to distribute requests across the dynamically assigned ports.


This prevents users from attempting to scale without understanding the prerequisite configuration changes needed. Is the docker-compose.yml configured to allow scaling, or should this section be reframed as a "future enhancement"?

---

I need to verify the actual state of the docker-compose.yml file to confirm the concerns raised. Let me search the repository.

---

🏁 Script executed:

```shell
#!/bin/bash
# Find docker-compose files in the repository
fd -e yml -e yaml | grep -i docker-compose

Length of output: 34


🏁 Script executed:

#!/bin/bash
# Also search for docker-compose files with different naming conventions
fd docker-compose

Length of output: 34


🏁 Script executed:

#!/bin/bash
# Read the docker-compose.yml file to verify configuration
cat "optifit backend/docker-compose.yml"

Length of output: 765


Scaling documentation conflicts with current docker-compose.yml configuration—prerequisites must be addressed.

The review comment is verified as accurate. The docker-compose.yml file contains:

  • Hardcoded container_name: optifit-backend (prevents multiple instances—Docker requires unique names)
  • Fixed port mapping "5000:5000" (prevents multiple instances from binding to the same host port)

The scaling command provided in the documentation (docker-compose up -d --scale optifit-backend=3) would fail with the current configuration. The documentation must be updated to either:

  1. Add a prerequisites section explaining the required docker-compose.yml changes (remove/make container_name configurable, change ports to a range)
  2. Reframe scaling as a future enhancement that requires these configuration changes first

The suggested diff in the review comment appropriately addresses this gap by explicitly calling out the prerequisites before users attempt the scaling command.

🤖 Prompt for AI Agents
In optifit backend/DOCKER_DEPLOYMENT.md around lines 231 to 244, the scaling
instructions conflict with the current docker-compose.yml (which hardcodes
container_name and fixed host port 5000:5000), so update the doc to either (A)
add a "Prerequisites" subsection before the Horizontal Scaling example listing
exact required docker-compose.yml changes: remove or parameterize
container_name, remove fixed host port binding or use ephemeral/host port ranges
or publish mode (or use docker swarm/kubernetes), and note how to run compose
without conflicting ports; or (B) reframe the scaling section as a future
enhancement and explicitly state that scaling will fail until those compose
changes are made—choose one approach and make the corresponding text changes so
users are warned and instructed before attempting docker-compose --scale.


## 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`
Loading
Loading