-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
96 lines (72 loc) · 4.07 KB
/
Copy pathDockerfile
File metadata and controls
96 lines (72 loc) · 4.07 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
91
92
93
94
95
96
# Stage 1: Dependencies
FROM node:20-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci
# Stage 2: Builder
FROM node:20-alpine AS builder
ENV NEXT_TELEMETRY_DISABLED=1
RUN apk add --no-cache openssl bash
WORKDIR /app
# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
# Copy application files
COPY . .
# Ensure public exists (repo may not have it or it may be empty)
RUN mkdir -p /app/public
# Generate Prisma Client
RUN npx prisma generate
# Set dummy DATABASE_URL for build (prevents Prisma from trying to connect during build)
ENV DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy"
# Default: webpack build (see scripts/build-safe.sh). Turbopack chunk names (~, turbopack-*) often break
# OpenResty/nginx when a regex location steals /_next requests. Override with NEXT_USE_TURBOPACK=1 only
# if your edge config is safe for those URLs.
ARG NEXT_USE_TURBOPACK=0
ENV NEXT_USE_TURBOPACK=${NEXT_USE_TURBOPACK}
# Webpack production builds can exceed Node's default old-space limit (~2 GiB on 64-bit).
# Default 8192 aligns with scripts/build-safe.sh. Override: docker build --build-arg NODE_MAX_OLD_SPACE_SIZE=12288 .
ARG NODE_MAX_OLD_SPACE_SIZE=8192
ENV NODE_OPTIONS="--max-old-space-size=${NODE_MAX_OLD_SPACE_SIZE}"
# Build Next.js application
RUN npm run build
# Stage 3: Runner
FROM node:20-alpine AS runner
RUN apk add --no-cache bash openssl
WORKDIR /app
# OCI labels help operators and registries identify images without running them.
LABEL org.opencontainers.image.title="personal-website" \
org.opencontainers.image.description="Next.js App Router site and CMS (standalone)" \
org.opencontainers.image.source="https://github.com/Benedict-CS/personal-website"
ENV NODE_ENV=production
# Optional: set at build time (e.g. git SHA) — surfaced in GET /api/health as `appVersion`.
ARG APP_VERSION=dev
ENV APP_VERSION=${APP_VERSION}
RUN addgroup -g 1001 -S nodejs && adduser -S -u 1001 -G nodejs nextjs
# Copy standalone output from builder
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
# public: create empty dir here; real assets come from volume mount (./public) at runtime
RUN mkdir -p ./public
# Copy package.json so Prisma CLI can find it (requires from .bin resolve to node_modules/package.json and project root)
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/package.json ./node_modules/package.json
# Copy Prisma files (schema and migrations)
COPY --from=builder /app/prisma ./prisma
# Copy Prisma Client and CLI (needed for runtime and migrations)
# The prisma CLI wrapper looks for prisma_schema_build_bg.wasm in .bin/ so we copy it there too
RUN mkdir -p ./node_modules/.prisma ./node_modules/@prisma ./node_modules/prisma ./node_modules/.bin
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.prisma ./node_modules/.prisma
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/@prisma ./node_modules/@prisma
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/prisma ./node_modules/prisma
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.bin/prisma ./node_modules/.bin/prisma
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/prisma/build/prisma_schema_build_bg.wasm ./node_modules/.bin/prisma_schema_build_bg.wasm
# Switch to non-root user
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Used by `docker run` without Compose; Compose overrides with a DB-aware check.
HEALTHCHECK --interval=30s --timeout=10s --start-period=50s --retries=3 \
CMD node -e "require('http').get('http://127.0.0.1:3000/api/health',(r)=>process.exit(r.statusCode===200?0:1)).on('error',()=>process.exit(1))"
# Run migrations on startup with retries, then start the app.
CMD ["/bin/sh", "-c", "attempt=1; until [ \"$attempt\" -gt 30 ]; do npx prisma migrate deploy && break; echo \"Prisma migrate deploy failed (attempt $attempt/30). Retrying in 2s...\"; attempt=$((attempt + 1)); sleep 2; done; if [ \"$attempt\" -gt 30 ]; then echo \"Prisma migrate deploy failed after 30 attempts.\"; exit 1; fi; exec node server.js"]