-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
89 lines (68 loc) · 2.46 KB
/
Dockerfile
File metadata and controls
89 lines (68 loc) · 2.46 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# ============================================
# Stage 1: Dependencies
# ============================================
FROM node:20-alpine AS deps
# Install security updates and required packages
RUN apk update && apk upgrade && \
apk add --no-cache libc6-compat && \
rm -rf /var/cache/apk/*
# Install pnpm globally
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
# Copy package files for dependency installation
COPY package.json pnpm-lock.yaml* ./
# Install dependencies with frozen lockfile
RUN pnpm install --frozen-lockfile --prod=false
# ============================================
# Stage 2: Builder
# ============================================
FROM node:20-alpine AS builder
# Install security updates
RUN apk update && apk upgrade && \
rm -rf /var/cache/apk/*
# Install pnpm globally
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Set build-time environment variables
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
# Build the application
RUN pnpm build
# ============================================
# Stage 3: Runner (Production)
# ============================================
FROM node:20-alpine AS runner
# Install security updates and required runtime dependencies
RUN apk update && apk upgrade && \
apk add --no-cache \
tzdata \
&& rm -rf /var/cache/apk/*
# Create non-root user for security
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
WORKDIR /app
# Set production environment
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Copy necessary files from builder
# Next.js standalone output includes everything needed in .next/standalone
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
# Switch to non-root user
USER nextjs
# Expose port
EXPOSE 3000
# Set timezone
ENV TZ=UTC
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Health check - uses dedicated health endpoint
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})" || exit 1
# Start the application
# Next.js standalone creates server.js in the root of standalone output
CMD ["node", "server.js"]