An application is more than its code. It runs on top of a stack of things the code does not control:

Change any layer and the same code can behave differently.
Drift between machines
Machines that were set up by hand, or set up at different times, drift apart:
| Layer | Developer laptop | Production server |
|---|---|---|
| OS | macOS 14.6 | Ubuntu 22.04 |
| Node.js | v20.17.0 | v18.19.1 |
| Python | 3.12.7 | 3.10.12 |
| OpenSSL | 3.3 | 3.0 |
For example, if a program uses Array.prototype.toSorted() (added in Node.js 20), it might pass every test on the developer's machine but crash in production with:
TypeError: items.toSorted is not a functionThe smallest difference in environemnts can cause major issues.
The Solution: Container images
A packages the application with its runtime, libraries and OS files. A is a process started from that image.
The host only provides the kernel and Docker. Everything else comes from the image, so every machine that runs the same image gets the same runtime.
The image name decides the runtime, not the host:
docker run --rm node:20-alpine node --version
docker run --rm node:18-alpine node --versionv20.18.0
v18.20.4The part after the colon is the tag. It selects a version of the image: node:20-alpine is Node.js 20 on Alpine Linux, node:18-alpine is Node.js 18. --rm deletes the container when it exits; the image stays.
What an image does not fix
- The kernel. Containers share the host kernel. A Linux image needs a Linux kernel.
- Configuration you pass in. Environment variables and mounted files still differ per environment (Chapter 3).
- External services. The database and the network are outside the image.