docker-entrypoint.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env bash
  2. # R01-N22: deploy-time migrations.
  3. #
  4. # Apply any pending SQL migrations before Apache starts to serve traffic, so
  5. # the request path can simply CHECK the schema state and refuse to serve when
  6. # something is unexpectedly out of date — no half-applied DDL hazard.
  7. #
  8. # Failure here aborts the container start (we exit non-zero) so the operator
  9. # notices in `docker logs`. Apache otherwise picks up the trailing args
  10. # verbatim (CMD `apache2-foreground`).
  11. set -euo pipefail
  12. APP_ROOT="${APP_ROOT:-/var/www/html}"
  13. # R01-N27: session-storage path (defaults match Dockerfile + .env.example).
  14. SESSION_PATH="${SESSION_PATH:-/var/www/data/sessions}"
  15. # R01-N27: session-file lifetime in minutes. Default 480 (= 8h), matching
  16. # `session.gc_maxlifetime` set by `SessionGuard::start()`. Operators may
  17. # override via the container env (e.g. tighten on a public deployment).
  18. SESSION_GC_MAX_AGE_MINUTES="${SESSION_GC_MAX_AGE_MINUTES:-480}"
  19. # R01-N27: how often to sweep, in seconds. Default 3600 (= once per hour).
  20. SESSION_GC_INTERVAL_SECONDS="${SESSION_GC_INTERVAL_SECONDS:-3600}"
  21. echo "[entrypoint] running deploy-time migrations…"
  22. php "${APP_ROOT}/bin/migrate.php"
  23. # R01-N27: PHP's built-in session GC fires probabilistically off request
  24. # traffic, so a low-traffic deployment keeps stale session files for days
  25. # past `gc_maxlifetime`. This backgrounded loop deletes session files
  26. # older than $SESSION_GC_MAX_AGE_MINUTES every $SESSION_GC_INTERVAL_SECONDS.
  27. # It is a child of this script's PID 1, so a `docker stop` propagates and
  28. # tears it down cleanly along with Apache. No new package dependency — only
  29. # coreutils' `find`, already present in the php:8.3-apache base image.
  30. if [ -d "${SESSION_PATH}" ]; then
  31. echo "[entrypoint] starting session GC loop (path=${SESSION_PATH}, max-age=${SESSION_GC_MAX_AGE_MINUTES}m, every ${SESSION_GC_INTERVAL_SECONDS}s)"
  32. (
  33. while true; do
  34. sleep "${SESSION_GC_INTERVAL_SECONDS}"
  35. # `-mmin +N` matches files older than N minutes; `-type f` avoids
  36. # touching the directory itself; errors swallowed so a transient
  37. # filesystem hiccup does not kill the loop.
  38. find "${SESSION_PATH}" -mindepth 1 -type f -mmin +"${SESSION_GC_MAX_AGE_MINUTES}" -delete 2>/dev/null || true
  39. done
  40. ) &
  41. fi
  42. echo "[entrypoint] starting: $*"
  43. exec "$@"