Overview
FKApi includes a complete Docker setup with:- Multi-stage Dockerfile for optimized images
- docker-compose.yml with all services
- Service profiles for flexible deployments
- Health checks for all containers
- Volume management for data persistence
Prerequisites
Before you begin, install:- Docker 20.10 or higher
- Docker Compose V2 or higher
- Git
- Linux
- macOS
- Windows
Quick Start
# Django Settings
DJANGO_SECRET_KEY=your-secret-key-here
DJANGO_DEBUG=False
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1,yourdomain.com
DJANGO_PORT=8000
# PostgreSQL
POSTGRES_DB=fkapi
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secure_password
POSTGRES_PORT=5432
# Redis
REDIS_PORT=6379
# Celery
ENABLE_CELERY=True
# Flower
FLOWER_PORT=5555
# Monitoring (optional)
PROMETHEUS_PORT=9090
# Build images
docker compose build
# Start all services
docker compose up -d
# View logs
docker compose logs -f
# Run database migrations
docker compose exec web python manage.py migrate
# Create superuser
docker compose exec web python manage.py createsuperuser
Docker Architecture
Dockerfile
FKApi uses a multi-stage build for optimization:- Multi-stage build: Reduces final image size
- Layer caching: Faster rebuilds
- Health checks: Automatic container monitoring
- Dynamic command: Development vs production mode
Services
Thedocker-compose.yml defines these services:
Core Services (Always Active)
Database (db)Monitoring Services (Optional)
Monitoring services use themonitoring profile:
Prometheus (prometheus)
Service Profiles
FKApi uses Docker Compose profiles for flexible deployments:Default Profile (Minimal Setup)
Start only core services:- PostgreSQL database
- Redis cache
- Django web application
- Celery worker
- Celery beat
- Flower
Monitoring Profile
Add monitoring stack:- Prometheus (metrics collection)
- Redis Exporter (Redis metrics)
- PostgreSQL Exporter (database metrics)
Common Operations
Viewing Logs
Restarting Services
Rebuilding Images
Running Commands
Managing Data
Production Deployment
Security Hardening
For production, update your.env:
Using Docker Secrets
For sensitive data, use Docker secrets:Resource Limits
Set resource limits for production:Health Checks
All services include health checks. Monitor with:Troubleshooting
Container Fails to Start
Check logs:- Environment variable errors
- Database not ready (increase start_period in health check)
- Port conflicts
- Permission issues
Database Connection Errors
Verify database is running:Redis Connection Errors
Test Redis connection:Port Already in Use
Change ports in.env:
Out of Disk Space
Clean up Docker:Best Practices
Environment Variables
Environment Variables
- Never commit
.envfiles - Use
.env.exampleas template - Use Docker secrets for production
- Document all variables
- Validate required variables on startup
Data Persistence
Data Persistence
- Use named volumes for data
- Regular database backups
- Test restore procedures
- Monitor disk usage
- Implement backup rotation
Networking
Networking
- Use Docker networks for service isolation
- Expose only necessary ports
- Use reverse proxy (nginx) for production
- Enable HTTPS/TLS
- Implement rate limiting
Logging
Logging
- Configure log rotation
- Use centralized logging
- Monitor log volume
- Set appropriate log levels
- Parse logs for errors