Repository navigation
Expand file tree
/
Copy pathDockerfile
More file actions
60 lines (42 loc) · 1.8 KB
/
Copy pathDockerfile
File metadata and controls
60 lines (42 loc) · 1.8 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
FROM node:22-alpine AS base
# ---- deps stage: install production + dev dependencies ----
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock .yarnrc.yml ./
COPY .yarn/releases ./.yarn/releases
RUN corepack enable && yarn install --immutable
# ---- builder stage: build the Next.js application ----
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Build-time env. The app reads DATABASE_URL at runtime, but Next may evaluate server
# modules during `next build`, so CI/Coolify builds should provide the same Postgres URL.
ARG DATABASE_URL
ENV DATABASE_URL=$DATABASE_URL
RUN test -n "$DATABASE_URL" || \
(echo "ERROR: Docker build requires --build-arg DATABASE_URL" >&2; exit 1)
ENV NEXT_TELEMETRY_DISABLED=1
RUN yarn build
# ---- runner stage: minimal production image ----
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
# DATABASE_URL must be supplied at container runtime (Coolify Postgres connection string).
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder /app/db/migrations ./db/migrations
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Custom server (`server.js`) calls `next()`; standalone tracing omits most of
# `next/dist/compiled` (including webpack), but the programmatic server still loads it.
# Without this, production fails with MODULE_NOT_FOUND for webpack-lib in the container.
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/next/dist/compiled ./node_modules/next/dist/compiled
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]