FROM node:24 AS admin
WORKDIR /src/admin
COPY admin/package.json admin/package-lock.json ./
RUN npm ci
COPY admin/ ./
# The build writes into ../internal/adminui/dist
RUN mkdir -p /src/internal/adminui/dist && npm run build

FROM golang:1.27 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
COPY --from=admin /src/internal/adminui/dist ./internal/adminui/dist
RUN CGO_ENABLED=0 go build -o /parsimony ./cmd/parsimony && mkdir /data

# Run as the unprivileged "nonroot" user (uid 65532). /data is the only writable path:
# the database and its WAL files live there. A new named volume inherits this ownership;
# for a bind mount, make the host directory writable by uid 65532.
FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /parsimony /parsimony
COPY --from=build --chown=65532:65532 /data /data
VOLUME /data
EXPOSE 2424 8080 8081
USER 65532:65532
# Mount the config read-only at /etc/parsimony/parsimony.yaml; keep "db: /data/parsimony.db" in it.
ENTRYPOINT ["/parsimony", "serve", "-config", "/etc/parsimony/parsimony.yaml"]
