Images vs containers
Understanding how images are built from layers explains why Docker is fast, why images can be huge or tiny, and why changes inside a container disappear.
Images are made of layers
An image is a stack of read-only layers. Each layer is a set of file changes: install Python, copy your code, and so on. Layers are shared and cached: if ten images use python:3.12, its layers exist on disk only once.
docker pull python:3.12-slim
docker history python:3.12-slim # see the layers and their sizes
docker image inspect python:3.12-slimContainers add a writable layer
When you start a container, Docker adds a thin writable layer on top of the image's layers. Every file you create or change goes there.
┌───────────────────────────┐
│ Container layer (R/W) │ ← your changes, deleted with the container
├───────────────────────────┤
│ Layer: COPY app code │
│ Layer: pip install │ read-only image layers
│ Layer: python runtime │ (shared by all containers)
│ Layer: debian base │
└───────────────────────────┘This leads to the most important rule in Docker:
Containers are disposable. Anything written inside a container is lost when it's removed. Data that must survive goes in a volume (lesson 5).
See it for yourself:
docker run -it --name temp alpine sh
# inside the container:
echo "hello" > /note.txt
exit
docker start -ai temp # same container → the file is still there
cat /note.txt
exit
docker rm temp
docker run -it alpine sh # NEW container from the same image
cat /note.txt # → No such fileImage names and tags
registry/namespace/repository:tag
docker.io/library/nginx:1.27-alpine
ghcr.io/my-org/my-api:v2.3.1- If you leave out the registry, Docker uses Docker Hub.
- If you leave out the tag, Docker uses
:latest, which is just a tag name, not a guarantee of anything. In production, always pin specific versions. - Variants:
-alpineis a tiny Alpine Linux base,-slimis a trimmed Debian, and no suffix means a full Debian with lots of tools.
docker images # list local images
docker pull node:22-alpine
docker tag node:22-alpine my-node:dev # add another name to the same image
docker rmi my-node:dev # remove a tag/imageInspecting containers
docker run -d --name web -p 8080:80 nginx:1.27-alpine
docker inspect web # full JSON config: IP, mounts, env, etc.
docker stats # live CPU / memory usage
docker top web # processes inside the container
docker diff web # files changed in the writable layerContainer lifecycle
created → running → paused → running → stopped (exited) → removed
▲ │
└──────── docker start ────────┘A container lives as long as its main process (PID 1). When that process exits, the container stops. That's why docker run ubuntu with no command exits immediately: bash has no terminal attached, so it quits.
Practice
- Pull
nginx:1.27-alpineandnginx:1.27and compare their sizes withdocker images. - Run the alpine one, create a file inside it with
docker exec, and view it withdocker diff. - Explain in one sentence why the file would be lost after
docker rm.
1.Two containers run from the same image. One modifies /etc/config. What does the other see?
2.What does the tag latest guarantee?