docker-entrypoint.sh 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/sh
  2. # Bambuddy container entrypoint.
  3. #
  4. # Runs as root (the image leaves USER unset, so containers start as
  5. # root by default), chowns /app/data and /app/logs to PUID:PGID, then
  6. # drops to PUID:PGID via gosu and execs the application. This fixes the
  7. # class of "Permission denied" errors that bit users when:
  8. #
  9. # - a Docker named volume was first created with root ownership and
  10. # the container was running with `user: 1000:1000` (named volumes
  11. # created by the daemon take its ownership; Dockerfile chmod hacks
  12. # cover the parent path but not subdirs created at runtime).
  13. # - a bind-mount source path didn't exist on the host yet, so dockerd
  14. # created it as root before the container started, leaving it
  15. # unwritable by uid 1000 inside the container — see #1211 / #668
  16. # for the virtual_printer bind-mount case the shipped compose
  17. # template ships uncommented.
  18. #
  19. # If the container is started with an explicit `user:` directive
  20. # (compose `user:` or `docker run --user`), the entrypoint runs as that
  21. # user instead of root and chown isn't possible. The script falls
  22. # through to direct exec without modifying ownership — preserving the
  23. # previous behavior for users who pin a specific uid via compose.
  24. set -eu
  25. # Default to 1000:1000 to match the legacy `user: "1000:1000"` default
  26. # in our previously-shipped compose template; overridable via env so
  27. # users who run docker as a different uid can match their host without
  28. # editing the compose user: directive.
  29. PUID="${PUID:-1000}"
  30. PGID="${PGID:-1000}"
  31. # If we're not root, we can't chown anything. Exec the original command
  32. # and trust that the user has set up host-side ownership themselves.
  33. if [ "$(id -u)" -ne 0 ]; then
  34. exec "$@"
  35. fi
  36. # `chown -R` is gated behind a top-level ownership check so a correctly-
  37. # owned directory isn't traversed on every container start. A user with
  38. # a multi-GB archive directory would otherwise pay seconds-to-minutes
  39. # of chown traversal at every restart.
  40. chown_if_needed() {
  41. target="$1"
  42. [ -d "$target" ] || mkdir -p "$target"
  43. current="$(stat -c '%u:%g' "$target" 2>/dev/null || echo '')"
  44. if [ "$current" != "$PUID:$PGID" ]; then
  45. echo "[entrypoint] chown -R ${PUID}:${PGID} ${target}"
  46. chown -R "${PUID}:${PGID}" "$target" || true
  47. fi
  48. }
  49. chown_if_needed /app/data
  50. chown_if_needed /app/logs
  51. # Bind-mount-source path needs the same treatment when present. dockerd
  52. # creates missing bind-mount sources as root on the host before the
  53. # container starts; the chown here propagates through the bind mount to
  54. # the host-side directory and fixes the issue once and for all.
  55. if [ -d /app/data/virtual_printer ]; then
  56. chown_if_needed /app/data/virtual_printer
  57. fi
  58. # Drop privileges and run the application. python's file capabilities
  59. # (cap_net_bind_service=+ep, set in the Dockerfile) survive the uid
  60. # switch, so binding to :322 / :990 still works post-drop.
  61. exec gosu "${PUID}:${PGID}" "$@"