-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdockerfile
52 lines (39 loc) · 1.36 KB
/
dockerfile
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
# Stage 1: Dependencies
FROM node:18-alpine AS deps
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
# Make sure sharp is in your package.json if you need image optimization
RUN pnpm install --frozen-lockfile
# Stage 2: Builder
FROM node:18-alpine AS builder
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Build the Next.js application (standalone mode)
RUN pnpm build
# Stage 3: Runner
FROM node:18-alpine AS runner
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
ENV NODE_ENV=production
# Create a non-root user and group
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# Copy only the necessary files from the builder stage
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# -- Create and set permissions for the cache directory --
# Although standalone mode might not always strictly need it,
# Next.js at runtime may still try to access .next/cache
RUN mkdir -p .next/cache && chown -R nextjs:nodejs .next
# Switch to the non-root user
USER nextjs
# Expose the port
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Command to start the application
CMD ["node", "server.js"]