So you wanna run a highly available version of Redis and you find Redis Sentinel. But you wanna test it out before you commit or just wanna get started in the easiest and cheapest (free) way possible.
Well using Docker you can have Redis Sentinel up an running in seconds!
Redis Sentinel using the command line
So the easiest way is probably just to fire up redis and redis-sentinel in two seperate commands like this:
docker run --name redis -p 6379:6379 redis docker run --name redis-sentinel -e REDIS_MASTER_HOST=localhost -p 26379:26379 bitnami/redis-sentinel:latest
But I like to make idempotent one-liners cause i will often be using them in relation to a package.json file or something where i fire off a short command to run a longer more complex command.
(docker rm redis --force || true) && (docker rm redis-sentinel --force || true) && docker run --name redis -p 6379:6379 -d redis && docker run --name redis-sentinel -e REDIS_MASTER_HOST=localhost -p 26379:26379 bitnami/redis-sentinel:latest
The nice thing about running it this way instead of with Compose, as we’ll be discussing next, is that you can run this command again and again with no issues. Where as you can not run
docker-compose up if its already up. So if you want to restart your service for some reason, you will have to run
docker-compose restart. This might not be a big issue, but if you have multiple projects that depend on
Redis then you might want the command in each of those projects and having 1 command to run will most likely be preferred by your coworkers and newcomers to your project.
If you are using windows make sure you have the newest version of PowerShell(7+)
If you wanna use a
docker-compose you can use this
docker-compose.yml file below and run it with
version: '2' services: redis-sentinel: image: bitnami/redis-sentinel:5.0 volumes: - redis-sentinel_data:/bitnami redis: image: bitnami/redis:5.0 environment: - ALLOW_EMPTY_PASSWORD=yes volumes: - redis_data:/bitnami volumes: redis-sentinel_data: driver: local redis_data: driver: local
To restart use
docker-compose restart and
docker-compose down to take it back down.