1
+ FROM node: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 corepack enable 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 app ./app
20
+ COPY components ./components
21
+ COPY config ./config
22
+ COPY const ./const
23
+ COPY lib ./lib
24
+ COPY pages ./pages
25
+ COPY public ./public
26
+ COPY styles ./styles
27
+ COPY jsconfig.json .
28
+ COPY postcss.config.js .
29
+ COPY tailwind.config.js .
30
+
31
+
32
+ # Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry
33
+ # Uncomment the following line to disable telemetry at build time
34
+ ENV NEXT_TELEMETRY_DISABLED 1
35
+
36
+ # Build Next.js based on the preferred package manager
37
+ RUN \
38
+ if [ -f yarn.lock ]; then yarn build; \
39
+ elif [ -f package-lock.json ]; then npm run build; \
40
+ elif [ -f pnpm-lock.yaml ]; then pnpm build; \
41
+ else npm run build; \
42
+ fi
43
+
44
+ # Note: It is not necessary to add an intermediate step that does a full copy of `node_modules` here
45
+
46
+ # Step 2. Production image, copy all the files and run next
47
+ FROM base AS runner
48
+
49
+ WORKDIR /app
50
+
51
+ # Don't run production as root
52
+ RUN addgroup --system --gid 1001 nodejs
53
+ RUN adduser --system --uid 1001 nextjs
54
+ USER nextjs
55
+
56
+ COPY --from=builder /app/public ./public
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
+ # Uncomment the following line to disable telemetry at run time
64
+ # ENV NEXT_TELEMETRY_DISABLED 1
65
+
66
+ # Note: Don't expose ports here, Compose will handle that for us
67
+
68
+ CMD ["node", "server.js"]
0 commit comments