Docker & Setup
The minimal and production-shaped stacks, users, and the lab profiles
Like every module in this repo, Redis runs in Docker, with two compose
files in redis/.
The minimal stack
services:
redis:
image: redis:8.8.3
ports:
- "6379:6379"
volumes:
- redis_data:/data
volumes:
redis_data:cd redis
docker compose up -d
docker compose exec redis redis-cli
127.0.0.1:6379> PING
PONG
127.0.0.1:6379> SET hello world
OK
127.0.0.1:6379> GET hello
"world"This runs Redis with its built-in defaults: no password, no memory limit, RDB snapshots only. Fine for the first lessons; unsafe anywhere else.
The production-shaped stack
compose_prod.yaml adds a config file, ACL users, persistence, a memory
cap, monitoring, and a GUI:
docker compose down # stop the minimal one first; both publish 6379
cp .env.example .env
docker compose -f compose_prod.yaml up -d
docker exec -it redis redis-cli --user default --pass redis123The config file
config/redis.conf sets everything explicitly. The settings that
matter most:
Each one gets its own lesson later. Check any running value with
CONFIG GET maxmemory*.
The container has mem_limit: 512m, double maxmemory. That gap is
deliberate. maxmemory counts only the dataset. Redis also needs
memory for client buffers, replication buffers, and, during an RDB
save or AOF rewrite, copy-on-write pages from fork(). Set the
container limit equal to maxmemory and the first background save
gets the process OOM-killed.
Users
config/users.acl defines four users. Redis checks every command
against the connected user's permissions:
| User | Password | Allowed |
|---|---|---|
default | redis123 | Everything |
app | app123 | Normal commands on app:*, cache:*, session:*, rate:*, lock:*, lb:*, stream:* only; no admin or dangerous commands |
readonly | readonly123 | Read commands on all keys |
exporter | exporter123 | The handful of commands redis_exporter needs |
docker exec -it redis redis-cli --user app --pass app123
127.0.0.1:6379> SET app:greeting hi
OK
127.0.0.1:6379> SET other:key hi
(error) NOPERM No permissions to access a key
127.0.0.1:6379> FLUSHALL
(error) NOPERM User app has no permissions to run the 'flushall' commandApplications should always connect as a restricted user like app.
More in Security.
The lab profiles
# Python labs (build once)
docker compose -f compose_prod.yaml --profile labs build labs
docker compose -f compose_prod.yaml run --rm labs python seed.py
# replication + Sentinel failover
docker compose -f compose_prod.yaml --profile ha up -d
# Redis Cluster
docker compose -f compose_prod.yaml --profile cluster up -dRedisInsight
Open http://localhost:5540, choose Add Redis database, and enter host
redis, port 6379, username default, password redis123. (The host
is redis, not localhost, because RedisInsight connects from inside
the Docker network.) It shows every key with its type, TTL, and memory,
plus a slowlog viewer and a profiler. Useful for exploring; the lessons
use redis-cli so you learn the commands themselves.
Monitoring
The Prometheus stack from the Prometheus & Grafana module
already scrapes redis-exporter (job redis), has Redis alerts, and
provisions a Redis Overview dashboard. Start it from ../prometheus
whenever you want graphs while working through these lessons.
Everyday commands
# shell
docker exec -it redis redis-cli --user default --pass redis123
# one-off commands
docker exec redis redis-cli --user default --pass redis123 --no-auth-warning INFO memory
# stop, keep data
docker compose -f compose_prod.yaml stop
# remove everything, including data
docker compose -f compose_prod.yaml --profile ha --profile cluster down -v