FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:1-alpine
RUN addgroup -g 1001 -S appgroup && adduser -u 1001 -S appuser -G appgroup
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist /usr/share/nginx/html
RUN chown -R 1001:1001 /usr/share/nginx/html /var/cache/nginx /var/log/nginx /var/run /run && \
    sed -i 's/^user  nginx;/# user nginx;/' /etc/nginx/nginx.conf && \
    sed -i 's|pid.*nginx\.pid;|pid /tmp/nginx.pid;|' /etc/nginx/nginx.conf
EXPOSE 8080
USER 1001
CMD ["nginx", "-g", "daemon off;"]
