Container build and runtime
Prerequisite: node-enterprise-audit-and-migration. Objective: Review pinned build recipe. Each example is independent. Tested with Node.js 22.16.0 on Linux. Save as main.mjs in a fresh disposable directory and run node main.mjs. These are static code blocks; external system behavior is not established by a local model.
How it works
Use a build stage for compilation or tests and a smaller runtime stage containing only required output and production dependencies. Pin a reviewed base digest and update it under a security policy. Use npm ci with a committed compatible lockfile when dependencies exist; lifecycle scripts need review. Use a nonroot identity, a direct exec-form command, a restricted build context and a writable temporary location only where needed. A digest identifies content, not whether that content is secure.
Inspect a proposed runtime policy
import assert from 'node:assert/strict';const config={uid:10001,argv:['node','main.mjs'],readOnlyRoot:true};assert.ok(config.uid>0);assert.deepEqual(config.argv,['node','main.mjs']);console.log('nonroot direct command');Expected output:
nonroot direct commandStatic policy checks do not prove container isolation.
Select only runtime assets
import assert from 'node:assert/strict';const files=['main.mjs','test.mjs','README.md','.env','package-lock.json'];const allowed=new Set(['main.mjs','package-lock.json']);const selected=files.filter(f=>allowed.has(f));assert.deepEqual(selected,['main.mjs','package-lock.json']);console.log(selected.join(','));Expected output:
main.mjs,package-lock.jsonAn explicit fixture allowlist excludes secrets and tests; real dependencies must be reviewed.
Common mistake
Do not embed secrets in image layers, copy the entire private workspace, or treat a successful build as a runtime test.
Practice
Review the staged Dockerfile, then identify which checks require an actual image build and a running container.
Reference candidate: node-container-build-and-runtime-lookup (route unverified). Documentation anchors: https://docs.docker.com/build/building/best-practices/. External versions and full integration are pending.
Administrative staged lab: staged-deployment-labs/container/Dockerfile.template. Read its README for prerequisites, execution boundaries and cleanup. Not a verified public route.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Container build and runtime — knowledge check
Apply the lesson to a fresh scenario and explain the evidence boundary.
Why pin an image digest?
Which command form directly launches Node?
What is still needed after building an image?