-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathDockerfile
More file actions
83 lines (60 loc) · 2.1 KB
/
Dockerfile
File metadata and controls
83 lines (60 loc) · 2.1 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
FROM node:22-alpine AS base
# Install dependencies only when needed
FROM base AS dependencies
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat && apk upgrade
WORKDIR /app
# Copy package files
COPY package.json pnpm-lock.yaml ./
# Enable corepack and install dependencies
RUN corepack enable && \
pnpm install --frozen-lockfile --production=false
FROM base AS builder
WORKDIR /app
# Enable corepack for pnpm
RUN corepack enable
# Copy installed dependencies
COPY --from=dependencies /app/node_modules ./node_modules
# Copy source code
COPY . .
# Set build environment
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Build args for build-time environment variables
ARG CONTACT_WEBHOOK_URL=""
ARG NEXT_PUBLIC_POSTHOG_KEY=""
ENV CONTACT_WEBHOOK_URL=$CONTACT_WEBHOOK_URL
ENV NEXT_PUBLIC_POSTHOG_KEY=$NEXT_PUBLIC_POSTHOG_KEY
# Build the application
RUN pnpm build
# Production runtime stage
FROM base AS runner
WORKDIR /app
# Set production environment
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# Runtime environment variables (will be passed from build args)
ARG CONTACT_WEBHOOK_URL=""
ARG NEXT_PUBLIC_POSTHOG_KEY=""
ENV CONTACT_WEBHOOK_URL=$CONTACT_WEBHOOK_URL
ENV NEXT_PUBLIC_POSTHOG_KEY=$NEXT_PUBLIC_POSTHOG_KEY
# Install curl for health checks and create system group and user for security
RUN apk add --no-cache curl && \
addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Copy public files and build output
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Switch to non-root user
USER nextjs
# Expose port
EXPOSE 3000
# Set runtime configuration
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Health check (checking if the app responds)
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:3000 || exit 1
# Start the application
CMD ["node", "server.js"]