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
12
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
13
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; \
14
+ elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
15
+ else echo "Lockfile not found." && exit 1; \
17
16
fi
18
17
19
- COPY src ./src
20
- COPY public ./public
21
- COPY next.config.js \
22
- postcss.config.js \
23
- tailwind.config.ts \
24
- tsconfig.json \
25
- ./
26
-
27
- # Environment variables must be present at build time
28
- # https://github.com/vercel/next.js/discussions/14030
29
- ARG ENV_VARIABLE
30
- ENV ENV_VARIABLE=${ENV_VARIABLE}
31
- ARG NEXT_PUBLIC_ENV_VARIABLE
32
- ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
33
-
34
- # Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry
35
- # Uncomment the following line to disable telemetry at build time
36
- ENV NEXT_TELEMETRY_DISABLED 1
37
-
38
- # Build Next.js based on the preferred package manager
39
- RUN \
40
- if [ -f yarn.lock ]; then yarn build; \
41
- elif [ -f package-lock.json ]; then npm run build; \
42
- elif [ -f pnpm-lock.yaml ]; then pnpm build; \
43
- else yarn build; \
44
- fi
45
18
46
- # Note: It is not necessary to add an intermediate step that does a full copy of `node_modules` here
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 . .
47
24
48
- # Step 2. Production image, copy all the files and run next
49
- FROM base AS runner
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
29
+
30
+ RUN yarn build
50
31
32
+ # If using npm comment out above and use below instead
33
+ # RUN npm run build
34
+
35
+ # Production image, copy all the files and run next
36
+ FROM base AS runner
51
37
WORKDIR /app
52
38
53
- # Don't run production as root
39
+ ENV NODE_ENV production
40
+ # Uncomment the following line in case you want to disable telemetry during runtime.
41
+ # ENV NEXT_TELEMETRY_DISABLED 1
42
+
54
43
RUN addgroup --system --gid 1001 nodejs
55
44
RUN adduser --system --uid 1001 nextjs
56
- USER nextjs
57
45
58
46
COPY --from=builder /app/public ./public
59
47
48
+ # Set the correct permission for prerender cache
49
+ RUN mkdir .next
50
+ RUN chown nextjs:nodejs .next
51
+
60
52
# Automatically leverage output traces to reduce image size
61
53
# https://nextjs.org/docs/advanced-features/output-file-tracing
62
54
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
63
55
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
64
56
65
- # Environment variables must be redefined at run time
66
- ARG ENV_VARIABLE
67
- ENV ENV_VARIABLE=${ENV_VARIABLE}
68
- ARG NEXT_PUBLIC_ENV_VARIABLE
69
- ENV NEXT_PUBLIC_ENV_VARIABLE=${NEXT_PUBLIC_ENV_VARIABLE}
57
+ USER nextjs
70
58
71
- # Uncomment the following line to disable telemetry at run time
72
- # ENV NEXT_TELEMETRY_DISABLED 1
59
+ EXPOSE 3000
73
60
74
- # Note: Don't expose ports here, Compose will handle that for us
61
+ ENV PORT 3000
62
+ # set hostname to localhost
63
+ ENV HOSTNAME "0.0.0.0"
75
64
65
+ # server.js is created by next build from the standalone output
66
+ # https://nextjs.org/docs/pages/api-reference/next-config-js/output
76
67
CMD ["node" , "server.js" ]
0 commit comments