FROM node:20-alpine AS builder

RUN mkdir -p /data && chown node:node /data

WORKDIR /app

COPY package.json .
RUN npm install

COPY . .

RUN npm run build

FROM node:20-alpine

WORKDIR /app

RUN addgroup -S appgroup && adduser -S appuser -G appgroup

COPY package.json .
RUN npm install --omit=dev

RUN mkdir -p /data
RUN touch /data/portal-btc.db

COPY --from=builder /app/build .

VOLUME /data
RUN chown -R appuser:appgroup /app /data

USER appuser

EXPOSE 3000

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