Quick reference
*Bottom to top: one layer per step*
- Base image — OS files plus the runtime. Pin an exact version. ·
FROM python:3.12-slim - System packages — Extra libraries in one RUN, cache cleaned. ·
RUN apt-get install - Dependency list — Copied before the code so this layer is cached. ·
COPY requirements.txt - Install dependencies — Rebuilt only when the list changes. ·
RUN pip install - Application code — Changes every build. That is why it goes last. ·
COPY . /app - Container layer — Thin, writable, deleted with the container. ·
docker run
Explained
- L1 — Base image. FROM python:3.12-slim gives you a minimal set of OS files and the language runtime. Pin an exact version. A smaller base means fewer packages, which means fewer vulnerabilities to patch.
- L2 — System packages. Extra libraries the app needs, installed in a single RUN step that also cleans the package cache. Split across several steps, the cache stays in the image forever.
- L3 — Dependency list. COPY
requirements.txton its own, before the code. Docker reuses a cached layer when its input has not changed, so this layer is rebuilt only when the list of dependencies changes.
- L4 — Install dependencies. RUN pip install, or npm ci, or composer install. This is usually the slowest step, and thanks to L3 it is skipped on most builds.
- L5 — Application code. COPY .
/app. It changes on almost every build, so it goes last. Put it earlier and every code change rebuilds everything above it.
- L6 — Container layer. When you run the image, Docker adds a thin writable layer on top. Anything written there disappears when the container is removed, which is why databases and uploads belong in volumes.
docker history myapp:1.0 shows every layer and its size. The biggest one is where to start optimizing.
In one line
Order the layers. Builds get fast. What changes often goes on top. Data goes in volumes.
See the layers
docker history myapp:1.0More notes: Docker & containers
- P277The Docker command cheat sheet
- P1995 Dockerfile mistakes to avoid
- P196Alpine vs Debian base images
- P1949 docker commands for building and shipping images
- P1649 docker compose commands for daily work
- P163Anatomy of a container image reference
- P152Docker Compose vs Kubernetes
- P147Quiz: 3 container questions
Open in the notes app: search, filters, copy This note on GitHub