# 13.06 Orders service image. Small base, production dependencies only, non-root user,
# config from environment variables, logs on stdout, a liveness HEALTHCHECK.
FROM node:24-alpine

ENV NODE_ENV=production
WORKDIR /app

# Dependencies first, so this layer is cached until package*.json changes.
COPY package.json package-lock.json ./
RUN npm ci --omit=dev && npm cache clean --force

COPY service.mjs ./

# Inside a container the service listens on all interfaces of the container's own network.
ENV HOST=0.0.0.0 \
    PORT=8080 \
    DRAIN_DELAY_MS=2000 \
    SHUTDOWN_TIMEOUT_MS=20000
EXPOSE 8080

# The official node image ships a "node" user; never run the service as root.
USER node

# Liveness only: is the process answering? Readiness is for the proxy or orchestrator.
HEALTHCHECK --interval=10s --timeout=2s --start-period=5s --retries=3 \
  CMD ["node", "-e", "fetch('http://127.0.0.1:8080/livez').then(r=>process.exit(r.ok?0:1),()=>process.exit(1))"]

# docker stop sends SIGTERM; the service drains. Keep docker stop's timeout above
# DRAIN_DELAY_MS + SHUTDOWN_TIMEOUT_MS, e.g. docker stop -t 25.
STOPSIGNAL SIGTERM
CMD ["node", "service.mjs"]
