Topic 180 / Core
Docker and containers: image management, artifact registries
Chapter 11.2 · Compute: VMs, Containers, Serverless
What and why
VMs (→ 179) isolate at the hardware level, which is more than most application workloads need and slower than most workloads want. A container isolates at the operating-system level — processes share the host kernel, but each container gets its own filesystem, process namespace, and resource limits, via OS features (namespaces and cgroups) rather than a hypervisor. Builds on: → 179 (virtual machines).
Diagram
VM stack: Container stack:
Hardware → Hypervisor → Hardware → Host OS → Container runtime →
[Guest OS + App] × N [App + deps, sharing host kernel] × N
(per-instance kernel) (no per-instance kernel)
How it works
- A container image packages an application with its exact dependencies — libraries, runtime, config — as a set of immutable, layered filesystem diffs; the same image runs identically on a laptop and in production because the environment travels with the code.
- Images build in layers: a code change typically rebuilds only the top layer, reusing cached lower layers (base OS, dependencies) — disciplined structure (stable layers first, volatile code last) keeps builds and pulls fast.
- A container runtime starts a process from an image using the host kernel's namespaces and cgroups to isolate it from other containers on the same host — no separate kernel boot, so startup is near-instant.
- Images are tagged (a version identifier) and pushed to an artifact registry — a versioned store for images, parallel to a package registry for libraries — from which any host or orchestrator pulls them to run.
- Tagging discipline matters: a mutable tag makes "what's actually running" unanswerable after the fact; immutable, content-addressed tags make every deployment traceable to an exact image.
- Registries should scan images for known vulnerabilities (→ 172) before they're allowed to run, and enforce retention policies — old, unused images otherwise accumulate cost and attack surface indefinitely.
- One process per container is convention, not a technical requirement — it keeps the unit of scaling, restart, and failure small and well-defined, which is what makes orchestration (→ 181) tractable.
Practical example
(numbers illustrative). A service's image is 400 MB, most of it a stable base and dependency layer rebuilt perhaps weekly; a code-only change adds a ~2 MB top layer. A host that already has the base layers cached pulls only that 2 MB diff — deployment of a code change takes seconds, not minutes, because layering avoided re-transferring the 398 MB that didn't change.
When to use it
- Use containers as the default packaging unit for application services — fast start, a consistent environment, cheap to run many per host.
- Use a disciplined tagging and scanning policy from the first image — retrofitting traceability after an incident is much harder than building it in.
- Avoid containers for workloads that genuinely need VM-level isolation (strict multi-tenant security boundaries, a different kernel) — containers share the host kernel, so a kernel-level exploit can cross container boundaries in ways a hypervisor boundary resists.
- Failure scenarios: a "works on my machine" bug traced to an un-pinned base-image tag resolving to a different image at build time than at the last deploy; a registry with no retention or scanning policy silently accumulating vulnerable, unused images; a container assuming local disk writes persist, losing them on restart because its filesystem is ephemeral by default.
- Simpler alternative: for a single-developer project or a short-lived script, running the process directly on the host is simpler than containerizing it — containerize once more than one environment or developer needs the same guaranteed setup.
Practice
(lab kind, 20–30 min, shared lab: any container runtime you already have). Build a minimal image for a trivial HTTP service, tag it with an explicit version (not a mutable tag), build it, then change one line of code, rebuild, and confirm from the build output which layers were reused versus rebuilt. Done when you can point to the exact layer boundary that changed and explain why the others were cached.
Shared lab setupKey takeaway
Containers isolate at the OS level instead of the hardware level, trading some isolation strength for near-instant startup and a portable, immutable artifact; disciplined tagging and registry scanning are what make that artifact trustworthy. Done when you can: explain why a container starts faster than a VM, and design a tagging scheme that makes every running instance traceable to an exact build. Used next in → 181 (Kubernetes orchestrates containers at scale), → 186 (CI builds and pushes the image this topic describes).
References
None — this topic makes no external claims.
Source directory