# Docker Compose Redis

> This Compose file runs Redis 8 with a password, append-only persistence in a named volume, a 256 MB memory limit and a health check other services can wait on. Save it as compose.yaml, run docker compose up -d --wait, and connect at redis://:dev@127.0.0.1:6379.

Updated November 30, 2026. Canonical: https://sniptide.com/templates/docker-compose-redis
Open it in the Sniptide editor: https://app.sniptide.com/new?template=docker-compose-redis
Files as JSON: https://sniptide.com/templates/docker-compose-redis.json

## Files

### compose.yaml

```yaml
services:
  redis:
    image: redis:8-alpine
    restart: unless-stopped
    command:
      - redis-server
      - --requirepass
      - ${REDIS_PASSWORD:-dev}
      - --appendonly
      - "yes"
      - --maxmemory
      - 256mb
      - --maxmemory-policy
      - allkeys-lru
    environment:
      # redis-cli reads the password from this variable, so the health check can authenticate.
      REDISCLI_AUTH: ${REDIS_PASSWORD:-dev}
    ports:
      - "127.0.0.1:6379:6379"
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

volumes:
  redis-data:
```

## Run it [#run-it]

Use Docker with Compose v2. Run these commands from the folder that holds `compose.yaml`:

```bash title="Terminal"
docker compose up -d --wait
docker compose exec redis redis-cli ping
```

The second command prints `PONG`. From the host, connect with `redis://:dev@127.0.0.1:6379`. The `dev` password is a local default. Set `REDIS_PASSWORD` in a `.env` file next to `compose.yaml` before you use it anywhere else, and update the URL to match.

`docker compose down` stops Redis and keeps the data. Adding `-v` deletes the volume too.

## What each part does [#what-each-part-does]

| Line | What it does | When to change it |
| --- | --- | --- |
| `image: redis:8-alpine` | Pins the major version. | When you upgrade. Read the release notes for a new major first. |
| `--requirepass` | Requires a password from every client. `${REDIS_PASSWORD:-dev}` reads `.env` and falls back to `dev`. | Always set a real value outside your laptop. |
| `--appendonly yes` | Writes every change to a log in `/data`, so data survives a restart. | Remove it for a pure cache you can lose. |
| `--maxmemory` and `--maxmemory-policy` | Caps memory at 256 MB and evicts the least recently used keys when full. | Use `noeviction` when Redis holds queues or sessions you can't drop, and size the limit to your data. |
| `REDISCLI_AUTH` | Gives `redis-cli` the password inside the container, for the health check and `exec`. | Rarely. |
| `ports` | `127.0.0.1:6379:6379` publishes the port to your machine only. | Change the first `6379` if the port is taken. |
| `healthcheck` | Reports healthy once Redis answers `PING`. | Rarely. Other services wait on it, as the next variant shows. |

## Variant: with your app [#variant-with-your-app]

The app reaches Redis by its service name, `redis`, over the Compose network. Redis needs no published port here.

```yaml title="compose.yaml"
services:
  app:
    build: .
    environment:
      REDIS_URL: redis://:${REDIS_PASSWORD:-dev}@redis:6379/0
    ports:
      - "3000:3000"
    depends_on:
      redis:
        condition: service_healthy

  redis:
    image: redis:8-alpine
    command: ["redis-server", "--requirepass", "${REDIS_PASSWORD:-dev}", "--appendonly", "yes"]
    environment:
      REDISCLI_AUTH: ${REDIS_PASSWORD:-dev}
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 10

volumes:
  redis-data:
```

## Variant: with Redis Insight [#variant-with-redis-insight]

Redis Insight is a browser UI for looking at keys. Add it under `services`, run `docker compose up -d --wait`, open `http://localhost:5540` and add a database with host `redis`, port `6379` and your password.

```yaml title="compose.yaml"
  redis-insight:
    image: redis/redisinsight:latest
    ports:
      - "127.0.0.1:5540:5540"
    depends_on:
      redis:
        condition: service_healthy
```
