1
1
FROM node:18-alpine AS base
2
2
3
- # Step 1. Rebuild the source code only when needed
4
- FROM base AS builder
5
-
3
+ # Install dependencies only when needed
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
6
7
WORKDIR /app
7
8
8
9
# Install dependencies based on the preferred package manager
9
10
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
10
- # Omit --production flag for TypeScript devDependencies
11
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 yarn global add pnpm && pnpm i; \
15
- # Allow install without lockfile, so example works even without Node.js installed locally
16
- else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
17
- fi
18
-
19
- COPY src ./src
20
- COPY public ./public
21
- COPY next.config.js .
22
- COPY tsconfig.json .
23
-
24
- # Environment variables must be present at build time
25
- # https://github.com/vercel/next.js/discussions/14030
26
- ARG ENV_VARIABLE
27
- ENV ENV_VARIABLE=${ENV_VARIABLE}
28
- ARG NEXT_PUBLIC_ENV_VARIABLE
29
- ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
30
-
31
- ENV NEXT_TELEMETRY_DISABLED 1
32
-
33
- # Build Next.js based on the preferred package manager
34
- RUN \
35
- if [ -f yarn.lock ]; then yarn build; \
36
- elif [ -f package-lock.json ]; then npm run build; \
37
- elif [ -f pnpm-lock.yaml ]; then pnpm build; \
38
- else yarn build; \
39
- fi
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
40
17
41
18
42
- # Step 2. Production image, copy all the files and run next
43
- FROM base AS runner
19
+ # Rebuild the source code only when needed
20
+ FROM base AS builder
21
+ WORKDIR /app
22
+ COPY --from=deps /app/node_modules ./node_modules
23
+ COPY . .
24
+
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.
28
+ # ENV NEXT_TELEMETRY_DISABLED 1
44
29
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
36
+
37
+ # Production image, copy all the files and run next
38
+ FROM base AS runner
45
39
WORKDIR /app
46
40
41
+ ENV NODE_ENV production
42
+ # Uncomment the following line in case you want to disable telemetry during runtime.
43
+ # ENV NEXT_TELEMETRY_DISABLED 1
44
+
47
45
RUN addgroup --system --gid 1001 nodejs
48
46
RUN adduser --system --uid 1001 nextjs
49
- USER nextjs
50
47
51
48
COPY --from=builder /app/public ./public
52
49
53
- COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
50
+ # Set the correct permission for prerender cache
51
+ RUN mkdir .next
52
+ RUN chown nextjs:nodejs .next
53
+
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 ./
54
57
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
55
58
56
- # Environment variables must be redefined at run time
57
- ARG ENV_VARIABLE
58
- ENV ENV_VARIABLE=${ENV_VARIABLE}
59
- ARG NEXT_PUBLIC_ENV_VARIABLE
60
- ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
59
+ USER nextjs
61
60
62
- # Uncomment the following line to disable telemetry at run time
63
- ENV NEXT_TELEMETRY_DISABLED 1
61
+ EXPOSE 3000
64
62
63
+ ENV PORT 3000
65
64
66
- CMD ["node", "server.js"]
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