📋 Cheat Sheets
· 1 min read

Docker Compose with PostgreSQL & Redis — Just Copy It


You need PostgreSQL and Redis running locally. You don’t want to install either. Here’s the file.

The Docker Compose File

version: "3.8"

services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      DATABASE_URL: postgresql://postgres:postgres@db:5432/myapp
      REDIS_URL: redis://cache:6379
    depends_on:
      db:
        condition: service_healthy
      cache:
        condition: service_started

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: myapp
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 5s
      retries: 5

  cache:
    image: redis:7-alpine
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redisdata:/data

volumes:
  pgdata:
  redisdata:

What’s happening here

  • PostgreSQL 16 on Alpine (small image, fast pull)
  • Redis 7 on Alpine
  • Health check on Postgres — your app won’t start until the database is actually ready, not just “container is running”
  • Named volumes so your data survives [docker compose](/blog/docker-compose-validator/) down
  • unless-stopped restart policy — services come back after a crash or reboot

Common tweaks

Change the database name: Replace myapp in both POSTGRES_DB and DATABASE_URL.

Add pgAdmin for a GUI:

  pgadmin:
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@local.dev
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - "5050:80"

Add a Redis GUI:

  redis-commander:
    image: rediscommander/redis-commander
    environment:
      REDIS_HOSTS: local:cache:6379
    ports:
      - "8081:8081"

Commands you’ll use

# Start everything
docker compose up -d

# See logs
docker compose logs -f

# Stop everything (keep data)
docker compose down

# Stop everything (delete data)
docker compose down -v

# Rebuild after code changes
docker compose up -d --build

That’s it. No installing PostgreSQL. No configuring Redis. Just one file and one command.

📘