|
| 1 | +# syntax=docker/dockerfile-upstream:master-labs |
| 2 | + |
| 3 | +# Loosely based on Next.js Dockerfile example - https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile |
| 4 | +FROM node:20-slim AS base |
| 5 | + |
| 6 | +# Install package manager |
| 7 | +RUN --mount=type=cache,id=pnpm-auth-store,target=/root/.pnpm-store \ |
| 8 | + npm i --global --no-update-notifier --no-fund pnpm@latest |
| 9 | + |
| 10 | +USER node |
| 11 | + |
| 12 | +## DEPENDENCIES |
| 13 | +FROM base AS deps |
| 14 | +WORKDIR /app |
| 15 | + |
| 16 | +# Copy over package.json and lock files |
| 17 | +# - "--parents" flag required otherwise copying "patches" only copies the contents |
| 18 | +# of that directory, and not the directory itself. This requires the syntax |
| 19 | +# directive at the top of the file. |
| 20 | +COPY --parents --chown=node:node pnpm-lock.yaml pnpm-workspace.yaml patches ./ |
| 21 | +COPY --chown=node:node apps/examples/nextjs-docker/package.json ./package.json |
| 22 | + |
| 23 | +# Avoid 'cross-device link not permitted' error with pnpm install |
| 24 | +RUN echo "package-import-method=copy" > .npmrc |
| 25 | + |
| 26 | +# Install dependencies |
| 27 | +RUN --mount=type=cache,id=pnpm-auth-store,target=/root/.pnpm-store pnpm install |
| 28 | + |
| 29 | +## BUILDER |
| 30 | +FROM base AS builder |
| 31 | +WORKDIR /app |
| 32 | + |
| 33 | +# Copy code from the examples app |
| 34 | +COPY --chown=node:node apps/examples/nextjs-docker ./ |
| 35 | +# Copy dependencies from deps stage |
| 36 | +COPY --from=deps /app ./ |
| 37 | + |
| 38 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 39 | +ENV NODE_ENV production |
| 40 | + |
| 41 | +# Build the "standalone" Next.js version |
| 42 | +RUN pnpm build |
| 43 | +# Remove node_modules |
| 44 | +RUN rm -rf node_modules |
| 45 | + |
| 46 | +## RUNNER |
| 47 | +FROM base AS runner |
| 48 | +WORKDIR /app |
| 49 | + |
| 50 | +ENV NODE_ENV production |
| 51 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 52 | + |
| 53 | +# Copy everything from builder stage |
| 54 | +COPY --chown=node:node --from=builder /app ./ |
| 55 | + |
| 56 | +# Install only prod dependencies |
| 57 | +RUN --mount=type=cache,id=pnpm-auth-store,target=/root/.pnpm-store pnpm install --prod |
| 58 | +RUN --mount=type=cache,id=pnpm-auth-store,target=/root/.pnpm-store pnpm install -w sharp |
| 59 | + |
| 60 | +USER node |
| 61 | +EXPOSE 3000 |
| 62 | +ENV PORT 3000 |
| 63 | + |
| 64 | +# Run the built project |
| 65 | +CMD ["pnpm", "exec", "next", "start"] |
0 commit comments