Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #27: Setting a Password on Redis without a Custom Config

docker-tips-and-tricks.jpg

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.

Quick Jump:

There’s a couple of approaches you can take:

Custom image:

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 docker run.

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.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments