# syntax=docker/dockerfile:1

FROM alpine:latest

# Install npm and nodejs
RUN apk add --no-cache nodejs npm

# Create non-root user
ARG CUSTOM_UID
ARG CUSTOM_GID
ENV CUSTOM_USERNAME=frontend
ENV CUSTOM_GROUPNAME=frontend
RUN addgroup --gid ${CUSTOM_GID:-1000} ${CUSTOM_GROUPNAME} && \
    adduser --uid ${CUSTOM_UID:-1000} --shell /bin/ash ${CUSTOM_USERNAME} --ingroup ${CUSTOM_GROUPNAME} --disabled-password && \
    mkdir /app && chown ${CUSTOM_UID:-1000}:${CUSTOM_GID:-1000} /app && chmod 700 /app

# Copy source files
COPY --chown=${CUSTOM_USERNAME}:${CUSTOM_GROUPNAME} frontend/ /app/

# Install dependencies and build app
USER ${CUSTOM_UID:-1000}:${CUSTOM_GID:-1000}
WORKDIR /app/
RUN npm install
RUN npm run build

# Run node.js
ENV NODE_ENV=production
EXPOSE 3000
ENTRYPOINT ["node", "-r", "dotenv/config", "build"]
