Índice do artigofaltam 3 min de leitura
When a pipeline is "slow", the culprit is often a docker build repeating work.
The goal of this post is to leave you with a clear plan to:
- make your Dockerfile cache-friendly
- reuse layers across pipelines (through a registry or a dedicated runner)
- avoid pitfalls on ARM64 and x64
This post is part of the series:
CI/CD series map
- CI/CD guide in Git for small teams: performance, cache and runners
- GitLab CI: runner tags and job routing
- CI/CD cache: how to design keys and avoid invalid cache (GitHub and GitLab)
- Docker layer caching in CI: BuildKit, buildx, cache-from and cache-to
- Dedicated CI/CD runner: when it pays off and how to plan it in a small team
- macOS runners in CI: Intel vs Apple Silicon, costs and pitfalls
The basics that solve 80%: fix the Dockerfile
Layer caching works per Dockerfile instruction.
If you copy the whole repository before installing dependencies, any change invalidates everything.
Recommended pattern:
- Copy manifests/lockfiles.
- Install dependencies.
- Copy the rest of the code.
- Build.
Example (Node):
FROM node:24 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run buildAbout the image version (official Node.js schedule, checked on November 11, 2026): Node 20 went out of support on April 30, 2026, so do not use node:20 as a base anymore. Node 24 is in maintenance until April 30, 2028, and Node 26 became active LTS on October 28, 2026. Pin the major version in the tag (node:24, not node:latest) so you do not switch major versions without noticing. The node:24 tag is still republished on every minor and patch update, and each new base image invalidates the cache from the FROM line onward. Docker's documentation points out that tags are mutable and suggests pinning the digest to always get the same image version. Pinning the full version (node:24.x.y) reduces the changes, but that tag is also mutable and can be republished with a new base image. For truly predictable caching, pin the digest (node:24@sha256:...) and update on purpose.
1) What "Docker layer caching" means in CI (in practice)
Layer caching means reusing the results of earlier Docker build steps.
In CI, you only reuse layers consistently when:
- the runner keeps a local cache (dedicated runner)
- or you use a remote cache (registry cache)
2) Recommended strategy: registry cache
The idea is simple:
--cache-topublishes the cache to the registry--cache-frompulls the cache from the registry on the next run
Conceptual example with BuildKit/buildx:
docker buildx build --push \
--cache-from=type=registry,ref=registry.example.com/app:buildcache \
--cache-to=type=registry,ref=registry.example.com/app:buildcache,mode=max \
-t registry.example.com/app:${GIT_SHA} \
.Two details from Docker's documentation. First, mode=max stores every layer, including those from intermediate steps; the default (min) stores only the ones that end up in the final image. max takes more space but tends to hit the cache more often. Second, the registry cache only works with the default docker driver when the containerd image store is enabled; without it, create a builder with another driver (for example, docker buildx create --driver docker-container --use). With that driver, the image only leaves the builder with --push (or --load), which is why the example above uses --push.
3) On dedicated runners: local cache works very well
If you have a dedicated runner (and it is not ephemeral), the Docker daemon can keep layers locally.
Advantages:
- very fast
- simple to operate
Risks:
- a runner shared by many projects "fills up the disk"
- isolation and security need attention (especially with Docker-in-Docker)
4) ARM64 vs x64 in docker build
If you build images for more than one architecture:
- builds under emulation are slower
- caches are not fully reusable across architectures
Good practices:
- split pipelines (or a matrix) by architecture
- use buildx multi-arch when it makes sense
- avoid mixing caches across architectures
Final checklist
- The Dockerfile copies manifests before the code.
- Dependencies are installed in a stable layer (the cache gets reused).
- There is a cache strategy (registry or local).
- Architecture is part of the strategy (ARM/x64).
- The build is observable: time before/after and cache hit/miss.





