FastAPI backend application with PostgreSQL database, Alembic migrations, and Docker containerization.
- Docker
- Docker Compose
make allThis will:
- Build Docker images
- Start PostgreSQL and FastAPI services
- Run database migrations automatically
- Enable hot reload for development
Access Points:
- API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
make stop # Stop containers, keep dataor press Ctrl-C if running in foreground.
make up # Restart with existing datamake all # Build and start everything (default)
make up # Start services in background
make stop # Stop containers (keeps data)
make down # Stop and remove containers (keeps volumes)
make clean # Remove everything including database datamake logs # Follow live logs (Ctrl-C to stop)
make logs-tail # Show last 100 log linesmake migrate # Manually run migrations (usually not needed)koreji-backend/
├── src/
│ ├── main.py # FastAPI application entry point
│ ├── database.py # Database configuration
│ ├── models/ # SQLAlchemy models
│ │ ├── __init__.py
│ │ └── user.py
│ └── users/ # Users module
│ ├── router.py # API endpoints
│ ├── service.py # Business logic
│ └── schemas.py # Pydantic schemas
├── alembic/ # Database migrations
│ ├── env.py
│ └── versions/
├── docker-compose.yaml # Service orchestration
├── Dockerfile # Container definition
├── entrypoint.sh # Container startup script
├── requirements.txt # Python dependencies
├── alembic.ini # Alembic configuration
└── Makefile # Development commands
Migrations run automatically when containers start. For manual migration management:
- Modify or add models in
src/models/ - Generate migration script:
# Inside the container
docker-compose exec koreji-backend alembic revision --autogenerate -m "description"
# Or from host (if alembic installed locally)
alembic revision --autogenerate -m "description"- Review the generated file in
alembic/versions/ - Restart containers to apply:
make up# Apply migrations
docker-compose exec koreji-backend alembic upgrade head
# Rollback one migration
docker-compose exec koreji-backend alembic downgrade -1
# View migration history
docker-compose exec koreji-backend alembic history
# Check current version
docker-compose exec koreji-backend alembic currentCode changes in src/ are automatically detected and reload the server.
# Follow all logs
make logs
# Show recent logs
make logs-tail
# Follow specific service
docker-compose logs -f koreji-backend
docker-compose logs -f postgres# Connect to PostgreSQL
docker-compose exec postgres psql -U koreji -d koreji_db
# Common queries
\dt # List tables
\d users # Describe users table
SELECT * FROM users; # Query usersSet in docker-compose.yaml:
DATABASE_URL- PostgreSQL connection stringPYTHONUNBUFFERED- Python output buffering (set to 1)
For local development without Docker, create a .env file:
DATABASE_URL=postgresql://koreji:koreji_password@localhost:5432/koreji_dbFor production deployment:
- Change
fastapi devtofastapi runinentrypoint.sh - Set proper environment variables
- Use secrets management for credentials
- Configure proper logging
- Set up reverse proxy (nginx/traefik)
- Enable HTTPS
- Configure CORS properly
- Set up monitoring and health checks