FROM node:20-alpine AS client-build
WORKDIR /app/client
COPY client/package.json ./
RUN npm install
COPY client/ ./
RUN npm run build

FROM node:20-alpine AS server-deps
WORKDIR /app/server
COPY server/package.json ./
RUN npm install --production

FROM nginx:alpine AS runtime
RUN apk add --no-cache nodejs npm
COPY --from=client-build /app/client/dist /usr/share/nginx/html
COPY --from=server-deps /app/server/node_modules /app/server/node_modules
COPY server/index.js /app/server/
COPY nginx/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 8000
CMD ["sh", "-c", "node /app/server/index.js & nginx -g 'daemon off;'"]
