Skip to content

Commit e6040fa

Browse files
committed
🔄 Synced local '.' with remote 'apps/examples/nextjs'
1 parent b51ff0e commit e6040fa

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

Dockerfile

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
1-
# syntax=docker/dockerfile-upstream:master-labs
1+
FROM node:18-alpine AS base
22

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
3+
# Install dependencies only when needed
134
FROM base AS deps
5+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
6+
RUN apk add --no-cache libc6-compat
147
WORKDIR /app
158

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
9+
# Install dependencies based on the preferred package manager
10+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
11+
RUN \
12+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
13+
elif [ -f package-lock.json ]; then npm ci; \
14+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
15+
else echo "Lockfile not found." && exit 1; \
16+
fi
2217

23-
# Avoid 'cross-device link not permitted' error with pnpm install
24-
RUN echo "package-import-method=copy" > .npmrc
2518

26-
# Install dependencies
27-
RUN --mount=type=cache,id=pnpm-auth-store,target=/root/.pnpm-store pnpm install
28-
29-
## BUILDER
19+
# Rebuild the source code only when needed
3020
FROM base AS builder
3121
WORKDIR /app
22+
COPY --from=deps /app/node_modules ./node_modules
23+
COPY . .
3224

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-
25+
# Next.js collects completely anonymous telemetry data about general usage.
26+
# Learn more here: https://nextjs.org/telemetry
27+
# Uncomment the following line in case you want to disable telemetry during the build.
3828
# ENV NEXT_TELEMETRY_DISABLED 1
39-
ENV NODE_ENV production
4029

41-
# Build the "standalone" Next.js version
42-
RUN pnpm build
43-
# Remove node_modules
44-
RUN rm -rf node_modules
30+
RUN \
31+
if [ -f yarn.lock ]; then yarn run build; \
32+
elif [ -f package-lock.json ]; then npm run build; \
33+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
34+
else echo "Lockfile not found." && exit 1; \
35+
fi
4536

46-
## RUNNER
37+
# Production image, copy all the files and run next
4738
FROM base AS runner
4839
WORKDIR /app
4940

5041
ENV NODE_ENV production
42+
# Uncomment the following line in case you want to disable telemetry during runtime.
5143
# ENV NEXT_TELEMETRY_DISABLED 1
5244

53-
# Copy everything from builder stage
54-
COPY --chown=node:node --from=builder /app ./
45+
RUN addgroup --system --gid 1001 nodejs
46+
RUN adduser --system --uid 1001 nextjs
47+
48+
COPY --from=builder /app/public ./public
49+
50+
# Set the correct permission for prerender cache
51+
RUN mkdir .next
52+
RUN chown nextjs:nodejs .next
5553

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
54+
# Automatically leverage output traces to reduce image size
55+
# https://nextjs.org/docs/advanced-features/output-file-tracing
56+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
57+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
58+
59+
USER nextjs
5960

60-
USER node
6161
EXPOSE 3000
62+
6263
ENV PORT 3000
6364

64-
# Run the built project
65-
CMD ["pnpm", "exec", "next", "start"]
65+
# server.js is created by next build from the standalone output
66+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
67+
CMD HOSTNAME="0.0.0.0" node server.js

next.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/** @type {import("next").NextConfig} */
2-
module.exports = {}
2+
module.exports = {
3+
output: "standalone",
4+
}

0 commit comments

Comments
 (0)