# /srv/apps/docker-compose.yml
#
# One stanza per app. Adding a fourth app is six lines here, three in the
# Caddyfile, one in .env, and one CREATE DATABASE (see init-databases.sql).
#
# The apps publish no host ports. Only Caddy is reachable from the internet;
# everything else is on the private compose network. That is what lets every
# app sit on :443 behind its own hostname instead of :8001, :8002.

name: cs347

x-django: &django
  restart: unless-stopped
  env_file: [.env]
  expose: ["8000"]
  depends_on:
    postgres:
      condition: service_healthy

services:
  caddy:
    image: caddy:2-alpine
    restart: unless-stopped
    ports: ["80:80", "443:443"]
    env_file: [.env]
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      # Certificates live in /data. This volume MUST persist: wipe it and Caddy
      # re-requests every certificate, and Let's Encrypt allows only 5 identical
      # requests per week before it starts refusing. `docker compose down` is
      # fine; `docker compose down -v` is the one to avoid.
      - caddy_data:/data
      - caddy_config:/config

  # One Postgres serving one database per app. A managed Cloud SQL instance per
  # student would be ~$10/month each — about $300/month for the class — for a
  # teaching database that holds forty rows. This is the same Postgres.
  postgres:
    image: postgres:17-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: postgres
    volumes:
      - pg_data:/var/lib/postgresql/data
      # Runs ONLY when the data directory is empty, i.e. the very first start.
      # Adding an app later means creating its database by hand — see README.
      - ./init-databases.sql:/docker-entrypoint-initdb.d/init-databases.sql:ro
    # Modest defaults; the box has 1 GB of RAM and five other things to run.
    command: postgres -c shared_buffers=64MB -c max_connections=50
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d postgres"]
      interval: 5s
      timeout: 5s
      retries: 12

  locallibrary:
    <<: *django
    build: ./locallibrary
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/locallibrary
      REDIS_URL: redis://redis:6379
    # A YAML merge key does not combine mappings, it replaces them — so this
    # depends_on must repeat the postgres condition from the anchor above.
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started

  kahduke:
    <<: *django
    build: ./kahduke
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/kahduke

  final_project:
    <<: *django
    build: ./final_project
    environment:
      DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/final_project

  # Shared by any app that needs a channel layer or a cache.
  # One Redis for all of them — there is no reason to run three.
  redis:
    image: redis:7-alpine
    restart: unless-stopped

volumes:
  caddy_data:
  caddy_config:
  pg_data:
