FROM node:10-alpine AS base
# set working directory
WORKDIR /app

ARG NPM_AUTH
RUN echo "//registry.npmjs.org/:_authToken=$NPM_AUTH" > .npmrc
RUN cat .npmrc
RUN apk add --update --repository http://dl-3.alpinelinux.org/alpine/edge/testing \
            --no-cache --virtual .build-deps python \
            build-base \
            gcc \
            wget \
            git \
            pkgconfig \
            libmount ttf-dejavu ttf-droid ttf-freefont ttf-liberation \
            ttf-ubuntu-font-family fontconfig \
            g++ \
            cairo-dev \
            jpeg-dev \
            pango-dev \
            bash \
            imagemagick

COPY package.json ./
COPY . /app
RUN yarn install \
    && yarn next:build \
    && yarn --production \
    && mv node_modules /tmp/node_modules \
    && mv .next /tmp/next \
    && apk del --purge .build-deps

FROM node:10-alpine
ENV NODE_ENV=production
EXPOSE 8080
ENV PORT=8080
WORKDIR /app
COPY --from=base /tmp/node_modules /app/node_modules
COPY --from=base /tmp/next /app/.next
COPY . /app

CMD yarn start