Dockerfile 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Build frontend
  2. FROM node:22-bookworm-slim AS frontend-builder
  3. WORKDIR /app/frontend
  4. # Copy package files first for better caching
  5. COPY frontend/package*.json ./
  6. # Use cache mount for npm
  7. RUN --mount=type=cache,target=/root/.npm \
  8. npm ci
  9. COPY frontend/ ./
  10. RUN npm run build
  11. # Production image
  12. FROM python:3.13-slim-trixie
  13. WORKDIR /app
  14. # Install system dependencies
  15. ENV DEBIAN_FRONTEND=noninteractive
  16. RUN apt-get update && apt-get install -y --no-install-recommends \
  17. curl \
  18. ffmpeg \
  19. gnupg \
  20. gosu \
  21. iproute2 \
  22. libcap2-bin \
  23. openssh-client \
  24. && rm -rf /var/lib/apt/lists/*
  25. # Install the Tailscale CLI only (no tailscaled — the daemon runs on the host).
  26. # Bambuddy calls `tailscale status` / `tailscale cert` via the host's socket,
  27. # which the user mounts in via docker-compose when they want to enable the
  28. # Tailscale integration for virtual printers. Without the socket mount, the
  29. # binary is harmless — the code logs a hint and falls back to self-signed.
  30. RUN curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.noarmor.gpg \
  31. -o /usr/share/keyrings/tailscale-archive-keyring.gpg \
  32. && curl -fsSL https://pkgs.tailscale.com/stable/debian/trixie.tailscale-keyring.list \
  33. -o /etc/apt/sources.list.d/tailscale.list \
  34. && apt-get update && apt-get install -y --no-install-recommends tailscale \
  35. && rm -rf /var/lib/apt/lists/*
  36. # Allow binding to privileged ports (e.g. 990/FTPS) as non-root user.
  37. # File capabilities are more reliable than Docker cap_add with user: directive,
  38. # which depends on ambient capability support in the container runtime.
  39. RUN setcap cap_net_bind_service=+ep "$(readlink -f /usr/local/bin/python3)"
  40. # Install Python dependencies with cache mount.
  41. # pip is upgraded to >=26.1 first to close CVE-2026-6357 — the python:3.13-slim
  42. # base image ships pip 26.0.1, which runs its self-update check after installing
  43. # wheels (so a hostile wheel could hijack stdlib imports during install).
  44. COPY requirements.txt ./
  45. RUN --mount=type=cache,target=/root/.cache/pip \
  46. pip install --root-user-action=ignore --upgrade 'pip>=26.1' \
  47. && pip install --root-user-action=ignore -r requirements.txt
  48. # Copy backend
  49. COPY backend/ ./backend/
  50. # Capture the current git branch at build time. `.git/HEAD` is the only
  51. # .git metadata the build context lets through (see .dockerignore); it
  52. # contains `ref: refs/heads/<branch>`, which the SpoolBuddy remote-update
  53. # flow reads at runtime via detect_current_branch() in spoolbuddy_ssh.py.
  54. # Without this, the production image has no git metadata at all and would
  55. # always pull `main` on the remote device regardless of which branch
  56. # Bambuddy itself was built from.
  57. COPY .git/HEAD ./.git/HEAD
  58. # Copy built frontend from builder stage
  59. COPY --from=frontend-builder /app/static ./static
  60. # Copy embedded GCode viewer static assets (PrettyGCode + Bambuddy adapter).
  61. # Served by the explicit @app.get("/gcode-viewer/{...}") routes in main.py,
  62. # which resolve files under (static_dir.parent / "gcode_viewer") = /app/gcode_viewer/.
  63. # Without this COPY the routes return a bare 404 at request time and the 3D
  64. # Preview iframe shows {"detail":"Not Found"} (see #1218). The directory is
  65. # vendored third-party JS — the Vite build does NOT stage it into static/,
  66. # the dev server serves it via a configureServer middleware that's dev-only.
  67. COPY gcode_viewer/ ./gcode_viewer/
  68. # Create data directories. Ownership is normalised at startup by the
  69. # entrypoint (chowns to PUID:PGID and drops privileges via gosu before
  70. # exec'ing the app), so we don't need a chmod 777 hack here — that was
  71. # the workaround for the previous compose `user: "1000:1000"` model and
  72. # only worked when the volume's perms happened to survive (named volume
  73. # first-create case; bind-mount-source case bit users in #1211 / #668).
  74. #
  75. # The sentinel file is needed so a freshly-created Docker named volume
  76. # isn't "empty" from Docker's POV. On empty volumes Docker resyncs the
  77. # directory metadata (incl. ownership) from the image on every mount,
  78. # which would mean our entrypoint chown gets reverted on every restart
  79. # and re-fired on every start (slow on multi-GB archive dirs). With a
  80. # sentinel inside the volume on first mount, Docker considers the
  81. # volume populated and stops resyncing, so the chown is genuinely
  82. # one-shot.
  83. RUN mkdir -p /app/data /app/logs && \
  84. : >/app/data/.bambuddy && \
  85. : >/app/logs/.bambuddy
  86. # Entrypoint script: handles PUID/PGID + ownership normalisation +
  87. # privilege drop. See deploy/docker-entrypoint.sh for the full rationale.
  88. COPY deploy/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
  89. RUN chmod +x /usr/local/bin/docker-entrypoint.sh
  90. # Environment variables
  91. ENV PYTHONUNBUFFERED=1
  92. ENV DATA_DIR=/app/data
  93. ENV LOG_DIR=/app/logs
  94. ENV PORT=8000
  95. # Provide a local username + home for tools that call getpass.getuser() /
  96. # os.path.expanduser() under arbitrary PUIDs. With `user: "1001:1001"` the
  97. # stock python:3.13-slim image has no /etc/passwd entry for that UID, so
  98. # pwd.getpwuid() raises and breaks libraries that do host-level user lookups
  99. # (notably asyncssh, which uses the local username for ~/.ssh/config host
  100. # matching during the SpoolBuddy remote-update flow). Setting LOGNAME/USER
  101. # makes getpass.getuser() resolve via env vars instead of the passwd db;
  102. # HOME=/app gives a writable home that is guaranteed to exist.
  103. ENV HOME=/app
  104. ENV USER=bambuddy
  105. ENV LOGNAME=bambuddy
  106. EXPOSE 322
  107. EXPOSE 990
  108. EXPOSE 3000
  109. EXPOSE 3002
  110. EXPOSE 6000
  111. EXPOSE 8000
  112. EXPOSE 8883
  113. EXPOSE 50000-50100
  114. # Health check (uses PORT env var via shell)
  115. HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
  116. CMD python -c "import urllib.request, os; urllib.request.urlopen(f'http://localhost:{os.environ.get(\"PORT\", \"8000\")}/health')" || exit 1
  117. # Run the application
  118. # Use standard asyncio loop (uvloop has permission issues in some Docker environments)
  119. # Port is configurable via PORT environment variable (default: 8000)
  120. ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
  121. CMD ["sh", "-c", "uvicorn backend.app.main:app --host 0.0.0.0 --port ${PORT:-8000} --loop asyncio"]