Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
59 lines (42 loc) · 1.73 KB
/
Copy pathDockerfile
File metadata and controls
59 lines (42 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Use node:alpine as it's the smallest image for Node.js
FROM node:24-alpine AS base
# Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build
# To optimize Docker layer caching, we move the heavy node_modules directory
# out of the standalone output. This allows us to copy it as a separate layer
# in the runner stage, preventing Docker from repulling a 50MB+ layer every time
# your application code changes.
RUN mv .next/standalone/node_modules .next/standalone_node_modules
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy the standalone node_modules as a distinct layer.
# This layer rarely changes and will remain cached unless you install new dependencies.
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone_node_modules ./node_modules
# Copy the rest of the standalone output (application code and server.js).
# Because node_modules was moved, this layer is now extremely lightweight (< 5MB)
# and is the only layer that will need to be repulled when you change your code.
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# Copy static assets (also lightweight)
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT=3000
# set hostname to localhost
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]