Skip to main content

📦 Containers

Portable boxes for applications

The Shipping Container Analogy

Before standardized shipping containers:

  • Goods loaded individually onto ships
  • Different shapes, sizes, handling requirements
  • "Will it fit? Will it break?"

After standardized shipping containers:

  • Standard sizes fit any ship, truck, train
  • Contents don't matter - container is the unit
  • Predictable, stackable, transportable

Software containers work the same way. Package your application with everything it needs. It runs the same everywhere.


What Problem Do Containers Solve?

"Works on My Machine"

DeveloperOps
"Works on my machine!""Doesn't work on the server."

Why?

  • Different OS version
  • Different library versions
  • Different configuration
  • Missing dependencies

Containers Fix This

Container includes:

ComponentPurpose
Application codeYour software
RuntimePython, Node, Java, etc.
LibrariesAll dependencies
ConfigurationEnvironment settings
System toolsWhat your app needs

If it works in the container locally, it works in the container on the server.


Containers vs Virtual Machines

Architecture Comparison

Virtual Machines:                   Containers:
┌───────────────────┐               ┌─────┐ ┌─────┐ ┌─────┐
│     Your App      │               │App A│ │App B│ │App C│
├───────────────────┤               └──┬──┘ └──┬──┘ └──┬──┘
│   Guest OS (large)│                  └───────┼───────┘
├───────────────────┤                 Container Runtime
│    Hypervisor     │                          │
├───────────────────┤                       Host OS
│      Host OS      │                          │
└───────────────────┘                       Hardware

Each VM = Full OS copy             Containers share host kernel

Side-by-Side

AspectVMsContainers
SizeGBsMBs
Boot timeMinutesSeconds
IsolationFull OSProcess-level
Resource usageHeavyLight
PortabilityGoodExcellent
DensityDozens per serverHundreds per server

Container Building Blocks

Images

An image is a blueprint/template:

ContainsExample
Base OS (minimal)Alpine Linux (small base)
Your codeapp.py, index.js
Dependenciesnpm packages, pip modules
Configurationconfig.json, .env

Images are typically treated as immutable — you usually rebuild a new image rather than modifying one in place.

Containers

A container is a running instance of an image.

AnalogyExplanation
Image : ContainerClass : Object
Recipe : DishBlueprint : Building

One image can create many containers.

Registries

Registries store images (like GitHub for code).

RegistryType
Docker HubPublic
AWS ECRPrivate
Google GCRPrivate
Azure ACRPrivate

How Containers Work

Containers use Linux kernel technologies:

Namespaces (Isolation)

Each container has its own view of:

NamespaceWhat's Isolated
PIDProcess IDs
NETNetwork interfaces
MNTFilesystem
USERUser IDs

Container can feel like it has its own little machine.

Cgroups (Resource Limits)

Control resource usage:

LimitExample
CPUMax 2 cores
MemoryMax a few hundred MB
I/O100 IOPS

Prevents one container from hogging everything.

Layered Filesystem

Layer 4: Your app code       (writable container layer)
Layer 3: npm install         (shared image layer)
Layer 2: Node.js runtime     (shared image layer)
Layer 1: Ubuntu base         (shared image layer)

Layers are shared between images!

Benefits of Containers

BenefitHow It Helps
ConsistencySame container: dev → staging → prod
IsolationPython 2 and Python 3 apps on same server
Density100 containers vs 10 VMs per server
SpeedDeploy in seconds, not minutes
MicroservicesEach service in its own container

Container Lifecycle

BUILD → PUSH → PULL → RUN → STOP → REMOVE

1. BUILD image       docker build → Image created
2. PUSH to registry  docker push → Available anywhere
3. PULL to server    docker pull → Image on server
4. RUN container     docker run → Application running
5. STOP when done    docker stop → Container stopped
6. REMOVE            docker rm → Container removed

Orchestration: Managing Many Containers

The Problem

ScaleChallenge
10 containersManage manually
1000 containersWhere to run each? Load balance? Handle crashes?

Kubernetes

The industry standard orchestrator handles:

CapabilityWhat It Does
SchedulingPlaces containers on nodes
Load balancingDistributes traffic
Auto-scalingAdds/removes containers
Self-healingRestarts failed containers
Rolling updatesUpdates without downtime

Common Mistakes

1. Fat Containers

❌ Include✅ Exclude
Dev toolsRemove after build
Unused packagesPrefer production deps
Build toolsMulti-stage builds

Minimal containers start faster, use less space.

2. Running as Root

Container process as root increases risk. Run as non-root user.

3. Mutable State Inside Container

Container dies → Data lost. Use volumes for persistent data!

4. Using "latest" Tag

❌ Bad✅ Good
image:latestimage:vX.Y.Z
Unknown versionPredictable, traceable

FAQ

Q: Containers vs serverless?

AspectContainersServerless
ControlYou manage runtimeCloud manages all
ComplexityMore controlSimpler
Cold startFastCan be slow

Q: Windows containers?

Yes! Windows has native container support. But Linux containers are more common.

Q: Are containers a strong isolation boundary?

They provide isolation but share the kernel. Depending on your threat model, you may want VM isolation too. Follow sensible hardening practices.

Q: Docker vs containers?

Docker is the most popular container runtime. Containers are the concept; Docker is one implementation.


Summary

Containers package applications with all dependencies for consistent, portable deployment.

Key Takeaways:

  • Container = app + dependencies + config
  • Lighter than VMs, share host OS kernel
  • Same container works everywhere
  • Fast to start, easy to scale
  • Images are immutable blueprints
  • Use orchestration (Kubernetes) for many containers
  • Foundation of modern deployment

Containers greatly reduce "works on my machine" problems.

Leave a Comment

Comments (0)

Be the first to comment on this concept.

Comments are approved automatically.