FROM node:12-alpine

# set app user
RUN addgroup --gid 800 appuser && \
    adduser --uid 800 --ingroup appuser --disabled-password --gecos "" appuser && \
    mkdir /app && \
    chown appuser:appuser /app
USER appuser
WORKDIR /app

# add proxy configuration
USER root
COPY ./bio-proxy-configuration-for-docker /tmp/bio-data
ARG add_proxy
RUN if [ -n "$add_proxy" ] ; then sed -i 's/\r$//g' /tmp/bio-data/alpine-based.sh && \
    chmod a+x /tmp/bio-data/alpine-based.sh && \
    /tmp/bio-data/alpine-based.sh && \
    rm -r /tmp/bio-data ; fi
USER appuser

# copy data
COPY . .

# install and test
USER root
RUN . /etc/profile && apk add --virtual .npm-install-virt --no-cache git
USER appuser
RUN npm run install-prod
USER root
RUN apk del .npm-install-virt
USER appuser
ENV NODE_ENV production

# cleanup
USER root
RUN rm -r ./src ./public/js-source ./bio-proxy-configuration-for-docker && \
    npm cache clean --force
USER appuser

# healthcheck
HEALTHCHECK --interval=10s --timeout=2s --start-period=15s \
    CMD node ./out/healthcheck.js

# set command
CMD npm run production
