Open this lesson in your favourite AI. It'll walk you through the why, explain the demo, and quiz you on the try-it list.
An image is an immutable, content-addressed stack of read-only filesystem layers plus a JSON manifest; a container is a thin read-write layer on top of an image plus a running (or stopped) process. Confusing the two is the most common source of docker rmi errors and disk-bloat surprises. Once you see the layered filesystem with docker history and copy-on-write in action, image hygiene stops being mysterious.
Build a tiny image and watch each instruction become a layer with its own digest.
docker history demo:v1 and identify which line corresponds to the RUN apk add curl step and how many bytes it added.docker exec -it c1 sh, write a file with echo hi > /tmp/x, then run docker diff c1 and confirm the new file shows up.docker rmi alpine:3.20 while demo:v1 exists and read the error — then docker rmi demo:v1 first and retry to confirm the dependency rule.Use these three in order. Each builds on the one before.
In one paragraph, explain the difference between a Docker image and a Docker container, like I'm new to it.
Walk me through how Docker's overlay2 storage driver implements copy-on-write across image layers and the container's writable layer, step by step.
Given an image that has grown to 2.4 GB across 47 layers, explain how you would diagnose which `RUN` instructions contributed the most bytes and how you would refactor the Dockerfile to shrink it.
# Pull a base, list layers, then build on top of it:
docker pull alpine:3.20
docker history alpine:3.20
cat > Dockerfile <<'EOF'
FROM alpine:3.20
RUN apk add --no-cache curl
COPY hello.txt /hello.txt
CMD ["cat", "/hello.txt"]
EOF
echo "hi from a layer" > hello.txt
docker build -t demo:v1 .
docker history demo:v1 # each instruction = one layer
docker image inspect demo:v1 --format '{{json .RootFS.Layers}}' | jq
# Run a container; the writable top layer is per-container:
docker run --name c1 demo:v1
docker run --name c2 demo:v1
docker diff c1 # empty until you write inside