Snippets / Compose
Web + Postgres + Redis
The standard three-service local stack: an app, a database, and a cache.
postgresredisweb
docker-compose.yml
services:
web:
build: .
ports:
- "8080:3000"
environment:
# "db" and "cache" resolve via Compose's built-in DNS -- no need to
# know the containers' actual IPs, which change on every recreate.
DATABASE_URL: postgres://app:app@db:5432/app
REDIS_URL: redis://cache:6379
depends_on:
db:
# Waits for Postgres to report healthy, not just for the
# container to start -- avoids a race against db init time.
condition: service_healthy
cache:
condition: service_started
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
volumes:
# Named volume so data survives "docker compose down" (but not
# "docker compose down -v").
- db-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 5s
timeout: 3s
retries: 5
cache:
image: redis:7-alpine
volumes:
db-data:
More compose snippets