Files
afilmory/Dockerfile

68 lines
1.8 KiB
Docker

# Dockerfile for Next.js app in a pnpm monorepo
# This Dockerfile should be built from the root of the monorepo:
# > docker build -t photo-gallery-ssr -f apps/ssr/dockerfile .
# -----------------
# Base stage
# -----------------
FROM node:20-alpine AS base
WORKDIR /app
RUN corepack enable
# -----------------
# Builder stage
# -----------------
FROM base AS builder
RUN apk update && apk add --no-cache git
# Copy dependency definition files to leverage Docker cache
COPY pnpm-lock.yaml pnpm-workspace.yaml ./
COPY package.json ./
COPY apps/web/package.json ./apps/web/package.json
COPY apps/ssr/package.json ./apps/ssr/package.json
COPY packages/data/package.json ./packages/data/package.json
COPY scripts ./scripts
RUN sh ./scripts/preinstall.sh
RUN ls
# Install all dependencies
RUN pnpm install --frozen-lockfile
COPY config.json ./
COPY builder.config.json ./
# Copy all source code
COPY . .
# Build the app.
# The build script in the ssr package.json handles building the web app first.
RUN pnpm --filter=@photo-gallery/ssr build
# -----------------
# Runner stage
# -----------------
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
# ENV PORT and other configurations are now in the config files
# and passed through environment variables during runtime.
RUN apk add --no-cache curl wget
# Create a non-root user
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY --from=builder --chown=nextjs:nodejs /app/apps/ssr/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/apps/ssr/.next/static /app/apps/ssr/.next/static
COPY --from=builder --chown=nextjs:nodejs /app/apps/ssr/public /app/apps/ssr/public
# The standalone output includes the server.js file.
# The PORT environment variable is automatically used by Next.js.
EXPOSE 3000
CMD ["node", "apps/ssr/server.js"]