01. Containers and Images — Pack once, run clean¶
⏱️ Estimated time: 18 min | Level: intermediate
ELI5 callback: Think of a busy shipping port. The dock manager must place every container on the right ship. Heavy ML work needs a cargo crane, and port security keeps lanes and permissions clean.
Why containers beat snowflake servers¶
Containers solve environment drift between laptop, CI, and production. A container packages code, runtime, libraries, and startup command together. Keep the analogy close. The dock manager reads the manifest, the container carries one workload unit, the ship offers capacity, the cargo crane handles ML-heavy lifts, and port security blocks unsafe access. Simple, no? One artifact then moves cleanly from build stage to runtime stage. See. Start with consistency, not magic. Now watch.
build flow
┌────────────┐ ┌────────────┐ ┌────────────┐
│ source │ -> │ image │ -> │ runtime │
└─────┬──────┘ └─────┬──────┘ └─────┬──────┘
│ │ │
v v v
repeatable | portable | fast start
Same recipe, different machines.
How images and layers actually work¶
An image is a read-only recipe, not a running process. Each Dockerfile instruction usually creates another cached image layer. Good layer order speeds rebuilds and shrinks network transfer time. Bad layer order hides secrets, cache junk, and giant toolchains. See. Layers reward discipline. Now watch.
layer stack
┌────────────┐ ┌────────────┐ ┌────────────┐
│ base │ -> │ deps │ -> │ app │
└─────┬──────┘ └─────┬──────┘ └─────┬──────┘
│ │ │
v v v
OS libs | framework | code
Earlier layers should change less often.
Registries, tags, and pull behavior¶
A registry stores images so clusters can pull them later. Tags are convenient labels, but digests are the real identity. Pull policy decides when a node fetches a fresh copy. Image provenance matters because supply chains break silently first. See. Shipping labels matter only when the manifest is trustworthy. Now watch.
push and pull
┌────────────┐ ┌────────────┐ ┌────────────┐
│ build │ -> │ push │ -> │ pull │
└─────┬──────┘ └─────┬──────┘ └─────┬──────┘
│ │ │
v v v
CI artifact | registry | cluster node
Promotion should move one exact artifact.
Runtime details that surprise teams¶
Running a container really means starting a constrained Linux process. ENTRYPOINT sets the executable, while CMD supplies default arguments. Small runtime assumptions break fast once you deploy at cluster scale. Your image should be boring to start, stop, and inspect. See. Runtime defaults become production incidents when nobody tests them. Now watch.
startup path
┌────────────┐ ┌────────────┐ ┌────────────┐
│ entrypoint │ -> │ process │ -> │ signals │
└─────┬──────┘ └─────┬──────┘ └─────┬──────┘
│ │ │
v v v
PID 1 | work loop | shutdown
Start and stop behavior must be explicit.
Image standards for platform teams¶
Platform teams should standardize image rules before cluster growth accelerates. Golden base images reduce repeated security and compliance work. CI should build once and promote the same artifact everywhere. Developers need fast feedback before bad images ever reach Kubernetes. See. Standardization saves engineering hours and reduces midnight surprises. Now watch.
promotion path
┌────────────┐ ┌────────────┐ ┌────────────┐
│ PR │ -> │ scan │ -> │ release │
└─────┬──────┘ └─────┬──────┘ └─────┬──────┘
│ │ │
v v v
lint build | policy check | signed image
Build once, then only promote.
Where this lives in the wild¶
- Flipkart seller services build language-specific base images and scan them in CI.
- Swiggy backend teams promote one signed image across staging and production clusters.
- ML batch jobs in Kubeflow pull large training images from regional private registries.
- Platform groups at fintech companies enforce digest-only deployment for auditability.
Pause and recall¶
- Why is an image not the same thing as a running container?
- Why does layer order change both build speed and image size?
- Why are tags weaker identities than image digests?
- Which runtime details usually fail first under orchestration?
Interview Q&A¶
Q: Why do containers exist when VMs already isolate workloads? A: Containers package application dependencies in a portable unit that starts fast and behaves consistently. VMs isolate harder, but they are heavier and slower for routine app packaging. Common wrong answer to avoid: “Because containers are newer and therefore better.”
Q: Why are multi-stage builds so useful? A: They keep compilers, test tools, and temporary artifacts out of production images. That shrinks attack surface and often cuts image pull time as well. Common wrong answer to avoid: “They are only for making Dockerfiles look advanced.”
Q: Why should production deployments prefer digests over mutable tags? A: Digests point to one exact artifact, so rollback and debugging stay trustworthy. Mutable tags can silently move underneath you and break audit trails. Common wrong answer to avoid: “Tags are enough if the team is disciplined.”
Q: Why should images avoid writing state into their own filesystem? A: Pods can disappear anytime, so local writes vanish unless storage is attached. State belongs in external systems or explicit persistent volumes. Common wrong answer to avoid: “Because containers are read-only by default.”
Apply now (5 min)¶
Take one service you know and sketch a tiny Dockerfile for it. Mark which lines should change rarely and which should change often. Circle one place where a secret might accidentally leak into layers. Now rewrite the Dockerfile as a multi-stage build on paper. Finally, decide one immutable tag and one registry promotion rule.
Bridge. Goods packed. Now the dock manager must schedule them onto ships. → 02 → 02-pods-services-ingress.md