FROM node:8.9.3-alpine

ARG NODE_ENV=production
ENV NODE_ENV=$NODE_ENV

# Set timezone
RUN ln -f -s /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime

# Set a working directory
WORKDIR /usr/src/app

COPY package.json yarn.lock ./

RUN set -ex; \
  if [ "$NODE_ENV" = "production" ]; then \
  yarn install --no-cache --frozen-lockfile --production; \
  elif [ "$NODE_ENV" = "test" ]; then \
  touch yarn-error.log; \
  mkdir -m 777 logs; \
  yarn install --no-cache --frozen-lockfile; \
  chown -R node:node logs node_modules package.json yarn.lock yarn-error.log; \
  else \
  touch yarn-error.log; \
  mkdir -p -m 777 logs node_modules /home/node/.cache/yarn; \
  chown -R node:node logs node_modules package.json yarn.lock yarn-error.log /home/node/.cache/yarn; \
  fi;

# Run the container under "node" user by default
USER node

CMD [ "node", "test/index.js" ]