Skip to main content
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.
Invalidation conditions per instruction:

Instruction ordering

Principle: low-churn instructions first, high-churn last.
The 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:
Key points: merge 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 to major.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 / ENV stay in docker history; inject at runtime (docker run --env / Compose env_file / secrets), not hardcoded.
  • Be careful with ADD from URLs: caching and auth are complex; for downloads prefer RUN curl with a checksum, and for plain copies prefer COPY.

Next

Reference: docs.docker.com/build/cache, build best practices