Container build and runtime
What you need to know
A multi-stage image separates the tools required to build Next.js from the small set of files needed to run it. The runtime should use a reviewed base, a nonroot identity and the exact artifact produced by CI.
By the end, you can: design immutable nonroot image; separate build and runtime env.
Runtime stage pattern after a reviewed build
ARG BASE_IMAGEFROM ${BASE_IMAGE} AS runtimeWORKDIR /appENV NODE_ENV=production HOSTNAME=0.0.0.0 PORT=3000COPY --from=build --chown=10001:10001 /app/.next/standalone ./COPY --from=build --chown=10001:10001 /app/.next/static ./.next/staticCOPY --from=build --chown=10001:10001 /app/public ./publicUSER 10001:10001EXPOSE 3000CMD ["node","server.js"]Expected behavior: Partial multi-stage pattern; requires the preceding build stage and a reviewed digest. Container binding is internal; publish to host loopback only.
How it works
A Docker image is a filesystem and configuration artifact, while a container is a running instance. In the build stage, install reviewed dependencies from the lockfile and compile the app. In the runtime stage, copy the standalone tree and assets, including required native dependencies for the chosen OS and architecture. Pin base image digests and dependency versions before execution; a tag is a moving reference. Use a .dockerignore to exclude local environment files, private assets and stale build output. If an approved build requires credentials, use BuildKit secret mounts and make sure build output or logs never contain their values. A secret mount avoids embedding the credential as a build argument, but cannot protect a value that the build itself writes into generated files or logs. Do not pass real secrets as build arguments or bake them into ENV layers. Inject server-only runtime config through the deployment mechanism. Run as a nonroot UID; if caching writes to disk, scope writable paths and test permissions instead of making all files writable.
Exclude private and stale files from context
.gitnode_modules.nextout.env.env.**.pem*.keyExpected behavior: Prevents these matching paths entering the context. It does not scan for secrets under other names.
Common mistake
An image that runs as root may hide filesystem permission defects. A successful image build does not prove readiness, correct assets, application security or cross-platform native compatibility.
Practice and verification
Inspect the staged Dockerfile template, supply a reviewed base digest and package lock in an approved lab, and design checks for UID, writable cache directories, HTTP assets and shutdown. No image build is required to complete the design worksheet.
Scope: Proposed Next.js 16.x / React 19, App Router with Cache Components disabled unless noted. Framework and browser examples are staged, not executed. Hypothetical costs are not actual provider rates.