fix: Dockerfile server.js discovery — handle flat and nested standalone output

- Removed fragile find/cp flatten logic in builder
- Runner now copies full standalone/ and symlinks server.js if nested
- Fixes MODULE_NOT_FOUND when Next.js creates flat vs nested output
- Port 3020 and APP_DIR /home/ubuntu/cirux confirmed consistent
This commit is contained in:
2026-05-31 17:41:23 -05:00
parent d99aec2f1a
commit 09ebb795cd
+16 -20
View File
@@ -1,16 +1,13 @@
# ─── Stage 1: Builder (install ALL deps + build) ──────────────────────────── # ─── Stage 1: Builder ───────────────────────────────────────────────────────
FROM node:20-alpine AS builder FROM node:20-alpine AS builder
RUN apk add --no-cache libc6-compat RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Copy dependency files first (cache layer)
COPY package.json package-lock.json ./ COPY package.json package-lock.json ./
RUN npm ci && npm cache clean --force RUN npm ci && npm cache clean --force
# Copy source code
COPY . . COPY . .
# Build requires these env vars for static analysis
ARG NEXT_PUBLIC_SUPABASE_URL ARG NEXT_PUBLIC_SUPABASE_URL
ARG NEXT_PUBLIC_SUPABASE_ANON_KEY ARG NEXT_PUBLIC_SUPABASE_ANON_KEY
ENV NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL ENV NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL
@@ -18,17 +15,6 @@ ENV NEXT_PUBLIC_SUPABASE_ANON_KEY=$NEXT_PUBLIC_SUPABASE_ANON_KEY
RUN npm run build RUN npm run build
# Flatten standalone output — find the single subdir Next.js creates
RUN set -e && \
echo "=== Standalone output ===" && \
ls -la /app/.next/standalone/ && \
mkdir -p /app/.next/standalone-export && \
STANDALONE_DIR=$(find /app/.next/standalone -maxdepth 1 -mindepth 1 -type d | head -1) && \
echo "Copying from: $STANDALONE_DIR" && \
cp -r "$STANDALONE_DIR"/* /app/.next/standalone-export/ && \
ls -la /app/.next/standalone-export/ && \
test -f /app/.next/standalone-export/server.js && echo "✅ server.js found" || (echo "❌ server.js NOT FOUND" && exit 1)
# ─── Stage 2: Production Runner ──────────────────────────────────────────── # ─── Stage 2: Production Runner ────────────────────────────────────────────
FROM node:20-alpine AS runner FROM node:20-alpine AS runner
WORKDIR /app WORKDIR /app
@@ -37,12 +23,22 @@ ENV NODE_ENV=production
ENV PORT=3020 ENV PORT=3020
ENV HOSTNAME=0.0.0.0 ENV HOSTNAME=0.0.0.0
# Create non-root user RUN addgroup --system --gid 1001 nodejs && \
RUN addgroup --system --gid 1001 nodejs adduser --system --uid 1001 nextjs
RUN adduser --system --uid 1001 nextjs
# Copy flattened standalone output (server.js directly in /app) # Copy full standalone output (may be flat or contain a subdir)
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone-export/ ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# Ensure server.js exists at /app/server.js (create symlink if nested)
RUN if [ ! -f /app/server.js ]; then \
SERVER_PATH=$(find /app -maxdepth 2 -name server.js | head -1) && \
if [ -n "$SERVER_PATH" ]; then \
ln -s "$SERVER_PATH" /app/server.js && \
echo "Symlinked server.js: $SERVER_PATH -> /app/server.js"; \
else \
echo "ERROR: server.js not found" && exit 1; \
fi; \
fi
# Copy static assets # Copy static assets
COPY --from=builder --chown=nextjs:nodejs /app/public ./public COPY --from=builder --chown=nextjs:nodejs /app/public ./public