Skip to content

Commit 5613a7e

Browse files
authored
templates: update Dockerfile for the website template, use the LTS version for Node.js image (#10184)
Fixes #10153 With the current Node.js version - `18.8.0` Dockerfile in templates doesn't build <img width="498" alt="image" src="https://github.com/user-attachments/assets/91229bf2-a760-4f37-913e-6cca3e6806d0" /> Updated to LTS - `22.12.0`. Also updated Dockerfile in the `website` template as it was outdated and added comment that you need to set `output: 'standalone'` in `next.config.mjs`. <img width="791" alt="image" src="https://github.com/user-attachments/assets/be78d2cc-2489-4a7f-8fa2-8e88fcce4d8f" />
1 parent 5d3b816 commit 5613a7e

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

templates/blank/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.mjs file.
12
# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
23

3-
FROM node:18-alpine AS base
4+
FROM node:22.12.0-alpine AS base
45

56
# Install dependencies only when needed
67
FROM base AS deps

templates/website/Dockerfile

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,71 @@
1-
FROM node:18.8-alpine as base
1+
# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.js file.
2+
# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
23

3-
FROM base as builder
4+
FROM node:22.12.0-alpine AS base
45

5-
WORKDIR /home/node/app
6-
COPY package*.json ./
6+
# Install dependencies only when needed
7+
FROM base AS deps
8+
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
9+
RUN apk add --no-cache libc6-compat
10+
WORKDIR /app
711

12+
# Install dependencies based on the preferred package manager
13+
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
14+
RUN \
15+
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
16+
elif [ -f package-lock.json ]; then npm ci; \
17+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
18+
else echo "Lockfile not found." && exit 1; \
19+
fi
20+
21+
22+
# Rebuild the source code only when needed
23+
FROM base AS builder
24+
WORKDIR /app
25+
COPY --from=deps /app/node_modules ./node_modules
826
COPY . .
9-
RUN yarn install
10-
RUN yarn build
1127

12-
FROM base as runtime
28+
# Next.js collects completely anonymous telemetry data about general usage.
29+
# Learn more here: https://nextjs.org/telemetry
30+
# Uncomment the following line in case you want to disable telemetry during the build.
31+
# ENV NEXT_TELEMETRY_DISABLED 1
32+
33+
RUN \
34+
if [ -f yarn.lock ]; then yarn run build; \
35+
elif [ -f package-lock.json ]; then npm run build; \
36+
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
37+
else echo "Lockfile not found." && exit 1; \
38+
fi
39+
40+
# Production image, copy all the files and run next
41+
FROM base AS runner
42+
WORKDIR /app
1343

14-
ENV NODE_ENV=production
44+
ENV NODE_ENV production
45+
# Uncomment the following line in case you want to disable telemetry during runtime.
46+
# ENV NEXT_TELEMETRY_DISABLED 1
1547

16-
WORKDIR /home/node/app
17-
COPY package*.json ./
18-
COPY yarn.lock ./
48+
RUN addgroup --system --gid 1001 nodejs
49+
RUN adduser --system --uid 1001 nextjs
1950

20-
RUN yarn install --production
51+
# Remove this line if you do not have this folder
52+
COPY --from=builder /app/public ./public
53+
54+
# Set the correct permission for prerender cache
55+
RUN mkdir .next
56+
RUN chown nextjs:nodejs .next
57+
58+
# Automatically leverage output traces to reduce image size
59+
# https://nextjs.org/docs/advanced-features/output-file-tracing
60+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
61+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
62+
63+
USER nextjs
2164

2265
EXPOSE 3000
2366

24-
CMD ["node", "dist/server.js"]
67+
ENV PORT 3000
68+
69+
# server.js is created by next build from the standalone output
70+
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
71+
CMD HOSTNAME="0.0.0.0" node server.js

0 commit comments

Comments
 (0)