Multi-Stage Dockerfile Generator (Node, Go, Python, Rust)
Create hardened, lightweight multi-stage Dockerfiles for Node.js, Golang, Python, Rust, and Java with non-root security and cache optimization.
# =========================================================================# Multi-Stage Dockerfile: Next.js Standalone (Hardened & Layer-Cached)# Generated by ToolsAA Multi-Stage Dockerfile Generator# =========================================================================# syntax=docker/dockerfile:1.6
# -------------------------------------------------------------------------# Stage 1: Dependencies Resolution# -------------------------------------------------------------------------FROM node:22-alpine AS depsWORKDIR /app
# Install libc6-compat if Alpine (required for some Node native extensions)RUN apk add --no-cache libc6-compat
# Install package manager lockfiles first to leverage layer cachingCOPY package.json package-lock.json* yarn.lock* pnpm-lock.yaml* bun.lockb* ./RUN --mount=type=cache,target=/root/.npm \ if [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm install --frozen-lockfile; \ elif [ -f yarn.lock ]; then yarn --frozen-lockfile; \ elif [ -f bun.lockb ]; then npm install -g bun && bun install --frozen-lockfile; \ else npm ci; fi
# -------------------------------------------------------------------------# Stage 2: Application Builder# -------------------------------------------------------------------------FROM node:22-alpine AS builderWORKDIR /app
COPY --from=deps /app/node_modules ./node_modulesCOPY . .
# Disable Next.js telemetry during compilationENV NEXT_TELEMETRY_DISABLED=1ENV NODE_ENV=production
RUN --mount=type=cache,target=/app/.next/cache \ if [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ elif [ -f yarn.lock ]; then yarn build; \ else npm run build; fi
# -------------------------------------------------------------------------# Stage 3: Production Minimal Runner# -------------------------------------------------------------------------FROM node:22-alpine AS runnerWORKDIR /app
ENV NODE_ENV=production \ PORT=3000 \ NEXT_TELEMETRY_DISABLED=1ENV NODE_ENV=productionENV PORT=3000ENV HOSTNAME="0.0.0.0"
# Security: Create unprivileged system group and userRUN addgroup -g 10001 -S nodejs && \ adduser -u 10001 -S -G nodejs nextjs# Copy public static assets and standalone bundleCOPY --from=builder /app/public ./publicCOPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://127.0.0.1:3000/api/health || exit 1EXPOSE 3000
USER nextjsCMD ["node", "server.js"]
Pruned compilation tools and devDependencies from runner stage.
UID 10001 unprivileged non-root execution with dropped capabilities.
Sub-second rebuilds via lockfile pre-caching and BuildKit mounts.
Multi-Stage Layer Optimization
Separates compilation dependencies and caches from production runner environments, reducing final image footprint by up to 92%.
CIS Benchmark Security Guardrails
Generates non-root system users (UID 10001), drops Linux capabilities, and omits build toolchains from runtime containers.
Production Standards & Compliance
All project manifests and container scripts are parsed and synthesized strictly inside browser memory without network telemetry.