-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathDockerfile
More file actions
90 lines (67 loc) · 2.88 KB
/
Dockerfile
File metadata and controls
90 lines (67 loc) · 2.88 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
89
90
FROM node:lts-alpine AS base
# Install dependencies only when needed
FROM base AS deps
# Install libc6-compat for better compatibility and git for version info
RUN apk add --no-cache libc6-compat git
WORKDIR /app
# Enable corepack early for better caching
RUN corepack enable
# Copy dependency files
COPY package.json pnpm-lock.yaml* prisma.config.ts env.js ./
COPY lib/db/schema.prisma ./lib/db/schema.prisma
# Install pnpm and dependencies with cache mount for faster builds
RUN --mount=type=cache,target=/root/.local/share/pnpm/store \
--mount=type=cache,target=/root/.cache/pnpm \
corepack enable pnpm && pnpm i --frozen-lockfile --prefer-offline
# ---------
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
# Install git for version info
RUN apk add --no-cache git
# Copy node_modules from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy source code
COPY . .
# Set environment variables for build - they are provided at runtime
ENV SKIP_ENV_VALIDATION=true
ENV NODE_ENV=production
ENV NODE_OPTIONS="--max-old-space-size=4096"
# Enable pnpm, generate Prisma client, and build
# Note: prisma generate must run here because the generated client (lib/db/generated/)
# is gitignored and not copied from deps stage (only node_modules is copied)
# and without the client being present, the build will fail.
#
# The client is generated _again_ as part of migrate-and-start.sh in the final image
# to ensure that it inherits the correct runtime environment variables.
RUN corepack enable pnpm && pnpm exec prisma generate && pnpm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
# Set production environment variables
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
# Create user and group in a single layer
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# Copy public assets
COPY --from=builder /app/public ./public
# Create .next directory with correct permissions
RUN mkdir .next && chown nextjs:nodejs .next
# Copy built application with correct permissions
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy runtime scripts, database files, and dependencies needed for tsx
COPY --from=builder --chown=nextjs:nodejs /app/scripts ./scripts
COPY --from=builder --chown=nextjs:nodejs /app/lib/db ./lib/db
COPY --from=builder --chown=nextjs:nodejs /app/prisma.config.ts ./
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nextjs:nodejs /app/package.json ./
COPY --from=builder --chown=nextjs:nodejs /app/env.js ./
COPY --from=builder --chown=nextjs:nodejs /app/tsconfig.json ./
# Switch to non-root user
USER nextjs
EXPOSE 3000
# Use exec form for better signal handling
CMD ["sh", "scripts/migrate-and-start.sh"]