5.3.8 · D3MLOps & Deployment

Worked examples — Containerization with Docker

3,570 words16 min readBack to topic

Throughout we use one concrete layer stack — the canonical ML Dockerfile from the parent — with made-up but realistic per-layer times so we can compute real numbers:

Now look at Figure 1 (its title reads "Fig 1: Layer stack - find k, sum costs from k upward"). It draws this exact stack bottom-to-top, marks each layer's cost in yellow, and — with the red dashed bracket — shows the rebuild region for a code edit: only the top three layers (from up) get rebuilt, while the blue-outlined layers below stay cached at cost . Every example below is just this picture with a different bracket position.

Figure — Containerization with Docker

The scenario matrix

Every build/run situation this topic can throw falls into one of these cells. The examples below hit each cell exactly.

Cell Trigger What changes Expected Covered by
A. Zero-change rebuild, edited nothing nothing none () Ex 1
B. Code-only edit edit app.py copy-code layer up Ex 2
C. Dependency change edit requirements.txt mid-stack Ex 3
D. Base-image bump change FROM tag everything (degenerate) Ex 4
E. Wrong ordering COPY . . too early code edit busts pip Ex 5
F. Port mapping -p host:container arithmetic runtime, not build Ex 6
G. Ephemeral vs volume file written at runtime data survival Ex 7
H. Image-size / slim full vs slim base disk + pull cost Ex 8
I. Word problem CI fleet, pull savings shared layers Ex 9
J. Exam twist CMD override + ENTRYPOINT which args win Ex 10

The worked examples

Figure — Containerization with Docker
Figure — Containerization with Docker

Active recall

Recall Cover the matrix from memory

Q: Editing only app.py — which layer index is , and why isn't pip install rebuilt? ::: (the COPY . . layer); pip install is layer 4, below , so it's a cache hit. Q: Changing requirements.txt costs almost as much as a cold build — why "almost" and not exactly? ::: , so layers 1–2 (base + workdir, cost ) are still cached; only the base's s is saved. Q: Which single Dockerfile mistake turns a s rebuild into s? ::: Putting COPY . . before pip install, dropping below the pip layer so code edits bust it. Q: -p 8000:8080 when the app listens on 8000 — reachable? ::: No; the container side must be 8000. This forwards to 8080 where nothing listens → refused. Q: A file written at runtime with no volume — survives docker rm? ::: No; the writable layer is ephemeral. Mount -v host:container to persist. Q: docker run img --model v2 with CMD ["--model","v1"] — final command? ::: python predict.py --model v2; run-args replace the whole CMD, so v1 vanishes.


Connections


Editing only source code sets the first-changed layer k to which position in the canonical stack?
k = 5, the COPY . . layer; pip install (layer 4) stays cached.
Why does changing requirements.txt cost ~123 s but a base bump costs the full 133 s?
requirements enters at k=3 (base still cached, saving 10 s); FROM change makes k=1, nothing cached.
In -p HOST:CONTAINER, which number must equal the port the app actually binds?
The right-hand CONTAINER number.
With ENTRYPOINT fixed and CMD as defaults, what happens to CMD when you pass run-time args?
The run-time args replace the entire CMD wholesale, not per-flag.