415 lines
8.6 KiB
Markdown
415 lines
8.6 KiB
Markdown
# Docker Deployment Guide
|
|
|
|
This guide covers deploying the Drone Detection System using Docker and Docker Compose.
|
|
|
|
## Prerequisites
|
|
|
|
- Docker Engine 20.10+
|
|
- Docker Compose 2.0+
|
|
- At least 4GB RAM
|
|
- 10GB available disk space
|
|
|
|
## Quick Start
|
|
|
|
### 1. Environment Setup
|
|
|
|
```bash
|
|
# Copy environment template
|
|
cp .env.docker .env
|
|
|
|
# Edit .env with your Twilio credentials
|
|
nano .env
|
|
```
|
|
|
|
### 2. Basic Deployment
|
|
|
|
```bash
|
|
# Build and start all services
|
|
docker compose up -d
|
|
|
|
# View logs
|
|
docker compose logs -f
|
|
|
|
# Check service status
|
|
docker compose ps
|
|
```
|
|
|
|
### 3. Access the Application
|
|
|
|
- **Frontend**: http://localhost:3000
|
|
- **Backend API**: http://localhost:3001/api
|
|
- **Database**: localhost:5432
|
|
- **Redis**: localhost:6379
|
|
|
|
### Testing Docker Builds
|
|
|
|
Before running the full docker compose setup, you can test individual container builds:
|
|
|
|
```bash
|
|
# Test all builds
|
|
./test-docker-builds.sh # Linux/Mac
|
|
test-docker-builds.bat # Windows
|
|
|
|
# Test individual builds
|
|
docker build -t test-backend ./server
|
|
docker build -t test-frontend ./client
|
|
docker build -f docker/simulator/Dockerfile -t test-simulator .
|
|
```
|
|
|
|
## Service Architecture
|
|
|
|
```
|
|
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
|
│ Frontend │ │ Backend │ │ PostgreSQL │
|
|
│ (React) │◄──►│ (Node.js) │◄──►│ Database │
|
|
│ Port: 3000 │ │ Port: 3001 │ │ Port: 5432 │
|
|
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
|
│ │ │
|
|
│ ┌─────────────────┐ │
|
|
└──────────────►│ Redis │◄────────────┘
|
|
│ (Caching) │
|
|
│ Port: 6379 │
|
|
└─────────────────┘
|
|
```
|
|
|
|
## Docker Compose Profiles
|
|
|
|
### Development Profile (Default)
|
|
```bash
|
|
docker compose up -d
|
|
```
|
|
Includes: Frontend, Backend, Database, Redis
|
|
|
|
### Production Profile
|
|
```bash
|
|
docker compose --profile production up -d
|
|
```
|
|
Includes: All services + Nginx reverse proxy
|
|
|
|
### Simulation Profile
|
|
```bash
|
|
docker compose --profile simulation up -d
|
|
```
|
|
Includes: All services + Python drone simulator
|
|
|
|
## Service Details
|
|
|
|
### Frontend Container
|
|
- **Image**: Custom Nginx + React build
|
|
- **Port**: 3000:80
|
|
- **Features**:
|
|
- Gzip compression
|
|
- SPA routing support
|
|
- API proxying
|
|
- Security headers
|
|
|
|
### Backend Container
|
|
- **Image**: Node.js 18 Alpine
|
|
- **Port**: 3001:3001
|
|
- **Features**:
|
|
- Health checks
|
|
- Non-root user
|
|
- Log persistence
|
|
- Signal handling
|
|
|
|
### Database Container
|
|
- **Image**: PostgreSQL 15 Alpine
|
|
- **Port**: 5432:5432
|
|
- **Features**:
|
|
- Persistent storage
|
|
- Health checks
|
|
- Initialization scripts
|
|
- Performance tuning
|
|
|
|
### Redis Container
|
|
- **Image**: Redis 7 Alpine
|
|
- **Port**: 6379:6379
|
|
- **Features**:
|
|
- Persistent storage
|
|
- AOF logging
|
|
- Health checks
|
|
|
|
### Nginx Proxy (Production)
|
|
- **Image**: Nginx Alpine
|
|
- **Ports**: 80:80, 443:443
|
|
- **Features**:
|
|
- SSL termination
|
|
- Load balancing
|
|
- Static file serving
|
|
- WebSocket support
|
|
|
|
## Environment Variables
|
|
|
|
### Backend Environment
|
|
```bash
|
|
NODE_ENV=production
|
|
PORT=3001
|
|
DB_HOST=postgres
|
|
DB_PORT=5432
|
|
DB_NAME=drone_detection
|
|
DB_USER=postgres
|
|
DB_PASSWORD=postgres123
|
|
REDIS_HOST=redis
|
|
REDIS_PORT=6379
|
|
JWT_SECRET=your-jwt-secret
|
|
TWILIO_ACCOUNT_SID=your-twilio-sid
|
|
TWILIO_AUTH_TOKEN=your-twilio-token
|
|
TWILIO_PHONE_NUMBER=your-twilio-phone
|
|
CORS_ORIGIN=http://localhost:3000
|
|
```
|
|
|
|
### Frontend Build Arguments
|
|
```bash
|
|
VITE_API_URL=http://localhost:3001/api
|
|
VITE_WS_URL=ws://localhost:3001
|
|
```
|
|
|
|
## Data Persistence
|
|
|
|
### Volumes
|
|
- `postgres_data`: Database files
|
|
- `redis_data`: Redis persistence
|
|
- `./server/logs`: Application logs
|
|
|
|
### Backup Strategy
|
|
```bash
|
|
# Database backup
|
|
docker compose exec postgres pg_dump -U postgres drone_detection > backup.sql
|
|
|
|
# Restore database
|
|
docker compose exec -T postgres psql -U postgres drone_detection < backup.sql
|
|
|
|
# Volume backup
|
|
docker run --rm -v uamils_postgres_data:/data -v $(pwd):/backup alpine tar czf /backup/postgres_backup.tar.gz /data
|
|
```
|
|
|
|
## Monitoring and Logs
|
|
|
|
### View Logs
|
|
```bash
|
|
# All services
|
|
docker compose logs -f
|
|
|
|
# Specific service
|
|
docker compose logs -f backend
|
|
|
|
# Last 100 lines
|
|
docker compose logs --tail=100 backend
|
|
```
|
|
|
|
### Health Checks
|
|
```bash
|
|
# Check service health
|
|
docker compose ps
|
|
|
|
# Manual health check
|
|
curl http://localhost:3001/api/health
|
|
curl http://localhost:3000/health
|
|
```
|
|
|
|
### Resource Monitoring
|
|
```bash
|
|
# Container stats
|
|
docker stats
|
|
|
|
# Detailed container info
|
|
docker compose exec backend top
|
|
```
|
|
|
|
## Troubleshooting
|
|
|
|
### Common Issues
|
|
|
|
#### 1. Database Connection Issues
|
|
```bash
|
|
# Check database status
|
|
docker compose exec postgres pg_isready -U postgres
|
|
|
|
# View database logs
|
|
docker compose logs postgres
|
|
|
|
# Reset database
|
|
docker compose down -v
|
|
docker compose up -d
|
|
```
|
|
|
|
#### 2. Frontend Build Issues
|
|
```bash
|
|
# If npm ci fails (missing package-lock.json)
|
|
# The Dockerfile has been updated to use npm install instead
|
|
|
|
# Rebuild frontend
|
|
docker compose build --no-cache frontend
|
|
|
|
# Test frontend build individually
|
|
docker build -t test-frontend ./client
|
|
|
|
# Check build logs
|
|
docker compose logs frontend
|
|
```
|
|
|
|
#### 3. Backend API Issues
|
|
```bash
|
|
# Check backend health
|
|
curl http://localhost:3001/api/health/detailed
|
|
|
|
# View backend logs
|
|
docker compose logs backend
|
|
|
|
# Restart backend
|
|
docker compose restart backend
|
|
```
|
|
|
|
#### 4. Port Conflicts
|
|
```bash
|
|
# Check port usage
|
|
netstat -tulpn | grep :3000
|
|
netstat -tulpn | grep :3001
|
|
|
|
# Stop conflicting services
|
|
docker compose down
|
|
```
|
|
|
|
### Performance Tuning
|
|
|
|
#### 1. Database Optimization
|
|
```bash
|
|
# Increase shared_buffers for PostgreSQL
|
|
docker compose exec postgres psql -U postgres -c "ALTER SYSTEM SET shared_buffers = '256MB';"
|
|
docker compose restart postgres
|
|
```
|
|
|
|
#### 2. Memory Limits
|
|
```yaml
|
|
# Add to docker compose.yml services
|
|
services:
|
|
backend:
|
|
mem_limit: 512m
|
|
mem_reservation: 256m
|
|
frontend:
|
|
mem_limit: 256m
|
|
mem_reservation: 128m
|
|
```
|
|
|
|
## Production Deployment
|
|
|
|
### 1. SSL Configuration
|
|
```bash
|
|
# Generate SSL certificates
|
|
mkdir -p docker/ssl
|
|
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
|
|
-keyout docker/ssl/nginx.key \
|
|
-out docker/ssl/nginx.crt
|
|
```
|
|
|
|
### 2. Environment Security
|
|
```bash
|
|
# Use Docker secrets for sensitive data
|
|
echo "your-jwt-secret" | docker secret create jwt_secret -
|
|
echo "your-twilio-token" | docker secret create twilio_token -
|
|
```
|
|
|
|
### 3. Nginx Configuration
|
|
```bash
|
|
# Enable production profile
|
|
docker compose --profile production up -d
|
|
|
|
# Update nginx config for your domain
|
|
# Edit docker/nginx/default.conf
|
|
```
|
|
|
|
### 4. Monitoring Setup
|
|
```bash
|
|
# Add monitoring services
|
|
docker compose -f docker compose.yml -f docker compose.monitoring.yml up -d
|
|
```
|
|
|
|
## Scaling
|
|
|
|
### Horizontal Scaling
|
|
```yaml
|
|
# Scale backend instances
|
|
docker compose up -d --scale backend=3
|
|
|
|
# Load balancer configuration required
|
|
```
|
|
|
|
### Database Scaling
|
|
```yaml
|
|
# Add read replicas
|
|
postgres-replica:
|
|
image: postgres:15-alpine
|
|
environment:
|
|
POSTGRES_MASTER_SERVICE: postgres
|
|
POSTGRES_REPLICA_USER: replica
|
|
POSTGRES_REPLICA_PASSWORD: replica123
|
|
```
|
|
|
|
## Maintenance
|
|
|
|
### Updates
|
|
```bash
|
|
# Update images
|
|
docker compose pull
|
|
|
|
# Rebuild and restart
|
|
docker compose down
|
|
docker compose up -d --build
|
|
```
|
|
|
|
### Cleanup
|
|
```bash
|
|
# Remove unused containers
|
|
docker system prune
|
|
|
|
# Remove unused volumes
|
|
docker volume prune
|
|
|
|
# Clean build cache
|
|
docker builder prune
|
|
```
|
|
|
|
## Testing with Simulator
|
|
|
|
### Run Simulation
|
|
```bash
|
|
# Start simulation profile
|
|
docker compose --profile simulation up -d
|
|
|
|
# Run custom simulation
|
|
docker compose run --rm simulator python drone_simulator.py \
|
|
--devices 10 \
|
|
--duration 3600 \
|
|
--detection-interval 30
|
|
```
|
|
|
|
### Monitor Simulation
|
|
```bash
|
|
# View simulator logs
|
|
docker compose logs -f simulator
|
|
|
|
# Check API stats
|
|
curl http://localhost:3001/api/dashboard/stats
|
|
```
|
|
|
|
## Security Considerations
|
|
|
|
### Container Security
|
|
- Non-root users in all containers
|
|
- Read-only root filesystems where possible
|
|
- Limited container capabilities
|
|
- Security scanning with `docker scan`
|
|
|
|
### Network Security
|
|
- Custom bridge network isolation
|
|
- No unnecessary port exposures
|
|
- Internal service communication
|
|
|
|
### Data Security
|
|
- Encrypted environment variables
|
|
- SSL/TLS termination at proxy
|
|
- Database connection encryption
|
|
- Regular security updates
|
|
|
|
For additional security hardening, see [Security Best Practices](../docs/SECURITY.md).
|