Dockerfile Architecture
The Dockerfile (docker/Dockerfile) implements a multi-stage build with four distinct stages:
Base Stage
The base stage establishes the foundation for all subsequent stages:- Uses Python 3.13 slim Debian Bookworm image
- Includes
uvpackage manager from Astral - Sets unbuffered Python output for better logging
- Configures bytecode compilation for performance
Frontend Build Stage
Builds static assets using Node.js:- Compiled JavaScript bundles (via Rollup)
- Processed CSS and static assets
- Optimized templates
Python Build Stage
Installs Python dependencies and generates version information:- Installs build dependencies (gcc, git)
- Creates isolated virtual environment at
/opt/venv - Uses
uvfor fast dependency resolution - Generates version file with git commit and timestamp
Production Stage
Creates the minimal production image:- Runs as non-root user (
notify) - Minimal layer copying from build stages
- Proper file ownership and permissions
- Compiles Python bytecode for integrity
Test Stage
Extends production with testing tools:Building Images
Production Build
Build the production image:Test Build
Build with testing dependencies:Running Containers
Entrypoint Script
Theentrypoint.sh script provides flexible container startup:
Start Production Server
Start Development Server
Run Tests
Gunicorn Configuration
Production deployment uses Gunicorn with the following settings (gunicorn_config.py):
- Workers: 10 worker processes for concurrent request handling
- Worker class: Eventlet for async I/O operations
- Worker connections: 1000 concurrent connections per worker
- Keepalive: 90 seconds for connection reuse
- Timeout: Configurable via
HTTP_SERVE_TIMEOUT_SECONDS(default 30s)
Image Optimization
The multi-stage build provides several optimizations:- Layer separation: Build dependencies not included in production
- Bytecode compilation: Pre-compiled
.pycfiles for faster startup - Minimal base: Slim Debian image without unnecessary packages
- Cache optimization:
UV_CACHE_DIRuses/tmpfor build-time caching - Security: Non-root user with minimal permissions
Environment Variables
Key environment variables used in the Docker build:| Variable | Default | Purpose |
|---|---|---|
PYTHONUNBUFFERED | 1 | Disable Python output buffering |
UV_COMPILE_BYTECODE | 1 | Enable bytecode compilation |
UV_CACHE_DIR | /tmp/uv-cache/ | UV package cache location |
VIRTUAL_ENV | /opt/venv | Virtual environment path |
PATH | /opt/venv/bin:$PATH | Include venv in PATH |
PORT | Required | HTTP server port |
HTTP_SERVE_TIMEOUT_SECONDS | 30 | Gunicorn timeout |
Next Steps
- Review Production Deployment for deployment best practices
- See Configuration for environment setup