Adding a Redis Cache to a DevContainer Compose Stack

Your app needs a cache, and running Redis as a Compose service keeps the devcontainer close to production. This page adds Redis to the stack with a health check and shared networking, so the app reaches it at the hostname redis and the workspace waits until it's ready.

Prerequisites

You need a Compose-backed devcontainer.

  • A devcontainer.json using dockerComposeFile and a workspace service.
  • A shared user-defined bridge network.
  • The app's Redis connection settings.

Redis prerequisitesAdd the service, health-check it, network it, and reach it by name.Redis servicein composeHealth checkredis-cli pingNetworkshared bridgeReachhost = redis

Step-by-Step Implementation

  1. Declare the Redis service.
services:
  redis:
    image: redis:7-alpine
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
    networks: [devnet]
networks: { devnet: {} }
  1. Gate the workspace on Redis health.
    depends_on:
      redis: { condition: service_healthy }
  1. Point the app at the hostname redis.
REDIS_URL=redis://redis:6379
  1. Verify the app reaches it.
devcontainer exec --workspace-folder . -- redis-cli -h redis ping

Redis service anatomyA health-checked Redis service on the shared bridge is reachable by name from the app.redis serviceredis:7-alpinehealthcheckredis-cli ping gates readinessdevnet bridgeapp reaches redis by nameOptional volumepersist if needed

Common Pitfalls

Redis issues are a startup race or a name that won't resolve.

Redis triageA triage path from an unreachable or racing Redis to a stable one.Does redis-cli -h redis ping work?NOShare the bridge networkApp waits for readiness?YESHealth-gated depends_onCache is reachable + ready

SymptomRoot CauseRemediation
App can't reach redisNot on the shared networkPut both services on the devnet bridge
App connects before Redis is readyNo health gateAdd healthcheck + service_healthy
Cache lost on rebuild (if persisted)No volume for dataMount a volume if you need persistence
Wrong host in the appHardcoded localhostUse the service name redis

Conclusion

Adding Redis is a small Compose service with a health check and shared networking: the app reaches it as redis, and the workspace waits until redis-cli ping succeeds. Ephemeral by default, it matches a cache's role; add a volume only if you need persistence.

Configure and guaranteeA health-checked, networked Redis service gives a reachable, ready cache.Configureredis:7 serviceping health checkShared networkGuaranteeReachable by nameReady before appProd-like cache

FAQ

Should Redis data persist across rebuilds in dev? Usually not — a cache is ephemeral by nature, so the default in-memory service is fine and keeps rebuilds clean. If your workflow depends on warmed cache data, mount a volume at Redis's data directory, but for most dev use an ephemeral cache is correct.

Why can't my app reach redis? The app and Redis must share a user-defined bridge network for name resolution. Put both on the same network and connect to the hostname redis (the service name), not localhost — inside the app container, localhost is the app, not Redis.

How do I make the app wait for Redis? Add a healthcheck using redis-cli ping and set the workspace's depends_on to condition: service_healthy, so the app starts only once Redis is accepting connections rather than racing its startup.