Comprehensive Docker Compose setup with all services:
Services:
• MariaDB 11 - Database with persistent storage
• FastAPI Backend - Python 3.12 with Poetry
• Next.js Frontend - Node 20 with production build
• Adminer - Optional database management UI
Features:
✓ Health checks for all services
✓ Proper dependency ordering (database -> backend -> frontend)
✓ Networking with private subnet (172.20.0.0/16)
✓ Volume management for data persistence
✓ Environment variable configuration
✓ Logging configuration (10MB max, 3 files)
✓ Restart policies (unless-stopped)
Configuration Files:
• docker-compose.yml - Production-ready compose file
• .env - Development environment variables
• .env.example - Template for environment setup
• DOCKER_SETUP.md - Comprehensive setup guide
Improvements:
• Added curl to backend Dockerfile for health checks
• Better error handling and startup sequencing
• Database initialization with multiple SQL files
• Adminer for easy database management (port 8080)
• Detailed logging with file rotation
• Production-ready with comments and documentation
Environment Variables:
Database:
DB_HOST=mariadb, DB_PORT=3306
DB_NAME=evoting_db, DB_USER=evoting_user
DB_PASSWORD=evoting_pass123
Services:
BACKEND_PORT=8000, FRONTEND_PORT=3000
Security:
SECRET_KEY=your-secret-key-change-in-production
DEBUG=false (for production)
Health Checks:
• Database: mariadb-admin ping
• Backend: curl /health endpoint
• Frontend: wget to port 3000
Volumes:
• evoting_data - MariaDB persistent storage
• backend_cache - Backend cache directory
Networks:
• evoting_network (172.20.0.0/16)
• Internal service-to-service communication
Quick Start:
1. cp .env.example .env
2. docker-compose up -d
3. http://localhost:3000 (frontend)
4. http://localhost:8000/docs (API docs)
5. http://localhost:8080 (database admin)
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
143 lines
3.9 KiB
YAML
143 lines
3.9 KiB
YAML
version: '3.8'
|
|
|
|
services:
|
|
# ================================================================
|
|
# MariaDB Database Service
|
|
# ================================================================
|
|
mariadb:
|
|
image: mariadb:11-latest
|
|
container_name: evoting_db
|
|
restart: unless-stopped
|
|
environment:
|
|
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootpass123}
|
|
MYSQL_DATABASE: ${DB_NAME:-evoting_db}
|
|
MYSQL_USER: ${DB_USER:-evoting_user}
|
|
MYSQL_PASSWORD: ${DB_PASSWORD:-evoting_pass123}
|
|
MYSQL_INITDB_SKIP_TZINFO: 1
|
|
ports:
|
|
- "${DB_PORT:-3306}:3306"
|
|
volumes:
|
|
- evoting_data:/var/lib/mysql
|
|
- ./docker/init.sql:/docker-entrypoint-initdb.d/01-init.sql
|
|
- ./docker/populate_past_elections.sql:/docker-entrypoint-initdb.d/02-populate.sql
|
|
networks:
|
|
- evoting_network
|
|
healthcheck:
|
|
test: ["CMD", "mariadb-admin", "ping", "-h", "localhost", "--silent"]
|
|
timeout: 20s
|
|
retries: 10
|
|
start_period: 40s
|
|
logging:
|
|
driver: "json-file"
|
|
options:
|
|
max-size: "10m"
|
|
max-file: "3"
|
|
|
|
# ================================================================
|
|
# Backend FastAPI Service
|
|
# ================================================================
|
|
backend:
|
|
build:
|
|
context: .
|
|
dockerfile: docker/Dockerfile.backend
|
|
container_name: evoting_backend
|
|
restart: unless-stopped
|
|
environment:
|
|
DB_HOST: mariadb
|
|
DB_PORT: 3306
|
|
DB_NAME: ${DB_NAME:-evoting_db}
|
|
DB_USER: ${DB_USER:-evoting_user}
|
|
DB_PASSWORD: ${DB_PASSWORD:-evoting_pass123}
|
|
SECRET_KEY: ${SECRET_KEY:-your-secret-key-change-in-production}
|
|
DEBUG: ${DEBUG:-false}
|
|
PYTHONUNBUFFERED: 1
|
|
ports:
|
|
- "${BACKEND_PORT:-8000}:8000"
|
|
depends_on:
|
|
mariadb:
|
|
condition: service_healthy
|
|
volumes:
|
|
- ./backend:/app/backend
|
|
- backend_cache:/app/.cache
|
|
networks:
|
|
- evoting_network
|
|
command: uvicorn backend.main:app --host 0.0.0.0 --port 8000 --reload
|
|
healthcheck:
|
|
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
start_period: 40s
|
|
logging:
|
|
driver: "json-file"
|
|
options:
|
|
max-size: "10m"
|
|
max-file: "3"
|
|
|
|
# ================================================================
|
|
# Frontend Next.js Service
|
|
# ================================================================
|
|
frontend:
|
|
build:
|
|
context: .
|
|
dockerfile: docker/Dockerfile.frontend
|
|
args:
|
|
NEXT_PUBLIC_API_URL: http://localhost:${BACKEND_PORT:-8000}
|
|
container_name: evoting_frontend
|
|
restart: unless-stopped
|
|
ports:
|
|
- "${FRONTEND_PORT:-3000}:3000"
|
|
depends_on:
|
|
- backend
|
|
environment:
|
|
NEXT_PUBLIC_API_URL: http://localhost:${BACKEND_PORT:-8000}
|
|
NODE_ENV: production
|
|
networks:
|
|
- evoting_network
|
|
healthcheck:
|
|
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:3000/"]
|
|
interval: 30s
|
|
timeout: 10s
|
|
retries: 3
|
|
start_period: 40s
|
|
logging:
|
|
driver: "json-file"
|
|
options:
|
|
max-size: "10m"
|
|
max-file: "3"
|
|
|
|
# ================================================================
|
|
# Optional: Adminer (Database Management UI)
|
|
# Access at http://localhost:8080
|
|
# ================================================================
|
|
adminer:
|
|
image: adminer:latest
|
|
container_name: evoting_adminer
|
|
restart: unless-stopped
|
|
ports:
|
|
- "8080:8080"
|
|
depends_on:
|
|
- mariadb
|
|
networks:
|
|
- evoting_network
|
|
environment:
|
|
ADMINER_DEFAULT_SERVER: mariadb
|
|
logging:
|
|
driver: "json-file"
|
|
options:
|
|
max-size: "10m"
|
|
max-file: "3"
|
|
|
|
volumes:
|
|
evoting_data:
|
|
driver: local
|
|
backend_cache:
|
|
driver: local
|
|
|
|
networks:
|
|
evoting_network:
|
|
driver: bridge
|
|
ipam:
|
|
config:
|
|
- subnet: 172.20.0.0/16
|