The Shipping Container (Docker) a show in 8 chapters
Goal: ship an app that starts unchanged on another machine, and keep 3 apps in the disk of 1
1 of 8 Your guess
Before we start
Your program runs fine on your own laptop. You hand it to a server, a computer somewhere else that runs it for other people, and there it falls over.
A tool called Docker is how most software makes that trip now. Which picture is closest to what it sends?
Pick the closest. You find out which fits at the end.
58 boxes, one square each: 15 to a row, 3 full rows and 13 in the last
$5.83 = 583 cents583 ÷ 15.7 ≈ 37: nearly 40 times cheaper
2 of 8 The problem
1956: cargo, loaded by hand
Before 1956, a ship left port carrying crates, barrels and sacks, every one a different shape. Dockworkers carried them aboard by hand, one at a time.
A ship could sit in port for days. Cargo broke, went missing or got stolen at almost every handoff between truck, dock and hold.
Malcolm McLean, who owned a trucking firm in North Carolina, had a different idea: stop unloading the truck at all. Put the whole sealed trailer body on the ship.
On April 26, 1956, the SS Ideal X, a converted oil tanker, left Port Newark for Houston with 58 of his steel boxes on its deck. Loading them took under 8 hours.
McLean's own numbers: loading loose cargo the old way cost about $5.83 a tonne. Loading it sealed in his boxes cost about 15.7 cents.
What changed
- Cost to load a tonne: 5.83 → 15.7 cents
The box itself was not clever. It worked because every crane, every ship and every truck bed agreed on its exact shape, so nobody at any handoff had to ask what was inside.
Software had the same problem, and took fifty-seven more years to find its own box.
docker build -t shop .Seal: build an image named shop from this folderdocker run shopStart: run the image named shop
1 image, run 1 time = 1 container
3 of 8 The idea
2013: two commands
A small hosting company called dotCloud kept hearing one complaint. An app ran fine on a customer's laptop and broke on dotCloud's servers.
The reason was usually a library, code the app borrows instead of writing itself. The server had a different version of it than the laptop did.
Your goal: ship an app so it starts unchanged on another machine, with no setup there. Then keep 3 apps in about the disk space of 1.
dotCloud's engineers had half an answer. Linux, an operating system, could already wall one running program off from the others on the same machine.
Walled off, a program sees only its own files and cannot touch its neighbours. That is process isolation. But it took a page of hand-written settings every time, tied to that one machine.
On March 15, 2013, dotCloud's founder Solomon Hykes had a 5 minute talk slot at PyCon, a programming conference, in Santa Clara. About 15 people were in the room. He ran two commands.
Press Run line 1. Watch the laptop.
The sealed file is an image: a frozen copy of an app and everything it needs, saved as one named unit. The machine it runs on is its host.
An image on disk does nothing. Running it gives one live copy of it, doing something: a container.
Run the same image again and you get another, identical one, the way identical boxes come off one packing line.
dotCloud open-sourced the project the same year, and soon renamed the whole company after it: Docker, Inc.
FROM alpine:3.20Start from alpine 3.20: a small Linux operating system's filesRUN echo "service A" > /app.txtRun a command that writes service A into a file, app.txtCMD ["cat","/app.txt"]When a container starts, print app.txt
9 characters + 1 newline = 10 bytes
9 characters + 1 newline = 10 bytes
4 of 8 The idea
Stacked sheets
An image is not built in one shot. Its recipe, a file called a Dockerfile, is a short list of steps. Most steps lay one more sheet of files on top of the ones before.
A byte is the smallest unit a file is measured in. One character of text is about one byte.
Press Run line 1. Watch the stack.
Docker calls one sheet a layer. This image is 2 layers and a note.
Why is line 3 not a sheet?
A sheet holds files, and line 3 writes none. It only records what to run when a container starts, so it rides along as a note on the image. The lesson's image lists exactly 2 sheets.
Now change one line of your app and build again. Docker redoes only the top sheet. The base underneath is untouched, so the rebuild is fast and the image barely grows.
The app's line now says: service A
Two different apps built from the same base share every sheet below their own code. App A adds 10 bytes, app B adds 36.
Sharing a sheet safely needs one more idea: a way to know two sheets are byte for byte the same without comparing them.
app.txt holds these 10 bytes
its fingerprint, worked out on this page
one byte changed, A to a
64 digits, each one of 16 (0 to 9, a to f) · each digit is 4 bits, a bit being one 0 or 1 · 64 × 4 = 256 bits
a megabyte, MB, is a million bytesdemo-a = 8,822,697 + 10demo-b = 8,822,697 + 36counted: 8,822,697 + 8,822,707 + 8,822,733 = 26,468,137 bytes, about 26.47 MBon disk: 8,822,697 + 10 + 36 = 8,822,743 bytes
docker push localhost:5555/demo-a:v1Send demo-a to the registry at localhost:5555, on this same machineac5a850e4251: Pushed88b4fba61c4c: Pusheddocker push localhost:5555/demo-b:v1Send demo-b, which shares the baseaf89fa8a4826: Pushed88b4fba61c4c: Mounted from demo-a
5 of 8 The fix
One sheet, kept once
Run a file's bytes through a fingerprint function and you get one 64 digit number. Change a single byte and the whole number changes.
Docker's function is called SHA-256, and the fingerprint it gives is a hash.
One file, one 64 digit fingerprint.
Could two different files get the same 64 digits?
There are 2256 possible fingerprints, a number 78 digits long. Even 1020 files, far more than were ever built into any image, would hit an accidental match with a chance of about 4 in 1038: never, in practice.
Every layer is named by the hash of its own bytes, not by a name a person typed. The lesson built two small apps on alpine:3.20 and listed each image's layers.
The base layer's hash comes out the same in all three, because its bytes are the same. So Docker keeps one copy and lets every image point to it.
Docker prints the first 12 of the 64 digits. A layer's hash covers the whole sheet, so it differs from the file's own hash in step 1.
Ask Docker how big each image is and it says 8.82 MB, three times: it counts everything an image points at, shared or not.
What the disk actually holds is the base once, plus what each image adds: 10 bytes and 36 bytes, exactly the two text files.
What changed
- Bytes for 3 images: 26,468,137 counted → 8,822,743 on disk
These hashes and sizes are one run on one machine. alpine:3.20 is republished with each patch, so yours will differ while the sums between them hold.
Built boxes need a shelf both your laptop and a server can reach: a registry. Docker Hub is the public one. You push an image up to it and pull one down.
A registry files layers by hash, and keeps a small table of names apart from them.
Press Run line 1. Watch the shelf.
That is why the tenth app shipped on a shared base goes up in seconds while the first one took a minute.
6 of 8 The cost
Where the box stops
An image holds files, nothing else. When a container runs, its files are read by the operating system already running on the host.
The core of that operating system, the part every program asks for files, memory and time, is the kernel. The image never packs one of its own.
So "identical everywhere" means the files are identical. What reads them is whatever the host already has. That is why a container is not a virtual machine, a whole pretend computer that does pack its own.
A running container is an ordinary program on the host. The kernel fools it into seeing only its own programs, its own network and its own folders. That part is called namespaces.
The kernel also caps how much processor time and memory each one may use. That part is called cgroups.
The second gap is the chip, the processor that runs the instructions. One image is built for one kind of chip.
So a name like alpine:3.20 quietly points at a different image per chip, and a pull picks the one that matches the machine asking.
amd64 is the name for x86 chips, arm64 for ARM ones.
Pick a machine to pull alpine:3.20.
Build a box only for the chip in your laptop and ship it to a server with a different one, and the server refuses to start it at all.
"Works on my machine," wearing a new costume. It is the first thing worth checking when a box that ran everywhere suddenly will not.
Then why does it run on some laptops?
An ARM laptop with an emulator installed, a program that imitates another chip, can run an amd64 image anyway. That is a convenience of that one machine, not a property of the image. A plain server with no emulator fails.
7 of 8 Your guess
Your picture now
Goal met: one image starts unchanged on another machine with the same kind of chip, and 3 images sit in 8,822,743 bytes, the base plus 46. Which picture is closest now?
Pick one.
c is Docker: sealed files in shared layers, run on the host's own kernel. a is a virtual machine, which packs a whole operating system. b is the old page of setup steps, redone on every machine.
8 of 8 Keep this
Keep this
An image is an app's files, sealed and stacked in layers. A container is one running copy of it. Each layer is named by the hash of its bytes, so a shared base is kept once, on disk and on the registry.
The price: the box seals files only. The kernel and the chip still belong to the host.
Read the full lessonComing up You sealed the box and kept 3 images in the disk of 1. Next, switch one on: what docker run asks the Linux kernel for, and the walls Google built in 2006 so one job could not starve its neighbours.