#!/usr/bin/env bash
# Real demos for P077 "What Happens When You Run a Docker Container". Runs on render01.
# One container, ce-p077-laptop (Ana's laptop): a stock Ubuntu 24.04 with systemd as PID 1 and Ubuntu's own
# Docker (docker.io: dockerd, containerd, runc; lab/Dockerfile), run --privileged with its own cgroup namespace and
# a fresh volume for /var/lib/docker, so its Docker starts empty. Inside it the laptop sees what Docker builds for
# one `docker run`: the image pull, the API call over Docker's socket, the container's process, its namespaces,
# its cgroup memory limit, its overlay filesystem, its veth cable and the NAT rule for its port.
# Every command shown is typed into a real login shell in a pty (demo_driver.py).
# Needs: Docker on render01 and internet for Docker Hub (nginx:alpine).
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p077
cleanup() { docker rm -f -v $P-laptop >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t $P-laptop lab >/dev/null
# pyte (a VT100 screen in Python) rebuilds the final screen of the pull's live progress; render01's Python has no pip.
W=/tmp/$P && rm -rf $W && mkdir -p $W/py
docker run --rm -u "$(id -u):$(id -g)" -e HOME=/tmp -v $W/py:/out python:3.13-slim pip install -q --target /out pyte==0.8.2 >/dev/null 2>&1
docker run -d --name $P-laptop --hostname laptop --privileged --cgroupns=private \
  --tmpfs /run --tmpfs /run/lock -v /var/lib/docker -v /var/lib/containerd $P-laptop >/dev/null
for i in $(seq 60); do
  docker exec $P-laptop docker info >/dev/null 2>&1 && break; sleep 1
done
docker exec $P-laptop docker info >/dev/null
PYTHONPATH=$W/py python3 - "$P-laptop" <demo_driver.py
