{"name":"nextjs-dockerfile","title":"Next.js Dockerfile","description":"A multi-stage Next.js Dockerfile that builds the standalone output into a small image running as a non-root user, with .dockerignore and variants.","category":"deploy","updated":"2026-11-08","url":"https://sniptide.com/templates/nextjs-dockerfile","files":[{"name":"Dockerfile","language":"dockerfile","content":"# syntax=docker/dockerfile:1\n# Needs output: \"standalone\" in next.config.ts.\n\nFROM node:24-alpine AS base\nWORKDIR /app\nRUN corepack enable\n\n# Install dependencies only when the lockfile changes.\nFROM base AS deps\nCOPY package.json pnpm-lock.yaml ./\nRUN --mount=type=cache,id=pnpm,target=/root/.local/share/pnpm/store \\\n    pnpm install --frozen-lockfile\n\n# Build the app.\nFROM base AS build\nCOPY --from=deps /app/node_modules ./node_modules\nCOPY . .\nENV NEXT_TELEMETRY_DISABLED=1\nRUN pnpm build\n\n# Run the standalone server as a non-root user.\nFROM node:24-alpine AS runner\nWORKDIR /app\nENV NODE_ENV=production \\\n    NEXT_TELEMETRY_DISABLED=1 \\\n    PORT=3000 \\\n    HOSTNAME=0.0.0.0\nRUN addgroup -S -g 1001 nodejs && adduser -S -u 1001 -G nodejs nextjs\nCOPY --from=build --chown=nextjs:nodejs /app/.next/standalone ./\nCOPY --from=build --chown=nextjs:nodejs /app/.next/static ./.next/static\nCOPY --from=build --chown=nextjs:nodejs /app/public ./public\nUSER nextjs\nEXPOSE 3000\nCMD [\"node\", \"server.js\"]\n"},{"name":".dockerignore","language":"text","content":".git\n.next\nnode_modules\nnpm-debug.log*\n.env\n.env*.local\nDockerfile\n.dockerignore\ncoverage\n"}]}