Dockerfile layer cache
Understanding the layer cache is what tells you why Dockerfile instruction order affects build speed.
Core principles
- Each instruction is a layer, the unit of caching. Docker compares the instruction string and relevant file checksums; only an exact match hits the cache.
- Invalidation cascades: when one layer’s cache is invalidated, every layer after it rebuilds, whether or not they changed.
| Instruction | Triggers invalidation |
|---|---|
FROM | The base image digest changes |
RUN | The instruction string changes (not the actual output: apt-get update pulling new versions does not invalidate the cache) |
COPY / ADD | The source file checksum changes (mtime does not count) |
ENV / ARG / WORKDIR, etc. | The instruction string changes |
Instruction ordering
Principle: low-churn instructions first, high-churn last.RUN apt-get update trap: keep update and install in the same RUN, otherwise the cached update layer can leave install pulling stale versions:
Forcing a skip and cleaning up
Cache mount (persist package cache across builds)
Persist package download cache across builds (not in an image layer):--mount=type=cache keeps the cache out of image layers and only persists it on the builder, saving build time over the traditional approach.
Best-practice template
Collected into a template you can copy directly:RUN and clean apt cache (rm -rf /var/lib/apt/lists/*), pip install --no-cache-dir to keep pip cache out of the image, pin the base tag, switch to a non-root USER, use .dockerignore to shrink the context, and use WORKDIR with absolute paths instead of RUN cd.
Notes and security
- Avoid
latest: pin the base tomajor.minor(or stricter, a digest@sha256:...) so the base does not silently change versions. - Switch to a non-root
USER: running containers as root by default is risky; create a dedicated user and switch to it. - Keep secrets out of the Dockerfile:
ARG/ENVstay indocker history; inject at runtime (docker run --env/ Composeenv_file/ secrets), not hardcoded. - Be careful with
ADDfrom URLs: caching and auth are complex; for downloads preferRUN curlwith a checksum, and for plain copies preferCOPY.
Next
- Dockerfile instructions one by one: USER, HEALTHCHECK, and other details.
- A Python app Dockerfile: multi-stage to shrink the image further.