MLOps & Data Engineering

Module 11 of 18

Module 11: Docker & Containers

5 min read838 words
What you'll learn
Explain what a container is and the problem it solvesDistinguish images from containersUnderstand why containers matter for data & MLRecognize what orchestration (Kubernetes) adds

"'It works on my machine' is the oldest excuse in software. Containers make it work on every machine — the same way, every time."

Level: Intermediate · Time: ~4 days · Prerequisites: basic command line

Learning Objectives

By the end of this module, you will be able to:

  • Explain what a container is and the problem it solves
  • Distinguish images from containers
  • Understand why containers matter for data & ML
  • Recognize what orchestration (Kubernetes) adds

1. The "Works on My Machine" Problem

Software depends on its environment — the right language version, libraries, and settings. Move it to another machine and subtle differences break it. Containers package your code together with everything it needs to run, so it behaves identically everywhere.

Explain like I'm new: A container is like a lunchbox that holds the meal and the utensils and napkin. Wherever you take it, you have exactly what you need — no relying on the destination to have a fork. Your app carries its whole environment with it.

2. Images vs Containers

Two words you'll hear constantly:

TermMeaning
ImageA blueprint — a snapshot of code + environment
ContainerA running instance of an image

You build an image once and run many identical containers from it — on your laptop, a server, or the cloud.

Concept: Think "class vs object." The image is the recipe; the container is the actual dish you cook from it. One recipe, unlimited identical dishes.

This one-to-many relationship is exactly what makes scaling easy. Build the image for your prediction service once, and you can launch ten identical containers behind a load balancer to handle a traffic spike, then shut eight down when it passes. Each is a perfect copy — same libraries, same code, same behavior — so it doesn't matter which one a request lands on. Containers are also lightweight: unlike a full virtual machine, they share the host's operating system, so they start in seconds, not minutes.

3. Docker in Practice

Docker is the most common container tool. You write a small Dockerfile describing the environment (base OS, install these libraries, copy this code, run this command), build an image, and run it anywhere Docker exists.

dockerfile
FROM python:3.11
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
CMD ["python", "/app/pipeline.py"]

Try this: List everything your project needs to run — language version, libraries, config. That list is essentially your Dockerfile. Containers turn "setup instructions in a README" into something automatic and guaranteed.

4. Why Containers Matter for Data & ML

ML especially suffers from environment chaos — specific library and driver versions that must match exactly. Containers make a model's environment reproducible: the same container that ran in testing runs in production, so results don't change mysteriously.

Common mistake: Training a model in one environment and deploying it in a different one, then puzzling over why predictions differ. Containers eliminate that class of bug by shipping the exact environment alongside the code. A model trained with one version of a math library can produce subtly different numbers under another version — the kind of bug that costs days to track down and vanishes entirely when the exact environment travels with the code.

5. From One Container to Many: Kubernetes

Running one container is easy. Running hundreds across many servers — restarting crashed ones, scaling with demand, load-balancing — needs an orchestrator. Kubernetes is the standard: it manages fleets of containers automatically.

Concept: Docker packages one app; Kubernetes runs many reliably at scale. You don't need Kubernetes for small projects, but it's how large systems keep services healthy and elastic.

A helpful analogy: if a container is a shipping container, Kubernetes is the port and its cranes — deciding which ship (server) each container goes on, replacing any that fall overboard, and adding more when traffic surges. You wouldn't build a whole port to move a single box, and likewise you shouldn't reach for Kubernetes to run one small service. But once you're operating dozens of containers that must stay up around the clock, doing that coordination by hand is impossible, and an orchestrator becomes essential rather than optional.

✅ Checkpoint

  1. What problem do containers solve?
  2. What's the difference between an image and a container?
  3. What does Kubernetes add on top of Docker?

Answers: 1) They package code with its full environment so it runs identically everywhere, ending "works on my machine." 2) An image is the blueprint/snapshot; a container is a running instance of it. 3) It orchestrates many containers across servers — scaling, restarting, and load-balancing them automatically.

Key Takeaway: Containers bundle code with its entire environment so it runs the same everywhere, solving "works on my machine." An image is the blueprint; a container is a running instance. Docker builds and runs them; this reproducibility is vital for data and ML. To run many containers reliably at scale, Kubernetes orchestrates the fleet.

Further Learning

Part of "MLOps & Data Engineering." Original content for this learning platform.