Volume 4 · Chapter 4 — Compute and Containers
Containers and Docker Fundamentals
4.1 Introduction
Containers are how most modern applications - including banking microservices - are packaged and run, and Docker is the technology that popularized them. A container bundles an application with everything it needs to run into a single portable unit that behaves identically everywhere. Understanding containers, how they differ from virtual machines, and the image-versus-container distinction is essential because containers underpin the orchestration (ECS and EKS) covered in the next chapters and because “why containers?” is a staple interview question that reveals whether you understand consistency, portability, and density.
4.2 Why Containers
The core problem containers solve is “it works on my machine but not in production.” A container packages the application together with its dependencies, libraries, and runtime configuration, so it runs identically regardless of the underlying environment - a developer’s laptop, a test server, or production. This gives consistency (the same artifact everywhere eliminates environment-specific surprises), portability (run it on any host with a container runtime), and efficiency (containers share the host operating system kernel, so they’re lightweight and start in seconds, letting you pack many on a host). For banks moving to microservices, containers are the standard packaging unit precisely because they make deployments consistent and reproducible across every environment.
4.3 Containers vs Virtual Machines
Containers and virtual machines both isolate workloads, but at different levels. A virtual machine virtualizes the hardware - each VM runs a full guest operating system on top of a hypervisor, so VMs are heavier (each carries an entire OS), slower to start, and fewer fit on a host. A container virtualizes the operating system - containers share the host’s OS kernel and isolate only the application and its dependencies, so they’re much lighter, start in seconds, and you can run many more per host. The trade-off is isolation strength: VMs provide stronger isolation (separate kernels), while containers are lighter but share the kernel. In practice, containers often run on top of VMs (container hosts are themselves VMs), combining the density of containers with the isolation boundary of VMs.
flowchart TB
subgraph VMs[Virtual Machines - heavier]
HV[Hypervisor] --> VM1[Guest OS + App]
HV --> VM2[Guest OS + App]
end
subgraph Cont[Containers - lighter, share host kernel]
Eng[Container runtime] --> C1[App + deps]
Eng --> C2[App + deps]
Eng --> C3[App + deps]
end
Host[Host OS / kernel] --- Eng 4.4 Images vs Containers
A crucial distinction is between an image and a container. An image is a read-only template - a packaged snapshot of the application, its dependencies, and configuration, built once and stored in a registry (container images are versioned and immutable). A container is a running instance of an image - when you run an image, you get a container, and you can run many containers from the same image. The analogy is that an image is like a class and a container is like an object instance of it, or an image is the recipe and containers are the dishes made from it. This separation is powerful: you build and test an image once, store it with a version tag, and run identical containers from it in every environment, guaranteeing the running software matches exactly what was built and tested.
# Build an immutable, versioned image once; run identical containers from it anywhere.
docker build -t bank-payments:1.4.2 .
docker push registry.example.com/bank-payments:1.4.2
docker run -d registry.example.com/bank-payments:1.4.2 # a container = a running image
4.5 Registries and Image Security
Images are stored in a registry - on AWS, Elastic Container Registry (ECR) - from which orchestrators pull them to run. Because the image is what actually runs in production, its security matters: images should be built from trusted, minimal base images to reduce attack surface, scanned for vulnerabilities before deployment, versioned with specific tags (not a floating “latest”) so deployments are reproducible and traceable, and stored in a private registry with access controlled by IAM. For a bank, image scanning and provenance are part of the security pipeline - you don’t run an image in production that hasn’t been scanned and approved, because a vulnerable or tampered image is a direct path into the environment.
4.6 Banking Production Scenario
A bank packages each microservice as a Docker image built from a hardened, minimal base image. The image is versioned with a specific tag, scanned for vulnerabilities in the CI pipeline, and pushed to a private ECR repository with IAM-controlled access. Deployments pull that exact versioned image, so the container running in production is identical to what was built and tested - eliminating environment drift and “works on my machine” failures. Because containers share the host kernel and are lightweight, many services run efficiently across the fleet, and because images are immutable and versioned, rolling back is as simple as deploying the previous image tag. Image scanning gates the pipeline, so no unscanned or vulnerable image reaches production.
4.7 Troubleshooting: Container Issues
Container problems often come down to image and configuration. A container that behaves differently in production than in test usually means the image isn’t truly identical - often because a floating tag like “latest” pulled a different build; the fix is specific version tags so the exact image is reproducible. A container that won’t start may have a misconfiguration, a missing environment value or secret, or a failing entrypoint - check its logs. A container consuming too much memory may hit its resource limit and be killed - right-size the limits. A security finding on a running image means it wasn’t scanned or a vulnerable dependency slipped through - scan images in the pipeline and rebuild from patched bases. The consistent discipline is immutable, versioned, scanned images and externalized configuration, so what runs is exactly what was built and its behaviour is reproducible.
Why use containers, and how do they differ from virtual machines?
Containers solve the classic “it works on my machine but not in production” problem by packaging an application together with all its dependencies, libraries, and runtime configuration into a single unit that runs identically everywhere - a developer’s laptop, a test environment, or production. That gives three big benefits: consistency, because the same artifact runs the same way in every environment, eliminating environment-specific surprises; portability, because a container runs on any host with a container runtime; and efficiency, because containers are lightweight and start in seconds, so you can pack many onto a host. For organizations moving to microservices, containers are the standard packaging unit precisely because they make deployments consistent and reproducible. The difference from virtual machines is the level at which they isolate. A virtual machine virtualizes the hardware: each VM runs a full guest operating system on a hypervisor, so VMs are relatively heavy - each carries an entire OS - slower to start, and fewer fit on a host, but they provide strong isolation because each has its own kernel. A container virtualizes the operating system: containers share the host’s OS kernel and isolate only the application and its dependencies, so they’re far lighter, start in seconds, and you can run many more per host - the trade-off being that they share the kernel, so isolation is lighter than a VM’s. In practice the two are often combined: containers run on hosts that are themselves virtual machines, so you get the density and speed of containers with the isolation boundary of VMs at the host level. The key mental model is that a VM packages a whole machine including its OS, while a container packages just an application and its dependencies on a shared kernel - which is why containers are lighter, faster, and more portable, and why they’ve become the default way to package and run modern applications, including a bank’s microservices where consistent, reproducible deployments and efficient density matter.
Explain the difference between a container image and a container.
An image and a container are related but distinct, and confusing them is a common mistake. A container image is a read-only template - a packaged, versioned snapshot of an application together with its dependencies, libraries, and configuration, built once and stored in a registry. It’s immutable: once built and tagged with a version, that image is a fixed artifact. A container is a running instance of an image - when you run an image, you get a container, and you can run many containers from the same image simultaneously. The analogy I use is that an image is like a class and a container is like an object instance of that class, or an image is a recipe and containers are the dishes cooked from it - one template, many running instances. This separation is powerful and central to why containers give reproducible deployments. You build and test an image once, tag it with a specific version, and store it in a registry; then in every environment you run containers from that exact image, so the software running in production is guaranteed to be identical to what was built and tested - there’s no rebuilding per environment and no drift. It also makes rollbacks trivial: because images are immutable and versioned, rolling back is just running containers from the previous image tag. And it enables scaling: to handle more load you simply run more containers from the same image, each identical. A practical implication is to always deploy specific version tags rather than a floating tag like “latest,” because “latest” can point to different builds over time and breaks the guarantee that the running container matches a known, tested image. So the distinction is that an image is the immutable, versioned template stored in a registry, and a container is a live running instance of that image - you build the image once and run many identical containers from it, which is exactly what delivers consistency, easy scaling, and simple rollbacks.
Chapter Revision Notes
- Containers package an app with its dependencies so it runs identically everywhere - consistency, portability, efficiency.
- VMs virtualize hardware (full guest OS each, heavier, stronger isolation); containers virtualize the OS (share the kernel, lighter, faster, denser).
- An image is an immutable versioned template in a registry; a container is a running instance of an image (class vs object).
- Build/test an image once, run identical containers everywhere; deploy specific version tags, not floating “latest.”
- Store images in a private registry (ECR), scan for vulnerabilities, and build from trusted minimal bases - the image is what runs in production.