File size: 6,626 Bytes
1b16c79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# BackgroundFX Pro Docker Deployment

This directory contains Docker configurations for deploying BackgroundFX Pro in various environments.

## Quick Start

### Prerequisites

- Docker 20.10+ installed
- Docker Compose 2.0+ installed
- NVIDIA Docker runtime (for GPU support)
- At least 16GB RAM
- 20GB+ free disk space

### GPU Deployment (Recommended)

```bash
# Build and run with GPU support
make build
make run

# Or using docker-compose directly
docker-compose up -d backgroundfx-gpu
```

Access the application at http://localhost:7860

### CPU-Only Deployment

```bash
# Build and run CPU version
make build-cpu
make run-cpu

# Or using docker-compose
docker-compose --profile cpu up -d backgroundfx-cpu
```

Access at http://localhost:7861

## Available Docker Images

### 1. GPU Image (Dockerfile)
- **Base**: nvidia/cuda:12.1.0-cudnn8-runtime-ubuntu20.04
- **Features**: Full GPU acceleration, all models supported
- **Use case**: Production deployment with NVIDIA GPU

### 2. Production Image (Dockerfile.prod)
- **Type**: Multi-stage optimized build
- **Features**: Minimal size, pre-compiled Python, security hardening
- **Use case**: Production deployment with high performance requirements

### 3. CPU Image (Dockerfile.cpu)
- **Base**: python:3.10-slim
- **Features**: CPU-optimized, smaller footprint
- **Use case**: Development, testing, or CPU-only servers

## Docker Compose Services

### Core Services

- **backgroundfx-gpu**: Main application with GPU support
- **backgroundfx-cpu**: CPU-only variant
- **redis**: Cache and job queue
- **nginx**: Reverse proxy (production profile)

### Support Services

- **model-downloader**: Pre-download models (setup profile)
- **prometheus**: Metrics collection (monitoring profile)
- **grafana**: Metrics visualization (monitoring profile)

## Configuration

### Environment Variables

Copy the example environment file and customize:

```bash
cp docker/.env.example docker/.env
```

Key settings:
- `DEVICE`: auto, cuda, or cpu
- `MODEL_CACHE_DIR`: Model storage location
- `MAX_MEMORY_GB`: Memory limit
- `QUALITY_PRESET`: low, medium, high, ultra

### Volumes

- `model-cache`: Cached ML models (~2-5GB)
- `uploads`: Uploaded files
- `outputs`: Processed results
- `redis-data`: Redis persistence

## Makefile Commands

```bash
# Building
make build          # Build GPU image
make build-cpu      # Build CPU image
make build-all      # Build all variants
make build-nocache  # Build without cache

# Running
make run            # Run GPU version
make run-cpu        # Run CPU version
make run-dev        # Development mode
make run-prod       # Production with monitoring

# Management
make stop           # Stop containers
make restart        # Restart containers
make clean          # Clean up
make logs           # View logs
make shell          # Container shell

# Models
make download-models  # Download all models
make list-models      # List available models

# Monitoring
make status         # Container status
make stats          # Resource usage
make health         # Health checks
```

## Production Deployment

### 1. Build Production Image

```bash
make build-prod
```

### 2. Configure Environment

Edit `docker/.env` with production settings:
- Set secure `AUTH_SECRET_KEY`
- Configure `REDIS_PASSWORD`
- Adjust resource limits
- Enable authentication if needed

### 3. Deploy with Monitoring

```bash
make run-prod
```

This starts:
- Main application (port 7860)
- API server (port 8000)
- Nginx proxy (ports 80/443)
- Redis cache
- Prometheus + Grafana monitoring

### 4. SSL/TLS Setup

Place certificates in `nginx/ssl/`:
- `nginx/ssl/cert.pem`
- `nginx/ssl/key.pem`

### 5. Scaling

For horizontal scaling:

```bash
docker-compose up -d --scale backgroundfx-gpu=3
```

## GPU Support

### Check GPU Availability

```bash
make gpu-check
# Or
docker run --rm --gpus all nvidia/cuda:12.1.0-base-ubuntu20.04 nvidia-smi
```

### Install NVIDIA Docker Runtime

Ubuntu/Debian:
```bash
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update && sudo apt-get install -y nvidia-docker2
sudo systemctl restart docker
```

## Troubleshooting

### Out of Memory

Adjust memory limits in `docker-compose.yml`:
```yaml
deploy:
  resources:
    limits:
      memory: 8G  # Reduce if needed
```

### Models Not Loading

Pre-download models:
```bash
make download-models
```

### Permission Issues

Fix ownership:
```bash
docker-compose exec -u root backgroundfx-gpu chown -R appuser:appuser /app
```

### Slow Processing

- Ensure GPU is detected: `make gpu-check`
- Check resource usage: `make stats`
- Adjust quality preset in `.env`

## Development

### Local Development with Docker

```bash
# Mount local code for live reload
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
```

### Running Tests

```bash
make test
```

### Building Custom Images

```bash
# With custom registry
make push REGISTRY=myregistry.com VERSION=1.0.0
```

## Monitoring

### Grafana Dashboard

Access at http://localhost:3000 (admin/admin)

Pre-configured dashboards:
- Container metrics
- GPU utilization
- Processing statistics
- Error rates

### Prometheus Metrics

Access at http://localhost:9090

Available metrics:
- `processing_time_seconds`
- `frames_processed_total`
- `model_load_time_seconds`
- `memory_usage_bytes`

## Backup and Restore

### Backup Volumes

```bash
make backup
```

Creates timestamped backups in `./backups/`

### Restore from Backup

```bash
make restore BACKUP_FILE=models-20240101-120000.tar.gz
```

## Security Considerations

1. **Change default passwords** in production
2. **Enable authentication** via AUTH_ENABLED=true
3. **Configure CORS** appropriately
4. **Use SSL/TLS** in production
5. **Limit exposed ports** using firewall rules
6. **Regular security updates**: `docker pull` base images

## Performance Optimization

### GPU Optimization
- Use TensorRT models when available
- Enable mixed precision with FP16
- Adjust batch size based on GPU memory

### CPU Optimization
- Use quantized models
- Enable OpenMP threading
- Adjust worker count based on cores

### Memory Optimization
- Enable swap for large videos
- Use frame skipping for preview
- Implement progressive processing

## Support

For issues or questions:
1. Check logs: `make logs`
2. Verify health: `make health`
3. Review configuration: `docker-compose config`
4. Check system requirements: `make gpu-check`