Docker Tutorial — Images vs Containers
This is one of the most fundamental concepts in Docker: an image is a template, a container is a running instance of that template.
The Class vs Object Analogy
If you’ve done any object-oriented programming, this will click instantly:
| Concept | Programming | Docker |
|---|---|---|
| Template | Class definition | Docker Image |
| Instance | Object created from class | Docker Container |
| Immutable | Class doesn’t change when you create objects | Image doesn’t change when you create containers |
| Multiple instances | Many objects from one class | Many containers from one image |
Images Are Read-Only
A Docker image is a read-only template made up of stacked layers:
┌─────────────────────────┐
│ Layer 4: COPY app.py │ (your code)
├─────────────────────────┤
│ Layer 3: RUN pip install│ (dependencies)
├─────────────────────────┤
│ Layer 2: RUN apt-get │ (system packages)
├─────────────────────────┤
│ Layer 1: FROM python:3.11│ (base OS + Python)
└─────────────────────────┘
Once built, these layers never change. That’s what makes images portable and cacheable.
Containers Add a Writable Layer
When you create a container from an image, Docker adds a thin writable layer on top:
┌─────────────────────────┐
│ Writable Layer │ ← container can write here
│ (logs, temp files, etc.)│
├═════════════════════════┤
│ Image Layers (read-only)│
│ Layer 4: COPY app.py │
│ Layer 3: pip install │
│ Layer 2: apt-get │
│ Layer 1: python:3.11 │
└─────────────────────────┘
When the container is removed, the writable layer is deleted. Data in the writable layer is ephemeral.
See It in Action
# One image
$ docker pull nginx
# Three containers from the same image
$ docker run -d --name web1 nginx
$ docker run -d --name web2 nginx
$ docker run -d --name web3 nginx
# All three share the same image layers (saves disk space)
$ docker ps
CONTAINER ID IMAGE STATUS NAMES
a1b2c3d4e5f6 nginx Up 10 sec web1
f6e5d4c3b2a1 nginx Up 8 sec web2
1a2b3c4d5e6f nginx Up 5 sec web3
Quick Reference
Image = read-only template (class)
Container = running instance of an image (object)
Image layers: stacked, immutable, cached, shared
Container: adds writable layer on top
When container is removed: writable layer is deleted