FROM node:20-alpine AS alpine
RUN apk add --no-cache libc6-compat
RUN apk update
# Setup pnpm and turbo on the alpine base
FROM alpine as base
RUN npm install pnpm turbo --global
RUN pnpm config set store-dir ~/.pnpm-store
# Prune projects
FROM base AS pruner
ARG PROJECT="@repo/api"
WORKDIR /app
COPY . .
RUN turbo prune --scope=${PROJECT} --docker
# Build the project
FROM base AS builder
ARG PROJECT="@repo/api"
WORKDIR /app
# Copy lockfile and package.json's of isolated subworkspace
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=pruner /app/out/json/ .
# First install the dependencies (as they change less often)
RUN pnpm install --frozen-lockfile
# Copy source code of isolated subworkspace
COPY --from=pruner /app/out/full/ .
# Env vars required to build the project
ARG DATABASE_URL
ARG RESEND_KEY
ARG BUILD_ENV
RUN turbo build:${BUILD_ENV} --filter=${PROJECT}
RUN rm -rf ./node_modules
RUN pnpm install --prod --frozen-lockfile
# Final image
FROM alpine AS runner
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nodejs
USER nodejs
WORKDIR /app
COPY --from=builder --chown=nodejs:nodejs /app .
WORKDIR /app/apps/api
ARG PORT=8080
ENV PORT=${PORT}
CMD ["node","dist/index.js"]