1
- # syntax=docker/dockerfile-upstream:master-labs
1
+ FROM node:18-alpine AS base
2
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
3
+ # Install dependencies only when needed
13
4
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
14
7
WORKDIR /app
15
8
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
22
17
23
- # Avoid 'cross-device link not permitted' error with pnpm install
24
- RUN echo "package-import-method=copy" > .npmrc
25
18
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
30
20
FROM base AS builder
31
21
WORKDIR /app
22
+ COPY --from=deps /app/node_modules ./node_modules
23
+ COPY . .
32
24
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.
38
28
# ENV NEXT_TELEMETRY_DISABLED 1
39
- ENV NODE_ENV production
40
29
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
45
36
46
- # # RUNNER
37
+ # Production image, copy all the files and run next
47
38
FROM base AS runner
48
39
WORKDIR /app
49
40
50
41
ENV NODE_ENV production
42
+ # Uncomment the following line in case you want to disable telemetry during runtime.
51
43
# ENV NEXT_TELEMETRY_DISABLED 1
52
44
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
55
53
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
59
60
60
- USER node
61
61
EXPOSE 3000
62
+
62
63
ENV PORT 3000
63
64
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
0 commit comments