# syntax=docker/dockerfile:1
# Doghouse app image. Build context is the folder above deploy/ (see docker-compose.yml).
# Build: node 22 compiles everything. Runtime: node:22-alpine with production deps only, compiled
# for musl (sharp, better-sqlite3 and argon2 ship musl binaries; g++ is there as a fallback).
ARG NODE_IMAGE=node:22-alpine

# ---- 1. build: install everything, compile shared + server + web ----
FROM ${NODE_IMAGE} AS build
WORKDIR /src
RUN apk add --no-cache python3 make g++
COPY package.json package-lock.json ./
COPY packages/shared/package.json packages/shared/
COPY apps/server/package.json apps/server/
COPY apps/web/package.json apps/web/
RUN npm ci --no-audit --no-fund
COPY tsconfig.base.json ./
COPY packages/shared packages/shared
COPY apps/server apps/server
COPY apps/web apps/web
RUN npm run build

# ---- 2. deps: production dependencies of the server only (+ the shared workspace link) ----
# npm also installs the glibc variants of sharp/argon2; Alpine only needs the musl ones, so the
# glibc ones are deleted (saves ~19 MB; sharp's wasm fallback stays).
FROM ${NODE_IMAGE} AS deps
WORKDIR /app
RUN apk add --no-cache python3 make g++
COPY package.json package-lock.json ./
COPY packages/shared/package.json packages/shared/
COPY apps/server/package.json apps/server/
COPY apps/web/package.json apps/web/
RUN npm ci --omit=dev --workspace=apps/server --include-workspace-root=false --no-audit --no-fund \
 && mkdir -p apps/server/node_modules \
 && rm -rf node_modules/@img/sharp-linux-* node_modules/@img/sharp-libvips-linux-* \
           node_modules/@node-rs/*-gnu \
 && npm cache clean --force

# ---- 3. runtime ----
FROM ${NODE_IMAGE}
RUN apk add --no-cache su-exec tini
ENV NODE_ENV=production \
    HOST=0.0.0.0 \
    PORT=3000 \
    DATA_DIR=/data \
    STATIC_DIR=/app/public
WORKDIR /app
COPY --from=deps /app/package.json ./package.json
# D22: build.json (written by the packager at the release root) tells the server which build it
# is. The [n] wildcard makes it optional, so a plain git checkout still builds (it shows "dev").
COPY package.json build.jso[n] ./
COPY --from=deps /app/node_modules ./node_modules
COPY --from=deps /app/apps/server/node_modules ./apps/server/node_modules
COPY --from=build /src/packages/shared/package.json ./packages/shared/package.json
COPY --from=build /src/packages/shared/dist ./packages/shared/dist
COPY --from=build /src/apps/server/package.json ./apps/server/package.json
COPY --from=build /src/apps/server/dist ./apps/server/dist
COPY --from=build /src/apps/web/dist ./public
COPY deploy/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod 0755 /usr/local/bin/docker-entrypoint.sh && mkdir -p /data && chown node:node /data
EXPOSE 3000
VOLUME ["/data"]
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD wget -q -O /dev/null http://127.0.0.1:3000/api/health || exit 1
# The entrypoint fixes ownership of the ./data bind mount, then runs the app as the non-root
# "node" user (uid 1000).
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]
CMD ["node", "apps/server/dist/index.js"]
