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 buildpath). - Add
cache_fromandcache_toto 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=maxGotchas
- The runner needs registry push access for
cache_toto 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
How to Build Images With Compose in a CI PipelineBuild all service images defined in docker-compose.yml in one step with docker compose build, pin the build c…
How to Build Compose Services With docker compose bake in CIUse docker compose bake to build all services through Buildx Bake for parallel, cache-aware image builds driv…
How to Run Compose on Self-Hosted RunnersRun docker compose on self-hosted CI runners reliably by cleaning stale state between jobs, pinning the Compo…