C.W.K.
Stream
Lesson 04 of 07 · published

Images vs Containers — Class vs Instance

~15 min · foundations, mental-model

Level 0Container Curious
0 XP0/36 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The single most important distinction

Confusing images and containers is the #1 reason new Docker users feel lost. Hold this in your head:

Image = read-only template (the class, the blueprint, the artifact).
Container = running instance with a writable layer on top (the object, the live thing).

One image, many containers

You build the image once. You run as many containers from it as you need. Each container has its own writable layer for runtime state — logs, temp files, anything the process writes that isn't on a mounted volume.

Containers are ephemeral by design

The writable layer dies with the container. docker rm removes the layer. This is a feature: it forces you to externalize state into volumes (data) or registries (images), which is exactly where state belongs.

If your app loses important data when a container is removed, the bug is that the data was never externalized. Not that containers are too fragile.

Code

Three containers, one image·bash
# Pull the image once
docker pull nginx:1.27-alpine

# Spin up three independent containers from it
docker run -d --name web1 -p 8081:80 nginx:1.27-alpine
docker run -d --name web2 -p 8082:80 nginx:1.27-alpine
docker run -d --name web3 -p 8083:80 nginx:1.27-alpine

# Each has its own writable layer.
# Each is reachable on a different host port.
# All share the same image layers on disk.
Prove ephemerality·bash
# Write something inside the container
docker run -d --name bookkeeper alpine sleep 3600
docker exec bookkeeper sh -c 'echo "customer state" > /data.txt'
docker exec bookkeeper cat /data.txt
# customer state

# Remove and recreate
docker rm -f bookkeeper
docker run -d --name bookkeeper alpine sleep 3600
docker exec bookkeeper cat /data.txt
# cat: can't open '/data.txt': No such file or directory

# The writable layer was thrown away. The image is unchanged.
# Solution: volumes. We get there in Track 4.

External links

Exercise

Run three containers from the same redis:7-alpine image, each on a different host port (6381, 6382, 6383). Connect to each with redis-cli and SET name redis-N. Stop all three. Restart them. Did the values persist? Explain why or why not, in two sentences.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.