subman-nextjs/Dockerfile

75 lines
1.6 KiB
Docker
Raw Permalink Normal View History

2024-10-01 12:56:25 +00:00
FROM node:20-alpine AS base
### Dependencies ###
FROM base AS deps
RUN apk add --no-cache libc6-compat git
# Setup pnpm environment
2024-10-01 14:34:51 +00:00
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
2024-10-01 12:56:25 +00:00
RUN corepack enable
2024-10-01 14:34:51 +00:00
RUN corepack prepare pnpm@latest --activate
2024-10-01 12:56:25 +00:00
2024-10-01 14:34:51 +00:00
## WORKDIR /app
2024-10-01 12:56:25 +00:00
2024-10-01 14:34:51 +00:00
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --prefer-frozen-lockfile
2024-10-01 12:56:25 +00:00
# Builder
FROM base AS builder
RUN corepack enable
2024-10-01 14:34:51 +00:00
RUN corepack prepare pnpm@latest --activate
2024-10-01 12:56:25 +00:00
COPY prisma prisma
WORKDIR /app
2024-10-01 14:34:51 +00:00
COPY --from=deps /node_modules ./node_modules
2024-10-01 12:56:25 +00:00
COPY . .
2024-10-01 14:34:51 +00:00
RUN pnpm prisma db push
RUN pnpm build
2024-10-01 12:56:25 +00:00
### Production image runner ###
FROM base AS runner
# Set NODE_ENV to production
ENV NODE_ENV production
# Disable Next.js telemetry
# Learn more here: https://nextjs.org/telemetry
ENV NEXT_TELEMETRY_DISABLED 1
# Set correct permissions for nextjs user and don't run as root
RUN addgroup nodejs
RUN adduser -SDH nextjs
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
2024-10-01 14:34:51 +00:00
COPY --from=builder --chown=nextjs:node:js /app/prisma ./prisma
2024-10-01 12:56:25 +00:00
USER nextjs
# Exposed port (for orchestrators and dynamic reverse proxies)
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD [ "wget", "-q0", "http://localhost:3000/health" ]
# Run the nextjs app
CMD ["node", "server.js"]