# Waltermart solar monitoring (kiosk + fleet summary)
# No secrets in the image: .env (solarops secret) and config/config.json (weather key) are supplied at run time.
# Node 24 LTS (supported to April 2028), pinned by version and digest; Dependabot proposes updates (RB-26, RB-28).

# ---- deps: install production packages only, no install scripts (express and ejs need none)
FROM node:24.22.2-alpine@sha256:a34ab6d7f6f4813f4d0c5469d74685604b962fbdbf5793a7bc7b4998ecb867c8 AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev --ignore-scripts

# ---- runtime: no npm, npx, yarn or corepack (not needed to run, and npm's bundled packages carry CVEs)
FROM node:24.22.2-alpine@sha256:a34ab6d7f6f4813f4d0c5469d74685604b962fbdbf5793a7bc7b4998ecb867c8
# tzdata so TZ applies to the process clock; the app itself uses config.timezone
# Exception DL3018 (unpinned apk package): tzdata is time-zone data only and the base image is pinned by digest; review by 2027-05-06
# hadolint ignore=DL3018
RUN apk add --no-cache tzdata \
 && rm -rf /usr/local/lib/node_modules /usr/local/bin/npm /usr/local/bin/npx /usr/local/bin/corepack \
           /usr/local/bin/yarn /usr/local/bin/yarnpkg /opt/yarn-* /root/.npm
ENV NODE_ENV=production \
    TZ=Asia/Manila \
    PORT=3000

WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

USER node
EXPOSE 3000
# /healthz answers without calling solarops, so a slow upstream never marks the container unhealthy
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
  CMD wget -qO- "http://127.0.0.1:${PORT}/healthz" >/dev/null || exit 1
CMD ["node", "server.js"]
