Docker SDK Setup Guide
Overview
The Research AI Assistant now uses Docker SDK instead of Gradio SDK for Hugging Face Spaces deployment. This provides more control and flexibility.
Changes Made
1. README.md Configuration
Changed from:
sdk: gradio
app_file: app.py
To:
sdk: docker
app_port: 7860
2. Dockerfile Configuration
Key Features:
- Base Image: Python 3.10-slim
- Entry Point:
main.py(starts both Gradio and Flask API) - Ports:
- 7860: Gradio UI (primary, exposed by HF Spaces)
- 5001: Flask API (background thread)
- Health Check: Monitors Gradio endpoint
How It Works
Startup Sequence
Docker Container Starts
- Runs
python main.py
- Runs
main.py Initializes
- Starts Flask API in background thread (port 5001)
- Creates Gradio interface from
app.py - Launches Gradio on port 7860
Both Services Available
- Gradio UI:
https://your-space.hf.space(port 7860) - Flask API: Available internally (port 5001)
- Gradio UI:
File Structure
Research_AI_Assistant/
βββ Dockerfile # Main Dockerfile (used by HF Spaces)
βββ Dockerfile.hf # Alternative/backup Dockerfile
βββ README.md # HF Spaces config (sdk: docker)
βββ main.py # Entry point (starts Flask + Gradio)
βββ app.py # Gradio interface definition
βββ flask_api.py # Flask API (started by main.py)
Benefits of Docker SDK
1. More Control
- Full control over container environment
- Can install system dependencies
- Custom startup scripts
2. Multiple Services
- Can run multiple processes (Gradio + Flask)
- Better resource management
- Health checks
3. Flexibility
- Not limited to Gradio's constraints
- Can add additional services
- Better debugging
4. Production Ready
- Standard Docker patterns
- Easier to migrate to other platforms
- Better for CI/CD
Deployment
On Hugging Face Spaces
Push to Repository
git push origin mainHF Spaces Auto-Builds
- Detects
sdk: dockerin README.md - Builds Docker image from Dockerfile
- Exposes port 7860
- Detects
Services Start
- Flask API starts in background
- Gradio UI launches on port 7860
- Both share orchestrator instance
Local Testing
Test the Docker build locally:
# Build image
docker build -t research-assistant .
# Run container
docker run -p 7860:7860 -p 5001:5001 \
-e HF_TOKEN=your_token \
research-assistant
Port Configuration
Exposed Ports
7860: Gradio UI (primary, exposed by HF Spaces)
- Accessible at:
https://your-space.hf.space
- Accessible at:
5001: Flask API (internal, background thread)
- Not directly exposed by HF Spaces
- Can be accessed via port forwarding if needed
Internal Communication
- Flask API runs in same container
- Both services share Python process
- Shared orchestrator instance
- No network overhead
Environment Variables
Set in HF Spaces Secrets:
HF_TOKEN=your_huggingface_token
ENABLE_FLASK_API=true # Default: true
FLASK_PORT=5001 # Default: 5001
FLASK_HOST=0.0.0.0 # Default: 0.0.0.0
Health Checks
The Dockerfile includes a health check:
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \
CMD curl -f http://localhost:7860/ || exit 1
- Checks Gradio endpoint every 30 seconds
- 60-second startup period (allows initialization)
- 3 retries before marking unhealthy
Troubleshooting
Issue: Build Fails
Check:
- Dockerfile syntax is correct
- Requirements.txt has all dependencies
- Python 3.10 is available
Solution:
# Test build locally
docker build -t test-build .
Issue: Port Not Accessible
Check:
app_port: 7860in README.md- Gradio is listening on 0.0.0.0:7860
- No firewall blocking
Solution:
- Verify in logs: "Running on local URL: http://0.0.0.0:7860"
Issue: Flask API Not Starting
Check:
- Logs show Flask initialization
ENABLE_FLASK_API=true(default)- Port 5001 not conflicting
Solution:
- Check logs for "Starting Flask API in background thread..."
- Verify Flask thread starts successfully
Comparison: Gradio SDK vs Docker SDK
| Feature | Gradio SDK | Docker SDK |
|---|---|---|
| Setup | Simple | More control |
| Multiple Services | β Limited | β Full control |
| System Dependencies | β οΈ Limited | β Full access |
| Customization | β οΈ Limited | β Complete |
| Debugging | β οΈ Harder | β Easier |
| Port Configuration | β οΈ Fixed | β Flexible |
| Health Checks | β No | β Yes |
Migration Notes
From Gradio SDK
If migrating from Gradio SDK:
- β
README.md: Changed
sdk: gradioβsdk: docker - β
Dockerfile: Created/updated to use
main.py - β
Entry Point: Changed from
app.pyβmain.py - β Ports: Explicitly configured (7860, 5001)
What Still Works
- β All Gradio functionality preserved
- β All API endpoints work
- β Flask API integration works
- β Orchestrator initialization unchanged
Summary
β
Docker SDK: More control and flexibility
β
Dual Services: Gradio + Flask API
β
Production Ready: Standard Docker patterns
β
Health Checks: Built-in monitoring
β
Flexible: Easy to extend and customize
The application now uses Docker SDK for better control and flexibility while maintaining all existing functionality.