Spaces:
Build error
Docker Setup for Crowd Detection API
This guide will help you dockerize and run the Crowd Detection API using Docker and Docker Compose.
π File Structure
Make sure your project directory has the following structure:
crowd-detection-api/
βββ main.py # Your main FastAPI application
βββ start_backend.py # Startup script
βββ requirements.txt # Python dependencies
βββ Dockerfile # Docker image definition
βββ docker-compose.yml # Docker Compose configuration
βββ .dockerignore # Files to exclude from Docker build
βββ build-and-run.sh # Build and run script
βββ uploads/ # Directory for uploaded files (created automatically)
βββ models/ # Directory for AI models (created automatically)
βββ logs/ # Directory for logs (created automatically)
π Quick Start
Option 1: Using the Build Script (Recommended)
Make the script executable:
chmod +x build-and-run.shRun the interactive script:
./build-and-run.shOr run directly with commands:
./build-and-run.sh run # Build and run with docker-compose ./build-and-run.sh simple # Build and run simple container ./build-and-run.sh test # Test API endpoints ./build-and-run.sh logs # Show container logs ./build-and-run.sh stop # Stop services
Option 2: Manual Docker Commands
Build the Docker image:
docker build -t crowd-detection-api:latest .Run the container:
docker run -d \ --name crowd-detection-backend \ -p 8000:8000 \ -v $(pwd)/uploads:/app/uploads \ -v $(pwd)/models:/app/models \ -v $(pwd)/logs:/app/logs \ --restart unless-stopped \ crowd-detection-api:latest
Option 3: Using Docker Compose
Start the services:
docker-compose up -dStop the services:
docker-compose down
π Accessing the API
Once the container is running, you can access:
- API Base URL: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
- Interactive API: http://localhost:8000/redoc
π Testing the API
Health Check
curl http://localhost:8000/health
Get Zones with Heatmap
curl http://localhost:8000/zones/heatmap
WebSocket Connection (Alerts)
const ws = new WebSocket('ws://localhost:8000/ws/alerts');
ws.onmessage = (event) => {
console.log('Alert:', JSON.parse(event.data));
};
π οΈ Development Mode
For development with auto-reload:
docker run -it --rm \
-p 8000:8000 \
-v $(pwd):/app \
-w /app \
python:3.9-slim \
bash -c "pip install -r requirements.txt && python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload"
π Container Management
View running containers:
docker ps
View container logs:
docker logs crowd-detection-backend -f
Execute commands in container:
docker exec -it crowd-detection-backend bash
Stop and remove container:
docker stop crowd-detection-backend
docker rm crowd-detection-backend
π§ Configuration
Environment Variables
You can configure the container using environment variables:
docker run -d \
--name crowd-detection-backend \
-p 8000:8000 \
-e PYTHONUNBUFFERED=1 \
-e ENVIRONMENT=production \
crowd-detection-api:latest
Volume Mounts
The container uses the following volumes:
./uploads:/app/uploads- For uploaded video/image files./models:/app/models- For AI model cache./logs:/app/logs- For application logs
π¨ Troubleshooting
Container won't start:
Check if port 8000 is available:
netstat -tulpn | grep 8000Check container logs:
docker logs crowd-detection-backend
API not responding:
Check if container is healthy:
docker psTest from inside container:
docker exec -it crowd-detection-backend curl http://localhost:8000/health
Model download issues:
The container automatically downloads YOLOv8 models on first run. If this fails:
- Check internet connectivity in container
- Pre-download models manually:
docker exec -it crowd-detection-backend python -c "from ultralytics import YOLO; YOLO('yolov8s.pt')"
π Security Considerations
- The container runs as a non-root user (
appuser) - Only necessary system packages are installed
- Resource limits are configured in docker-compose.yml
- Health checks are enabled for monitoring
π Performance Tuning
Resource Limits
Adjust in docker-compose.yml:
deploy:
resources:
limits:
cpus: '4.0' # Increase for better performance
memory: 8G # Increase for large models
reservations:
cpus: '2.0'
memory: 4G
GPU Support
For GPU acceleration, add to docker-compose.yml:
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: 1
capabilities: [gpu]
π Updates and Maintenance
Update the application:
Stop the container:
./build-and-run.sh stopRebuild and restart:
./build-and-run.sh run
Clean up Docker resources:
# Remove unused images
docker image prune
# Remove unused volumes
docker volume prune
# Remove unused networks
docker network prune
π Support
If you encounter issues:
- Check the container logs
- Verify all required files are present
- Ensure Docker has sufficient resources allocated
- Check network connectivity for model downloads
The API will automatically start when the container starts and includes health checks for monitoring.