Skip to content
Latchkey

How to Cache Compose Image Builds in CI

Compose builds go through Buildx, so registry-backed cache-from and cache-to keep unchanged layers warm between CI runs.

Declare cache_from and cache_to under each service build: block (or use inline cache), so the builder pulls prior layers from a registry instead of rebuilding them.

Steps

  • Enable BuildKit (default with recent Compose and the docker compose build path).
  • Add cache_from and cache_to to the service build config.
  • Point cache at a registry ref you can read and write in CI.

Compose build cache

docker-compose.yml
services:
  api:
    build:
      context: .
      cache_from:
        - type=registry,ref=registry.example.com/api:buildcache
      cache_to:
        - type=registry,ref=registry.example.com/api:buildcache,mode=max

Gotchas

  • The runner needs registry push access for cache_to to store the cache.
  • On ephemeral runners the local layer cache is empty each run, so a registry cache backend is what actually saves time; Latchkey managed runners keep a warm shared cache.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →