# Install dependencies of project
FROM node:fermium-alpine AS dependencies
RUN apk add --no-cache libc6-compat
WORKDIR /home/app/
COPY package.json ./ 
RUN npm i


# Build application to bunch of static files
FROM node:fermium-alpine AS builder
WORKDIR /home/app/
COPY --from=dependencies ./home/app/node_modules ./node_modules
COPY . .
RUN npm run build


# NGINX image
FROM nginx:1.21.6 as production
# Copy built assets from builder
COPY --from=builder /home/app/build /usr/share/nginx/html
# Add nginx.config
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy setup nginx entrypoint file
COPY ./scripts/entrypoint.sh .

# Expose ports
EXPOSE 80

# Execute script
RUN chmod +x ./entrypoint.sh

ENV NODE_ENV production
# ENV USER_NAME=node_user USER_UID=2000 GROUP_NAME=node_group GROUP_UID=2000

# RUN deluser --remove-home node \
#     && addgroup --g ${GROUP_UID} -S ${GROUP_NAME} \ 
#     && adduser -D -S -s /sbin/nologin -u ${USER_UID} -G ${GROUP_NAME} ${USER_NAME}
# USER "${USER_NAME}"

ENTRYPOINT ["./entrypoint.sh"]

# Start serving
CMD ["nginx", "-g", "daemon off;"]