1
+ FROM node:18-alpine AS base
2
+
3
+ # Step 1. Rebuild the source code only when needed
4
+ FROM base AS builder
5
+
6
+ WORKDIR /app
7
+
8
+ # Install dependencies based on the preferred package manager
9
+ COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
10
+ # Omit --production flag for TypeScript devDependencies
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
40
+
41
+
42
+ # Step 2. Production image, copy all the files and run next
43
+ FROM base AS runner
44
+
45
+ WORKDIR /app
46
+
47
+ RUN addgroup --system --gid 1001 nodejs
48
+ RUN adduser --system --uid 1001 nextjs
49
+ USER nextjs
50
+
51
+ COPY --from=builder /app/public ./public
52
+
53
+ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
54
+ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
55
+
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}
61
+
62
+ # Uncomment the following line to disable telemetry at run time
63
+ ENV NEXT_TELEMETRY_DISABLED 1
64
+
65
+
66
+ CMD ["node", "server.js"]
0 commit comments