# Two images from one context: the session server, and the static site.
#
# **`web` is last on purpose.** A build with no `--target` gets the final
# stage, and a generated docker-compose.yml written before multiplayer existed
# says plain `build: .` — so making `session` last silently turned the web
# service into a Node process that serves nothing on port 80, failed its
# healthcheck, and got rolled back by an automated deploy. The default has to
# stay the thing everyone was already building.

FROM docker.io/library/node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM docker.io/library/node:22-alpine AS session
WORKDIR /app
# Runtime dependencies and the shared TypeScript source (Node strips types
# on import; it only loads modules reachable from the server entry point).
COPY package.json package-lock.json ./
RUN npm ci --omit=dev --ignore-scripts && npm cache clean --force
COPY server ./server
# Blueprint migrations also use part shapes and atmosphere boundaries. Keep
# their transitive imports available as this shared model evolves.
COPY src ./src
# Fail the image build, rather than server startup, if shared imports break.
RUN node --input-type=module -e "await import('./src/blueprint/schema.ts'); await import('./src/net/apply-command.ts')"
# The volume is mounted here. Created in the image and owned by `node` so a
# fresh named volume inherits that ownership — otherwise the server runs
# unprivileged against a root-owned directory and silently saves nothing.
RUN mkdir -p /data && chown node:node /data
ENV PORT=8081 SEMIBOX_DATA=/data
VOLUME ["/data"]
EXPOSE 8081
USER node
CMD ["node", "server/index.js"]

FROM docker.io/library/nginx:stable-alpine AS web
COPY deploy/nginx.conf /etc/nginx/conf.d/default.conf
COPY deploy/20-resolver.sh /docker-entrypoint.d/20-resolver.sh
RUN chmod +x /docker-entrypoint.d/20-resolver.sh
COPY --from=build /app/dist /usr/share/nginx/html
EXPOSE 80
