Docker Foundation
Purpose
Produce container images that are small, reproducible, secure, and cache-efficient — for a backend service and for consistent local/CI environments — without baking in secrets or shipping bloated, root-running images.
When to Use
- When a service is containerized for deployment, or dev/CI needs reproducible environments.
- Not for choosing the deployment target (
deployment-selection) or scaffolding app code.
Inputs
- The service's runtime (Node version), build steps, and workspace context (
pnpm-workspaces/npm-workspaces).
- Deployment target expectations (
deployment-selection, ../../backend/backend-deployment).
Discovery Questions
- What is the runtime + exact version, and the build → run split (compile vs serve)?
- Is this a monorepo build (workspace-aware context, pruned dependencies)?
- What must be injected at runtime (config/secrets — never built in)?
Responsibilities
- Use multi-stage builds: a build stage (deps, compile) and a lean runtime stage copying only artifacts — no toolchain, dev deps, or source in the final image.
- Pick a small, trusted, pinned base image (specific digest/tag, slim/distroless where viable) —
latest is non-reproducible and a supply-chain risk (../../security/dependency-security).
- Order layers for cache efficiency: copy manifests + lockfile and install before copying source, so code changes don't bust the dependency layer (monorepo: prune to the target package's deps).
- Run as non-root: dedicated user, minimal filesystem permissions.
- No secrets in the image or build args that persist in layers — config/secrets injected at runtime (
secrets-management, environment-management); use build secrets mounts if a build needs a token.
- Add a thorough
.dockerignore (node_modules, .git, .env, tests) to shrink context and avoid leaking files.
- Produce reproducible, meaningful tags (immutable, e.g. content/commit-based) that
../../backend/backend-deployment promotes across environments unchanged.
Required Workflow
- Define build vs runtime stages.
- Pick + pin a small trusted base image.
- Order layers for dependency caching (workspace-pruned in monorepos).
- Set non-root user +
.dockerignore.
- Ensure runtime-injected config/secrets (none baked); define the tagging scheme.
Decision Rules
- Multi-stage always for compiled/Node services — runtime images carry artifacts, not toolchains.
- Pin base images by digest/specific tag — never
latest (reproducibility + supply chain).
- Secrets are runtime, never image-layer; a secret in any layer is leaked to anyone with the image.
- One immutable image promoted across environments — differences are runtime config only (
../../backend/backend-deployment).
Rules
- No secrets baked into images or persisted build args.
- Non-root runtime.
- Base images pinned;
.dockerignore present.
Anti-Patterns
- Single-stage images shipping the full toolchain + source.
FROM node:latest (non-reproducible, unvetted).
COPY . . before installing deps (cache-busting) or without .dockerignore (leaks .env/.git).
- Secrets in
ENV/build args baked into layers.
- Running as root.
Validation Checklist
Definition of Done
Reproducible multi-stage images on a small pinned base, cache-efficient, non-root, secret-free (runtime-injected config), with a thorough .dockerignore and immutable tags ready for promotion across environments.
Related Skills
deployment-selection, ../../backend/backend-deployment, secrets-management, environment-management, pnpm-workspaces, npm-workspaces, ci-cd, ../../security/dependency-security.
Related Knowledge
../../../knowledge/ (runtime versions, target constraints).
Related References
../../../references/devops/ (Dockerfile patterns, when populated).
Context Loading Guidance
- Requires: runtime + build steps, workspace context, target expectations.
- Does not require: app feature code, deployment-target internals.
- May load:
secrets-management, deployment-selection.
- Stop when: the image design (stages, base, caching, non-root, tags) is recorded.
Token Efficiency Guidance
The stage/layer plan + base-image + tag scheme is the artifact; don't paste full Dockerfiles into discussion.
1---2name: docker-foundation3description: Use to plan Docker images for services — multi-stage builds, small trusted base images, layer caching, non-root runtime, no secrets baked in, .dockerignore, and reproducible tags. For local dev orchestration or production images; does not scaffold application code.4---56# Docker Foundation78## Purpose910Produce container images that are small, reproducible, secure, and cache-efficient — for a backend service and for consistent local/CI environments — without baking in secrets or shipping bloated, root-running images.1112## When to Use1314- When a service is containerized for deployment, or dev/CI needs reproducible environments.15- **Not** for choosing the deployment target (`deployment-selection`) or scaffolding app code.1617## Inputs1819- The service's runtime (Node version), build steps, and workspace context (`pnpm-workspaces`/`npm-workspaces`).20- Deployment target expectations (`deployment-selection`, `../../backend/backend-deployment`).2122## Discovery Questions2324- What is the runtime + exact version, and the build → run split (compile vs serve)?25- Is this a monorepo build (workspace-aware context, pruned dependencies)?26- What must be injected at runtime (config/secrets — never built in)?2728## Responsibilities2930- Use **multi-stage builds**: a build stage (deps, compile) and a lean **runtime stage** copying only artifacts — no toolchain, dev deps, or source in the final image.31- Pick a **small, trusted, pinned base image** (specific digest/tag, slim/distroless where viable) — `latest` is non-reproducible and a supply-chain risk (`../../security/dependency-security`).32- Order layers for **cache efficiency**: copy manifests + lockfile and install before copying source, so code changes don't bust the dependency layer (monorepo: prune to the target package's deps).33- **Run as non-root**: dedicated user, minimal filesystem permissions.34- **No secrets in the image or build args that persist in layers** — config/secrets injected at runtime (`secrets-management`, `environment-management`); use build secrets mounts if a build needs a token.35- Add a thorough **`.dockerignore`** (node_modules, .git, .env, tests) to shrink context and avoid leaking files.36- Produce **reproducible, meaningful tags** (immutable, e.g. content/commit-based) that `../../backend/backend-deployment` promotes across environments unchanged.3738## Required Workflow39401. Define build vs runtime stages.412. Pick + pin a small trusted base image.423. Order layers for dependency caching (workspace-pruned in monorepos).434. Set non-root user + `.dockerignore`.445. Ensure runtime-injected config/secrets (none baked); define the tagging scheme.4546## Decision Rules4748- Multi-stage always for compiled/Node services — runtime images carry artifacts, not toolchains.49- Pin base images by digest/specific tag — never `latest` (reproducibility + supply chain).50- Secrets are runtime, never image-layer; a secret in any layer is leaked to anyone with the image.51- One immutable image promoted across environments — differences are runtime config only (`../../backend/backend-deployment`).5253## Rules5455- No secrets baked into images or persisted build args.56- Non-root runtime.57- Base images pinned; `.dockerignore` present.5859## Anti-Patterns6061- Single-stage images shipping the full toolchain + source.62- `FROM node:latest` (non-reproducible, unvetted).63- `COPY . .` before installing deps (cache-busting) or without `.dockerignore` (leaks .env/.git).64- Secrets in `ENV`/build args baked into layers.65- Running as root.6667## Validation Checklist6869- [ ] Multi-stage; lean runtime image.70- [ ] Small trusted base pinned by digest/tag.71- [ ] Layers ordered for dependency caching (workspace-pruned).72- [ ] Non-root user; `.dockerignore` present.73- [ ] No secrets in image/layers; runtime injection only.74- [ ] Reproducible immutable tags.7576## Definition of Done7778Reproducible multi-stage images on a small pinned base, cache-efficient, non-root, secret-free (runtime-injected config), with a thorough `.dockerignore` and immutable tags ready for promotion across environments.7980## Related Skills8182`deployment-selection`, `../../backend/backend-deployment`, `secrets-management`, `environment-management`, `pnpm-workspaces`, `npm-workspaces`, `ci-cd`, `../../security/dependency-security`.8384## Related Knowledge8586`../../../knowledge/` (runtime versions, target constraints).8788## Related References8990`../../../references/devops/` (Dockerfile patterns, when populated).9192## Context Loading Guidance9394- **Requires:** runtime + build steps, workspace context, target expectations.95- **Does not require:** app feature code, deployment-target internals.96- **May load:** `secrets-management`, `deployment-selection`.97- **Stop when:** the image design (stages, base, caching, non-root, tags) is recorded.9899## Token Efficiency Guidance100101The stage/layer plan + base-image + tag scheme is the artifact; don't paste full Dockerfiles into discussion.