Dockerfile 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Build frontend. Its output is plain JS/CSS, the same on every architecture,
  2. # so it is built natively on the build host: under QEMU emulation the arm64
  3. # vite build ran for hours or hung.
  4. FROM --platform=$BUILDPLATFORM node:22-bookworm-slim AS frontend-builder
  5. WORKDIR /app/frontend
  6. # Copy package files first for better caching
  7. COPY frontend/package*.json ./
  8. # Use cache mount for npm
  9. RUN --mount=type=cache,target=/root/.npm \
  10. npm ci
  11. COPY frontend/ ./
  12. RUN npm run build
  13. # Production image
  14. FROM python:3.13-slim-trixie
  15. WORKDIR /app
  16. # Install system dependencies
  17. ENV DEBIAN_FRONTEND=noninteractive
  18. RUN apt-get update && apt-get install -y --no-install-recommends \
  19. curl \
  20. ffmpeg \
  21. gnupg \
  22. gosu \
  23. iproute2 \
  24. libcap2-bin \
  25. openssh-client \
  26. ca-certificates \
  27. && rm -rf /var/lib/apt/lists/*
  28. # Install the Tailscale CLI only (no tailscaled — the daemon runs on the host).
  29. # Bambuddy calls `tailscale status` / `tailscale cert` via the host's socket,
  30. # which the user mounts in via docker-compose when they want to enable the
  31. # Tailscale integration for virtual printers. Without the socket mount, the
  32. # binary is harmless — the code logs a hint and falls back to self-signed.
  33. RUN curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.noarmor.gpg \
  34. -o /usr/share/keyrings/tailscale-archive-keyring.gpg \
  35. && curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.tailscale-keyring.list \
  36. -o /etc/apt/sources.list.d/tailscale.list \
  37. && apt-get update && apt-get install -y --no-install-recommends tailscale \
  38. && rm -rf /var/lib/apt/lists/*
  39. # Allow binding to privileged ports (e.g. 990/FTPS) as non-root user.
  40. # File capabilities are more reliable than Docker cap_add with user: directive,
  41. # which depends on ambient capability support in the container runtime.
  42. RUN setcap cap_net_bind_service=+ep "$(readlink -f /usr/local/bin/python3)"
  43. # Install Python dependencies with cache mount.
  44. # pip is upgraded to >=26.1 first to close CVE-2026-6357 — the python:3.13-slim
  45. # base image ships pip 26.0.1, which runs its self-update check after installing
  46. # wheels (so a hostile wheel could hijack stdlib imports during install).
  47. # requirements.lock pins the exact versions that were tested (see CONTRIBUTING.md).
  48. COPY requirements.txt requirements.lock ./
  49. RUN --mount=type=cache,target=/root/.cache/pip \
  50. pip install --root-user-action=ignore --upgrade 'pip>=26.1.2' \
  51. && pip install --root-user-action=ignore -r requirements.lock
  52. # Copy backend
  53. COPY backend/ ./backend/
  54. # Capture the current git branch at build time. `.git/HEAD` is the only
  55. # .git metadata the build context lets through (see .dockerignore); it
  56. # contains `ref: refs/heads/<branch>`, which the SpoolBuddy remote-update
  57. # flow reads at runtime via detect_current_branch() in spoolbuddy_ssh.py.
  58. # Without this, the production image has no git metadata at all and would
  59. # always pull `main` on the remote device regardless of which branch
  60. # Bambuddy itself was built from.
  61. COPY .git/HEAD ./.git/HEAD
  62. # Copy built frontend from builder stage
  63. COPY --from=frontend-builder /app/static ./static
  64. # Create data directories. Ownership is normalised at startup by the
  65. # entrypoint (chowns to PUID:PGID and drops privileges via gosu before
  66. # exec'ing the app), so we don't need a chmod 777 hack here — that was
  67. # the workaround for the previous compose `user: "1000:1000"` model and
  68. # only worked when the volume's perms happened to survive (named volume
  69. # first-create case; bind-mount-source case bit users in #1211 / #668).
  70. #
  71. # The sentinel file is needed so a freshly-created Docker named volume
  72. # isn't "empty" from Docker's POV. On empty volumes Docker resyncs the
  73. # directory metadata (incl. ownership) from the image on every mount,
  74. # which would mean our entrypoint chown gets reverted on every restart
  75. # and re-fired on every start (slow on multi-GB archive dirs). With a
  76. # sentinel inside the volume on first mount, Docker considers the
  77. # volume populated and stops resyncing, so the chown is genuinely
  78. # one-shot.
  79. RUN mkdir -p /app/data /app/logs && \
  80. : >/app/data/.bambuddy && \
  81. : >/app/logs/.bambuddy
  82. # Entrypoint script: handles PUID/PGID + ownership normalisation +
  83. # privilege drop. See deploy/docker-entrypoint.sh for the full rationale.
  84. COPY deploy/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
  85. RUN chmod +x /usr/local/bin/docker-entrypoint.sh
  86. # Environment variables
  87. ENV PYTHONUNBUFFERED=1
  88. ENV DATA_DIR=/app/data
  89. ENV LOG_DIR=/app/logs
  90. ENV PORT=8000
  91. # Provide a local username + home for tools that call getpass.getuser() /
  92. # os.path.expanduser() under arbitrary PUIDs. With `user: "1001:1001"` the
  93. # stock python:3.13-slim image has no /etc/passwd entry for that UID, so
  94. # pwd.getpwuid() raises and breaks libraries that do host-level user lookups
  95. # (notably asyncssh, which uses the local username for ~/.ssh/config host
  96. # matching during the SpoolBuddy remote-update flow). Setting LOGNAME/USER
  97. # makes getpass.getuser() resolve via env vars instead of the passwd db;
  98. # HOME=/app gives a writable home that is guaranteed to exist.
  99. ENV HOME=/app
  100. ENV USER=bambuddy
  101. ENV LOGNAME=bambuddy
  102. # Matplotlib (imported lazily by the STL thumbnail generator) tries to create
  103. # its font/style cache at $HOME/.config/matplotlib on first import. /app is
  104. # root-owned and not writable by the PUID:PGID the entrypoint drops to,
  105. # which trips an EPERM warning in everyone's logs and forces matplotlib
  106. # to fall back to a per-restart temp dir (paying the font-scan cost on
  107. # every container restart). Pinning the cache dir to /tmp/matplotlib
  108. # silences the warning and keeps the cache alive for the container's
  109. # lifetime. /tmp is writable by any uid, so this works regardless of PUID.
  110. ENV MPLCONFIGDIR=/tmp/matplotlib
  111. EXPOSE 322
  112. EXPOSE 990
  113. EXPOSE 3000
  114. EXPOSE 3002
  115. EXPOSE 6000
  116. EXPOSE 8000
  117. EXPOSE 8883
  118. EXPOSE 50000-50100
  119. # Health check (uses PORT env var via shell)
  120. HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
  121. CMD python -c "import urllib.request, os; urllib.request.urlopen(f'http://localhost:{os.environ.get(\"PORT\", \"8000\")}/health')" || exit 1
  122. # Run the application
  123. # Use standard asyncio loop (uvloop has permission issues in some Docker environments)
  124. # Port is configurable via PORT (default 8000); bind address via HOST (default
  125. # 0.0.0.0). Set HOST=127.0.0.1 to bind loopback only, e.g. when a reverse proxy
  126. # on the same host fronts the app.
  127. #
  128. # `exec` is load-bearing, not style. Without it the shell stays as PID 1 and
  129. # uvicorn runs as its child; dash does not forward signals, so `docker stop`
  130. # SIGTERMs the shell and uvicorn never hears about it. Every stop then ran to
  131. # the end of the grace period and died on SIGKILL (exit 137) — no WAL
  132. # checkpoint, no MQTT disconnect, no virtual-printer teardown, on every restart
  133. # and every image update. With `exec`, uvicorn *is* PID 1 and gets the signal.
  134. #
  135. # --timeout-graceful-shutdown caps the wait on in-flight requests. Uvicorn's
  136. # default is to wait forever, and an MJPEG camera stream is a response that
  137. # never completes, so a single open camera tile would otherwise pin the process
  138. # past Docker's 10s grace and back into SIGKILL. On timeout uvicorn cancels the
  139. # request tasks; the camera generators already unwind cleanly on CancelledError.
  140. ENV UVICORN_TIMEOUT_GRACEFUL_SHUTDOWN=5
  141. ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
  142. CMD ["sh", "-c", "exec uvicorn backend.app.main:app --host ${HOST:-0.0.0.0} --port ${PORT:-8000} --loop asyncio --timeout-graceful-shutdown ${UVICORN_TIMEOUT_GRACEFUL_SHUTDOWN}"]