#!/bin/sh
# HOWPL: run one site's Docker daemon ROOTLESS, as that site's system user.
# Started only by howpl-docker@<user>.service. It never uses or changes the
# host's Docker daemon (/var/run/docker.sock) and refuses to run as root, so
# there is no rootful fallback. The daemon lives in a rootlesskit user
# namespace with slirp4netns networking; published ports bind on the host
# through rootlesskit's port driver. cgroup v2 limits are enforced through
# the user's systemd manager (user@<uid>.service with delegated controllers).
set -eu
: "${HOWPL_DOCKER_DATA:?}"
if [ -z "${_HOWPL_IN_ROOTLESSKIT:-}" ]; then
  U=$(id -u)
  [ "$U" -ne 0 ] || { echo "howpl-dockerd-rootless: refusing to run as root" >&2; exit 1; }
  export _HOWPL_IN_ROOTLESSKIT=1 XDG_RUNTIME_DIR="/run/user/$U" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$U/bus"
  exec rootlesskit --state-dir="$XDG_RUNTIME_DIR/rootlesskit" --net=slirp4netns --mtu=65520 \
    --slirp4netns-sandbox=auto --slirp4netns-seccomp=auto --disable-host-loopback --port-driver=builtin \
    --copy-up=/etc --copy-up=/run --propagation=rslave "$0" "$@"
fi
# Inside the namespace: drop the parent's runtime links so dockerd creates its own.
rm -f /run/docker /run/containerd /run/xtables.lock
exec dockerd --data-root="$HOWPL_DOCKER_DATA" --exec-root="$XDG_RUNTIME_DIR/docker" --pidfile="$XDG_RUNTIME_DIR/docker.pid" \
  -H "unix://$XDG_RUNTIME_DIR/docker.sock" --userland-proxy-path=/usr/bin/docker-proxy --log-opt max-size=10m --log-opt max-file=3 "$@"
