#!/usr/bin/env bash
# Real demos for P137 "How to Fix Docker 'Permission Denied' on docker.sock". Runs on the Mac's Docker Desktop.
# One container, ce-p137-pc (a music school's office PC): a stock Ubuntu 24.04 with systemd as PID 1 and Ubuntu's own
# Docker (docker.io; lab/laptop/Dockerfile), run --privileged with its own cgroup namespace and fresh volumes, so its
# Docker starts empty, and with --restart always, so `sudo reboot` inside it is a real reboot (P100). The images it
# uses (nginx:alpine for the school's booking page, alpine:3.22, and docker:24-cli for the older client's wording)
# are pulled here and loaded into the PC off-screen, so it needs no internet. Every command shown is typed into a
# real login shell in a pty (demo_driver.py).
set -euo pipefail
cd "$(dirname "$0")"
cleanup() { docker rm -f -v ce-p137-pc >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t ce-p137-laptop lab/laptop >/dev/null
for img in nginx:alpine alpine:3.22 docker:24-cli; do docker pull -q "$img" >/dev/null; done
docker run -d --name ce-p137-pc --restart always --hostname office-pc --privileged --cgroupns=private \
  --tmpfs /run --tmpfs /run/lock -v /var/lib/docker -v /var/lib/containerd ce-p137-laptop >/dev/null
for i in $(seq 60); do docker exec ce-p137-pc docker info >/dev/null 2>&1 && break; sleep 1; done
docker save nginx:alpine alpine:3.22 docker:24-cli | docker exec -i ce-p137-pc docker load >/dev/null
# the school's booking page, started by Ana when she set the PC up
docker exec ce-p137-pc docker run -d --restart unless-stopped --name bookings -p 8080:80 nginx:alpine >/dev/null
python3 demo_driver.py ce-p137-pc
