Fixing Next.js Standalone Dockerfile 404 Errors on /_next/static and /public
Fix broken CSS, JS, and images returning 404 Not Found in Next.js output: 'standalone' Docker deployments. Complete multi-stage Dockerfile for single repos and Turborepo monorepos.
When you configure output: 'standalone' in next.config.mjs, Next.js traces required node_modules into .next/standalone and generates a minimal server.js file, but intentionally DOES NOT copy the .next/static or public/ directories into .next/standalone (assuming a CDN will serve them). When self-hosting Next.js in Docker on Coolify or Vexlo Cloud without an external S3 CDN, your runner stage must explicitly copy /app/.next/static into /app/.next/static (or /app/apps/web/.next/static in a monorepo) and /app/public into /app/public alongside server.js.
Verified Root Causes in Production Environments
- Omitted Static Asset Copy Directives in Runner Stage: Copying only .next/standalone leaves out compiled CSS/JS chunks (.next/static) and static media (public/), causing unstyled HTML and HTTP 404 responses on all /_next/static/chunks/* URLs.
- Monorepo Nested Standalone Path Mismatch: In Turborepo/pnpm workspaces, .next/standalone preserves the nested apps/web/ directory structure, so copying .next/static to the root /app/.next/static instead of /app/apps/web/.next/static still produces 404s.
- Starting Standalone Build with next start Instead of node server.js: Running npx next start on a standalone directory ignores the traced minimal server configuration.
1. Why Next.js Standalone Excludes .next/static and public/ by Design
Setting output: 'standalone' in next.config.mjs (or next.config.ts) instructs the Next.js compiler to use @vercel/nft (Node File Trace) to analyze every import in your application and produce a self-contained .next/standalone folder. This shrinks Docker image sizes from over 1.2 GB down to roughly 130 MB by stripping devDependencies and unused packages.
However, because Vercel's edge architecture uploads .next/static and public/ to an external object store, Next.js omits both folders from .next/standalone. When running node server.js inside a self-contained Docker container, the built-in Node HTTP server looks for static assets relative to the directory where server.js resides.
In production container environments behind Traefik, Nginx, or Cloudflare CDN, serving missing static chunks with a 404 status code also triggers browser MIME-type strictness errors (Refused to execute script because its MIME type 'text/html' is not executable). Because Next.js App Router relies on React Server Components (RSC) flight payloads alongside client chunk hydration, a missing .next/static folder breaks client-side navigation even if the initial server-rendered HTML document appears in the browser viewport.
When deploying on Vexlo Cloud or self-hosted Coolify instances, verify that your Dockerfile copies both .next/static and public with the exact --chown=nextjs:nodejs user permissions before switching to USER nextjs. This guarantees zero permission-denied errors when the standalone Node.js HTTP server streams static assets or optimizes images via sharp at runtime.
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
reactStrictMode: true,
// Optional: disable source maps in Docker builds to save RAM and image size
productionBrowserSourceMaps: false,
};
export default nextConfig;2. Complete Production Dockerfile for Standard Next.js Repositories
In a standard single-package Next.js repository, .next/standalone contains server.js at its root. Therefore, inside your final runner stage, you must copy three distinct artifacts from the builder stage: (1) /app/public to ./public, (2) /app/.next/standalone to ./, and (3) /app/.next/static to ./.next/static.
Always run the container under a non-root user (nextjs:nodejs with UID 1001) and bind HOSTNAME="0.0.0.0" so reverse proxies like Traefik, Caddy, and Nginx can reach port 3000.
# syntax=docker/dockerfile:1.7
FROM node:20-alpine AS base
RUN apk add --no-cache libc6-compat
WORKDIR /app
FROM base AS deps
COPY package.json package-lock.json* pnpm-lock.yaml* ./
RUN corepack enable && pnpm install --frozen-lockfile
FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN corepack enable && pnpm run build
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# 1. Copy public assets (favicons, images, robots.txt)
COPY --from=builder /app/public ./public
# 2. Copy standalone traced server and minimal node_modules
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
# 3. Copy compiled CSS/JS chunks into .next/static relative to server.js
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
CMD ["node", "server.js"]3. Fixing 404 Static Assets in Turborepo & pnpm Monorepos
In a monorepo where your Next.js app lives at apps/web, Next.js places the traced entry point at .next/standalone/apps/web/server.js so it can share workspace packages in the root node_modules folder. If you copy .next/static to /app/.next/static, the server at /app/apps/web/server.js will still return 404 Not Found for every CSS and JS file.
To fix monorepo standalone builds, copy apps/web/.next/static into ./apps/web/.next/static and apps/web/public into ./apps/web/public, then invoke CMD ["node", "apps/web/server.js"].
# Runner stage for Turborepo / pnpm workspace (app located at apps/web)
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Copy the entire traced standalone tree (includes root node_modules + apps/web/server.js)
COPY --from=builder /app/apps/web/.next/standalone ./
# Copy static and public into the nested apps/web/ path where server.js lives!
COPY --from=builder /app/apps/web/.next/static ./apps/web/.next/static
COPY --from=builder /app/apps/web/public ./apps/web/public
EXPOSE 3000
CMD ["node", "apps/web/server.js"]Pre-Deployment Verification Checklist
- Confirm output: 'standalone' is active in next.config.mjs.
- Verify COPY --from=builder /app/.next/static ./.next/static is present in the Dockerfile runner stage.
- Verify COPY --from=builder /app/public ./public is present in the Dockerfile runner stage.
- In monorepos, nest both .next/static and public/ inside ./apps/<app-name>/ to match the location of server.js.
Deploy on Vexlo Cloud in 60s (₹149/mo UPI)
Automated Docker & Nixpacks builds, managed PostgreSQL/Redis, and AI Deployment Doctor. Instant INR UPI for India or USD/EUR/GBP across 17 global currencies.
Frequently Asked Questions
Why does my Next.js Docker container load HTML fine but fail with 404 on all CSS and JS files?
Next.js output: 'standalone' does not copy .next/static into the .next/standalone folder automatically. You must add COPY --from=builder /app/.next/static ./.next/static to your Dockerfile.
Should I run next start or node server.js with Next.js standalone output?
Always run node server.js (or node apps/web/server.js in a monorepo). Running next start requires the full node_modules tree and fails in a pruned standalone image.
How do I cache /_next/static/* files behind Nginx or Traefik?
Because files inside /_next/static/ have content-hashed filenames, Next.js automatically sets Cache-Control: public, max-age=31536000, immutable once the directory is properly mounted beside server.js.