#!/usr/bin/env bash
# Real demos for P127 "How to Fix nginx 502 Bad Gateway Errors on Linux". Made on the Mac (Docker Desktop), like P093.
# One container, "gym": a stock Ubuntu 24.04 with systemd as PID 1, Ubuntu's nginx 1.24 in front and Ubuntu's
# gunicorn running Ana's small Python booking app behind it (lab/Dockerfile, lab/booking/). nginx's port 80 is
# published to this Mac's localhost only, for the real browser captures (lab/capture.py). Every command shown is typed
# into a real interactive bash in a pty (demo_driver.py); every 502, 504 and timing is a real request.
set -euo pipefail
cd "$(dirname "$0")"
cleanup() { docker rm -f ce-p127-gym >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t ce-p127-gym lab >/dev/null
docker run -d --name ce-p127-gym --hostname gym -p 127.0.0.1:18127:80 \
  --privileged --cgroupns=private -v /sys/fs/cgroup:/sys/fs/cgroup:rw --tmpfs /run --tmpfs /run/lock \
  ce-p127-gym >/dev/null
python3 - ce-p127 < demo_driver.py
