3.4 KiB
HXBooks Docker Deployment
This directory contains Docker Compose configuration for deploying HXBooks with Caddy as a reverse proxy.
Architecture
- App Container: Runs the Flask application on port 5000
- Caddy Container: Reverse proxy with automatic HTTPS, serves static files directly
- Database Initialization: Automatic on first startup (creates SQLite database and runs migrations)
- Volumes:
media: Book cover images (shared between app and Caddy)static: CSS/JS files (copied from app to volume on startup, served by Caddy)instance: Application configuration and SQLite databasecaddy_data: TLS certificatescaddy_config: Caddy configuration cache
Quick Start
-
Development (localhost):
docker-compose up -dAccess the app at http://localhost
-
Production with your domain:
- Edit
Caddyfileand replacelocalhostwith your domain - Run:
docker-compose up -d - Caddy will automatically obtain TLS certificates
- Edit
Configuration
Environment Variables
Set these in your docker-compose.yml or create a .env file:
# Flask configuration
FLASK_ENV=production
SECRET_KEY=your-secret-key-here
# Optional: Google Books API key
GOOGLE_BOOKS_API_KEY=your-api-key
Custom Configuration
Create instance/config.py for local overrides:
SECRET_KEY = "your-production-secret-key"
GOOGLE_BOOKS_API_KEY = "your-api-key"
# Add other Flask config as needed
Commands
# Start services
docker-compose up -d
# View logs
docker-compose logs -f app
docker-compose logs -f caddy
# Stop services
docker-compose down
# Rebuild and restart
docker-compose up --build -d
# Remove everything including volumes (⚠️ destroys data)
docker-compose down -v
File Serving Strategy
- Static files (CSS/JS): Copied from app container to shared volume on startup, served directly by Caddy
- Media files (book covers): Stored in shared volume, served directly by Caddy
- Application routes: Proxied to Flask app
This setup provides optimal performance by having Caddy serve static assets while the Flask app handles dynamic content.
Database Initialization
The application handles database setup automatically on first startup:
- Fresh Install: Creates SQLite database and initializes tables using
hxbooks db init - Existing Database: Runs Flask-Migrate migrations with
flask db upgrade - Database Location:
/app/instance/hxbooks.sqlite(persisted ininstancevolume)
Manual Database Operations (if needed):
# Connect to running container
docker-compose exec app sh
# Initialize database manually
hxbooks db init
# Create new migration
flask db migrate -m "description"
# Apply migrations
flask db upgrade
Production Notes
-
Database: Currently uses SQLite with volume persistence. For high-traffic sites, consider PostgreSQL.
-
Backups: The important data is in the
instanceandmediavolumes:# Backup docker run --rm -v hxbooks_instance:/source -v $(pwd):/backup alpine tar czf /backup/instance-backup.tar.gz -C /source . docker run --rm -v hxbooks_media:/source -v $(pwd):/backup alpine tar czf /backup/media-backup.tar.gz -C /source . -
Updates: To update the application:
docker-compose pull docker-compose up --build -d -
Monitoring: Consider adding health check endpoints and monitoring services to the stack.