# Database Containers

## PostgreSQL
- **Image**: `postgres:16-alpine` (ARM-compatible)
- **Required env**: `POSTGRES_PASSWORD` (mandatory), `POSTGRES_USER` (default: `postgres`), `POSTGRES_DB` (default: same as user)
- **Data volume**: Mount `./pgdata:/var/lib/postgresql/data` for persistence
- **Init scripts**: Mount `.sql` or `.sh` files into `/docker-entrypoint-initdb.d/` — they run once on first start
- **Healthcheck**: `pg_isready -U postgres`
- **Gotcha**: First start takes 10–30s to initialise. Use `depends_on` with `condition: service_healthy`

## MySQL / MariaDB
- **Image**: `mysql:8` or `mariadb:11` (both ARM-compatible)
- **Required env**: `MYSQL_ROOT_PASSWORD` (mandatory). Optional: `MYSQL_DATABASE`, `MYSQL_USER`, `MYSQL_PASSWORD`
- **Data volume**: `./mysql_data:/var/lib/mysql`
- **Init scripts**: `/docker-entrypoint-initdb.d/` (`.sql`, `.sql.gz`, `.sh`)
- **Healthcheck**: `mysqladmin ping -h localhost`
- **Gotcha**: MySQL 8 uses `caching_sha2_password` by default — older clients may need `--default-authentication-plugin=mysql_native_password`

## Redis
- **Image**: `redis:7-alpine` (ARM-compatible)
- **No auth by default**: Add `--requirepass $REDIS_PASSWORD` to `command:` if auth is needed
- **Data volume**: `./redis_data:/data` with `command: ["redis-server", "--appendonly", "yes"]` for persistence
- **Healthcheck**: `redis-cli ping`
- **Gotcha**: Without `appendonly`, data is lost on restart

## MongoDB
- **Image**: `mongo:7` (ARM-compatible from 5.0+)
- **Auth env**: `MONGO_INITDB_ROOT_USERNAME`, `MONGO_INITDB_ROOT_PASSWORD`
- **Data volume**: `./mongo_data:/data/db`
- **Init scripts**: `/docker-entrypoint-initdb.d/` (`.js`, `.sh`)
- **Healthcheck**: `mongosh --eval "db.adminCommand('ping')"`
- **Gotcha**: MongoDB <5.0 does NOT support ARM64. Use `mongo:5` minimum on Raspberry Pi / Apple Silicon

## SQLite (in-container)
- SQLite doesn't need a container — it's a file. Mount the directory containing the `.db` file as a volume
- Ensure the container user has write permissions on the directory
- Common pattern: `./data:/app/data` where the app writes `data/app.db`

## General Patterns
- Always set `restart: unless-stopped` on database services
- Always add a `healthcheck` — other services should `depends_on` with `condition: service_healthy`
- Pin image versions (e.g. `postgres:16`, not `postgres:latest`)
- Use named volumes for production, bind mounts for development
