Dockerfile 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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
  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. iproute2 \
  20. libcap2-bin \
  21. openssh-client \
  22. && rm -rf /var/lib/apt/lists/*
  23. # Allow binding to privileged ports (e.g. 990/FTPS) as non-root user.
  24. # File capabilities are more reliable than Docker cap_add with user: directive,
  25. # which depends on ambient capability support in the container runtime.
  26. RUN setcap cap_net_bind_service=+ep "$(readlink -f /usr/local/bin/python3)"
  27. # Install Python dependencies with cache mount
  28. COPY requirements.txt ./
  29. RUN --mount=type=cache,target=/root/.cache/pip \
  30. pip install --root-user-action=ignore -r requirements.txt
  31. # Copy backend
  32. COPY backend/ ./backend/
  33. # Capture the current git branch at build time. `.git/HEAD` is the only
  34. # .git metadata the build context lets through (see .dockerignore); it
  35. # contains `ref: refs/heads/<branch>`, which the SpoolBuddy remote-update
  36. # flow reads at runtime via detect_current_branch() in spoolbuddy_ssh.py.
  37. # Without this, the production image has no git metadata at all and would
  38. # always pull `main` on the remote device regardless of which branch
  39. # Bambuddy itself was built from.
  40. COPY .git/HEAD ./.git/HEAD
  41. # Copy built frontend from builder stage
  42. COPY --from=frontend-builder /app/static ./static
  43. # Create data directory for persistent storage
  44. # chmod 777 allows running as non-root user (e.g., with docker compose user: directive)
  45. RUN mkdir -p /app/data /app/logs && chmod 777 /app/data /app/logs
  46. # Environment variables
  47. ENV PYTHONUNBUFFERED=1
  48. ENV DATA_DIR=/app/data
  49. ENV LOG_DIR=/app/logs
  50. ENV PORT=8000
  51. # Provide a local username + home for tools that call getpass.getuser() /
  52. # os.path.expanduser() under arbitrary PUIDs. With `user: "1001:1001"` the
  53. # stock python:3.13-slim image has no /etc/passwd entry for that UID, so
  54. # pwd.getpwuid() raises and breaks libraries that do host-level user lookups
  55. # (notably asyncssh, which uses the local username for ~/.ssh/config host
  56. # matching during the SpoolBuddy remote-update flow). Setting LOGNAME/USER
  57. # makes getpass.getuser() resolve via env vars instead of the passwd db;
  58. # HOME=/app gives a writable home that is guaranteed to exist.
  59. ENV HOME=/app
  60. ENV USER=bambuddy
  61. ENV LOGNAME=bambuddy
  62. EXPOSE 322
  63. EXPOSE 990
  64. EXPOSE 3000
  65. EXPOSE 3002
  66. EXPOSE 6000
  67. EXPOSE 8000
  68. EXPOSE 8883
  69. EXPOSE 50000-50100
  70. # Health check (uses PORT env var via shell)
  71. HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
  72. CMD python -c "import urllib.request, os; urllib.request.urlopen(f'http://localhost:{os.environ.get(\"PORT\", \"8000\")}/health')" || exit 1
  73. # Run the application
  74. # Use standard asyncio loop (uvloop has permission issues in some Docker environments)
  75. # Port is configurable via PORT environment variable (default: 8000)
  76. CMD ["sh", "-c", "uvicorn backend.app.main:app --host 0.0.0.0 --port ${PORT:-8000} --loop asyncio"]