9.1.2.2 Compose Local Caches
A focused guide to Compose Local Caches, connecting core concepts with practical Docker and container operations.
Compose local caches are caching services — typically Redis or Memcached — defined within a Compose file, providing a developer with a consistent local caching layer for development and testing without needing to install and manage that caching software directly on their machine.
Defining a Local Cache Service
A cache service definition is typically minimal, since most caching software requires little configuration to run for local development purposes.
services:
cache:
image: redis:7
ports:
- "6379:6379"
Why Caches Typically Don't Need a Volume for Local Development
Unlike a database, a local development cache's content is often perfectly fine to lose between restarts, since cached data is by nature derived and regenerable — many local cache setups intentionally omit a volume, relying on the cache simply repopulating itself as the application runs.
services:
cache:
image: redis:7
Without an explicit volume, this cache's data exists only in its container's writable layer (or, depending on configuration, only in memory), which is generally an acceptable trade-off for local development use.
Connecting an Application to the Local Cache
An application configured to use this cache references it by the Compose service name, exactly as it would reference any other Compose-defined service.
services:
api:
environment:
- CACHE_URL=redis://cache:6379
cache:
image: redis:7
Resetting the Cache to a Clean State
Restarting the cache service provides an effectively clean cache state, useful when testing behavior that depends on cache content being absent.
docker compose restart cache
Why Compose Local Caches Matter
Defining a local caching service through Compose provides the same convenience and consistency benefits as a local database — a reliable, easily managed dependency available to every developer on a team without requiring direct installation, configured identically across everyone's development environment.