64 lines
1.6 KiB
Docker
64 lines
1.6 KiB
Docker
# Stage 1: Builder
|
|
FROM node:20-alpine AS builder
|
|
WORKDIR /app
|
|
|
|
# Clean the workspace
|
|
RUN rm -rf ./*
|
|
|
|
# Arguments for build-time configuration
|
|
ARG NEXT_PUBLIC_BASE_URL
|
|
ARG NEXT_PUBLIC_TARGET
|
|
ARG DIRECTUS_URL
|
|
ARG NPM_TOKEN
|
|
|
|
# Environment variables for Next.js build
|
|
ENV NEXT_PUBLIC_BASE_URL=$NEXT_PUBLIC_BASE_URL
|
|
ENV NEXT_PUBLIC_TARGET=$NEXT_PUBLIC_TARGET
|
|
ENV DIRECTUS_URL=$DIRECTUS_URL
|
|
ENV SKIP_RUNTIME_ENV_VALIDATION=true
|
|
ENV CI=true
|
|
|
|
# Enable pnpm v10
|
|
RUN corepack enable && corepack prepare pnpm@10.3.0 --activate
|
|
|
|
# Copy lockfile and manifest for dependency installation caching
|
|
COPY pnpm-lock.yaml package.json .npmrc* ./
|
|
|
|
# Install dependencies with cache mount
|
|
RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
|
|
--mount=type=secret,id=NPM_TOKEN \
|
|
export NPM_TOKEN=$(cat /run/secrets/NPM_TOKEN 2>/dev/null || echo $NPM_TOKEN) && \
|
|
pnpm install --frozen-lockfile
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Build application
|
|
RUN pnpm build
|
|
|
|
# Stage 2: Runner
|
|
FROM node:20-alpine AS runner
|
|
WORKDIR /app
|
|
|
|
# Install curl for health checks
|
|
RUN apk add --no-cache curl
|
|
|
|
# Create nextjs user and group
|
|
RUN addgroup --system --gid 1001 nodejs && \
|
|
adduser --system --uid 1001 nextjs && \
|
|
chown nextjs:nodejs /app
|
|
|
|
ENV HOSTNAME="0.0.0.0"
|
|
ENV PORT=3000
|
|
ENV NODE_ENV=production
|
|
|
|
# Copy standalone output and static files
|
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
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/.next/cache ./.next/cache
|
|
|
|
USER nextjs
|
|
|
|
CMD ["node", "server.js"]
|