File size: 7,296 Bytes
61d29fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
---
sidebar_position: 10
---

# πŸ› Docker Build Troubleshooting Guide

## Testing Docker Build Locally

Before deploying to Hugging Face, always test the build locally:

```bash
# Run comprehensive build test
./test-huggingface-build.sh
```

This script will:
1. βœ… Build the Docker image
2. βœ… Check image size (HF has 50GB limit)
3. βœ… Start the container
4. βœ… Wait for services to be ready
5. βœ… Test all endpoints (/, /docs, /api/docs, /api/health)
6. βœ… Show container logs

## Common Build Failures

### 1. **Node.js Build Failures**

**Symptom:** `npm ci` or `npm install` fails during docs build

**Causes:**
- Network timeouts
- Package version conflicts
- Missing dependencies

**Solutions:**

```dockerfile
# Increase timeouts in Dockerfile.huggingface
RUN npm config set fetch-retry-mintimeout 20000 && \
    npm config set fetch-retry-maxtimeout 120000 && \
    npm ci --prefer-offline --no-audit || npm install --prefer-offline --no-audit
```

**Test locally:**
```bash
cd website
npm ci
npm run build
```

### 2. **Frontend Build Failures**

**Symptom:** `npm run build` fails in frontend directory

**Causes:**
- TypeScript errors
- Missing environment variables
- Vite configuration issues

**Solutions:**

Check build locally:
```bash
cd frontend
npm ci
npm run build
```

Fix TypeScript errors:
```bash
npm run typecheck
```

### 3. **Python Dependencies Fail**

**Symptom:** `pip install` fails

**Causes:**
- Missing system dependencies
- Incompatible versions
- Network issues

**Solutions:**

Test locally:
```bash
pip install -r requirements.txt
```

Check system dependencies in Dockerfile:
```dockerfile
RUN apt-get update && apt-get install -y \
    build-essential \
    tesseract-ocr \
    # Add missing dependencies here
    && rm -rf /var/lib/apt/lists/*
```

### 4. **Image Size Too Large**

**Symptom:** Image exceeds 50GB limit on Hugging Face

**Solutions:**

Check image size:
```bash
docker images open-navigator-hf-test --format "{{.Size}}"
```

Reduce size:
- Remove unnecessary files in `.dockerignore`
- Use multi-stage builds
- Clean up apt cache: `rm -rf /var/lib/apt/lists/*`
- Remove dev dependencies after build

### 5. **Services Not Starting**

**Symptom:** Container starts but services don't respond

**Check logs:**
```bash
docker logs open-navigator-test-container
```

**Common issues:**
- Nginx configuration errors
- Port conflicts (7860)
- Missing environment variables
- Supervisor not starting services

**Test nginx config:**
```bash
docker exec open-navigator-test-container nginx -t
```

**Check supervisor status:**
```bash
docker exec open-navigator-test-container supervisorctl status
```

### 6. **File Copy Errors**

**Symptom:** `COPY` commands fail in Dockerfile

**Causes:**
- Files not in build context
- Incorrect paths
- .dockerignore excluding needed files

**Solutions:**

Check .dockerignore:
```bash
cat .dockerignore
```

Verify files exist:
```bash
ls -la .huggingface/
ls -la website/build/
```

### 7. **Static Files Not Found**

**Symptom:** Frontend or docs return 404

**Check build output:**
```bash
# Check if docs built
ls -la static/docs/

# Check if frontend built
ls -la api/static/
ls -la static/frontend/
```

**Fix frontend build path:**
```dockerfile
# Ensure vite.config.ts outputs to correct location
RUN cd /app/frontend && npm run build

# Verify output
RUN ls -la /app/api/static/

# Copy to nginx location
RUN cp -r /app/api/static/* /app/static/frontend/
```

## Debugging Steps

### 1. Build Image Locally

```bash
docker build -f Dockerfile.huggingface -t test-build . --progress=plain
```

The `--progress=plain` flag shows detailed output.

### 2. Run Container Interactively

