Skip to content

Commit c538547

Browse files
committed
feat: Update Dockerfile for multi-stage build and adjust Next.js output configuration
1 parent 2d6cb33 commit c538547

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

aws-deploy-guide/Dockerfile

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,51 @@
11
# Use the latest Node.js image
2-
FROM node:latest
2+
FROM node:20-alpine AS base
33

4-
# Set the working directory inside the container
4+
# Install dependencies only when needed
5+
FROM base AS deps
6+
RUN apk add --no-cache libc6-compat
57
WORKDIR /app
68

7-
# Copy package.json and package-lock.json (if available)
8-
COPY package*.json ./
9+
# Install dependencies based on the preferred package manager
10+
COPY package.json package-lock.json* ./
11+
RUN npm ci
912

10-
# Install dependencies
11-
RUN npm install
12-
13-
# Copy the rest of the application code
13+
# Rebuild the source code only when needed
14+
FROM base AS builder
15+
WORKDIR /app
16+
COPY --from=deps /app/node_modules ./node_modules
1417
COPY . .
1518

16-
# Build the Next.js application
19+
# Remove static export for production server
20+
ENV NEXT_TELEMETRY_DISABLED=1
21+
1722
RUN npm run build
1823

19-
# Expose port 3000 to allow access
24+
# Production image, copy all the files and run next
25+
FROM base AS runner
26+
WORKDIR /app
27+
28+
ENV NODE_ENV=production
29+
ENV NEXT_TELEMETRY_DISABLED=1
30+
31+
RUN addgroup --system --gid 1001 nodejs
32+
RUN adduser --system --uid 1001 nextjs
33+
34+
COPY --from=builder /app/public ./public
35+
36+
# Set the correct permission for prerender cache
37+
RUN mkdir .next
38+
RUN chown nextjs:nodejs .next
39+
40+
# Automatically leverage output traces to reduce image size
41+
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
42+
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
43+
44+
USER nextjs
45+
2046
EXPOSE 3000
2147

22-
# Start the application
23-
CMD ["npm", "start"]
48+
ENV PORT=3000
49+
ENV HOSTNAME="0.0.0.0"
50+
51+
CMD ["node", "server.js"]

aws-deploy-guide/next.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { NextConfig } from "next";
33
const nextConfig: NextConfig = {
44
/* config options here */
55
reactCompiler: true,
6-
output: 'export',
6+
output: process.env.GITHUB_ACTIONS ? 'export' : 'standalone',
77
images: {
88
unoptimized: true,
99
},

0 commit comments

Comments
 (0)