Docker Tip #27: Setting a Password on Redis Without a Custom Config
Just about every web app I develop uses Redis and being able to easily set a password on it is useful. Here's how you do it.
There’s a couple of approaches you can take:
You could set the password in the
redis.conf file and then build your own custom Docker image with that config file baked in. That certainly works but now you’re responsible for managing a custom Docker image.
Since it has your password in plain text, chances are you’d want to make that image private which now means you need to buy private repositories on the Docker Hub or another private registry (unless you happen to host your own).
Volume mount the config:
Another option is to create a custom
redis.conf like before but volume mount it into the official Redis image. This also works, but now you’re responsible for getting that config file on all of your boxes.
You also need to make sure it stays in sync if you ever change it. Suddenly configuration management is a real thing and you need be on top of it.
Use Docker’s CMD override feature with Docker Compose:
You’re likely using Compose and it’s really easy to set a password without any config file shenanigans. If you happen to not be using Compose, you can just override the
CMD by passing in a custom command to the end of
Here’s how you would normally use the official Redis image:
redis: image: 'redis:4-alpine' ports: - '6379:6379'
And here’s all you need to change to set a custom password:
redis: image: 'redis:4-alpine' command: redis-server --requirepass yourpassword ports: - '6379:6379'
Easy peasy! All we do is call the
redis-server and pass in a custom password. Everything will start up as normal and your Redis server will be protected by a password.