```bash
docker run -it --rm \
  -p 7860:7860 \
  --entrypoint /bin/bash \
  test-build
```

Then manually run commands:
```bash
# Check files
ls -la /app/
ls -la /app/static/

# Test nginx config
nginx -t

# Start services manually
supervisord -c /etc/supervisor/conf.d/supervisord.conf
```

### 3. Test Individual Services

```bash
# Test API directly
python -m uvicorn api.app:app --host 0.0.0.0 --port 8000

# Test nginx config
nginx -t -c /etc/nginx/nginx.conf

# Test supervisor
supervisorctl status
```

### 4. Check Network Connectivity

```bash
# Inside container
curl http://localhost:7860/
curl http://localhost:7860/docs
curl http://localhost:7860/api/docs
curl http://localhost:7860/api/health
```

## Hugging Face Specific Issues

### 1. **Build Timeout**

HF has build time limits. Optimize:
- Use layer caching effectively
- Minimize npm install time with `npm ci --prefer-offline`
- Pre-download large files

### 2. **Environment Variables**

Set in HF Space Settings β†’ Variables and secrets:
```bash
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
HUGGINGFACE_TOKEN=hf_...
LOG_LEVEL=INFO
HF_SPACES=1
```

### 3. **Hardware Requirements**

Docker Spaces require paid hardware:
- Settings β†’ Resource configuration
- Select "CPU Basic" minimum (~$22/month)

### 4. **Port Configuration**

HF Spaces expect port 7860:
```dockerfile
EXPOSE 7860
```

```bash
# In nginx config
listen 7860;
```

## Quick Fixes

### Reset Everything

```bash
# Clean Docker
docker system prune -a
docker volume prune

# Rebuild from scratch
docker build --no-cache -f Dockerfile.huggingface -t test-build .
```

### Check Build Context Size

```bash
# See what's being sent to Docker
docker build -f Dockerfile.huggingface -t test-build . 2>&1 | grep "Sending build context"
```

### Update Dependencies

```bash
# Update npm packages
cd website && npm update && cd ..
cd frontend && npm update && cd ..

# Update Python packages
pip list --outdated
pip install -U package-name
```

## Getting Help

1. **Check HF Build Logs:**
   - Go to your Space
   - Click "Logs" tab
   - Look for error messages

2. **Test Locally First:**
   ```bash
   ./test-huggingface-build.sh
   ```

3. **Compare with Working Build:**
   - Check git history: `git log --oneline`
   - Compare Dockerfiles: `git diff HEAD~1 Dockerfile.huggingface`

4. **Report Issues:**
   - Include build logs
   - Include local test results
   - Include Docker version: `docker --version`

## Deployment Workflow

βœ… **Recommended Process:**

```bash
# 1. Make changes
git add -A
git commit -m "Your changes"

# 2. Test build locally (THIS IS CRITICAL)
./test-huggingface-build.sh

# 3. If tests pass, deploy
./deploy-huggingface.sh

# 4. Monitor HF build logs
# Visit: https://huggingface.co/spaces/YOUR_USERNAME/open-navigator-for-engagement
```

⚠️ **Skip tests only if urgent:**

```bash
./deploy-huggingface.sh YOUR_USERNAME --skip-test
```

## Success Checklist

Before deploying to Hugging Face:

- [ ] Local build succeeds: `./test-huggingface-build.sh`
- [ ] All endpoints respond (/, /docs, /api/docs)
- [ ] No errors in container logs
- [ ] Image size < 50GB
- [ ] All environment variables configured
- [ ] HF Space hardware configured (CPU Basic minimum)
- [ ] Git changes committed

## Resources

- [Hugging Face Spaces Docker Guide](https://huggingface.co/docs/hub/spaces-sdks-docker)
- [Docker Best Practices](https://docs.docker.com/develop/dev-best-practices/)
- [Nginx Configuration](https://nginx.org/en/docs/)
- [Supervisor Configuration](http://supervisord.org/configuration.html)

---

**Last Updated:** 2026-04-26