DEV Community

Cover image for How to Spin Up Redis with Docker
Ganesh Kumar
Ganesh Kumar

Posted on

How to Spin Up Redis with Docker

Hello, I'm Ganesh. I'm working on FreeDevTools online, currently building a single platform for all development tools, cheat codes, and TL; DRs — a free, open-source hub where developers can quickly find and use tools without the hassle of searching the internet.

Ever find yourself needing a lightning-fast caching layer or a message broker for your new project, but you're stuck configuring config files and permissions? Whether you're building a real-time leaderboard, caching API responses for your Go backend, or managing session states, slowing down for setup is a pain.

Redis is the industry standard for in-memory data structures. It’s incredibly fast. And when you combine it with Docker Compose, you can spin up a secure, persistent, and fully configured instance in seconds.

In this post, we'll walk through the minimal, clean setup to get a Redis instance running, complete with a custom configuration file and password protection, all in one go.

What You'll Need

  • Docker and Docker Compose installed on your machine.
  • A new project folder to work in.

Step 1: The Project Structure

A clean setup keeps things simple. We need three files to maintain control over our configuration and versions. Let's create a project directory and inside it, a folder to hold our Redis-related files.

my-project/
├── docker-compose.yml
└── redis/
    ├── Dockerfile
    └── redis.conf

Enter fullscreen mode Exit fullscreen mode
  • docker-compose.yml: This is our orchestra conductor. It handles the networking and volumes.
  • redis/Dockerfile: This simple file locks in our specific Redis version.
  • redis/redis.conf: This is our control center. We use this to set passwords, memory limits, and persistence rules.

Step 2: The Dockerfile

This is the easiest part. We are just telling Docker to use the official Redis image. By using a Dockerfile, we ensure that if we ever need to add custom plugins or tools later, we have the structure ready.

Inside redis/Dockerfile, add this single line:

FROM redis:7.4-alpine

Enter fullscreen mode Exit fullscreen mode

This tells Docker to use the Redis 7.4 Alpine image. Using alpine keeps the image size tiny, and locking the version (7.4) prevents your setup from breaking if a major update (like Redis 8) changes defaults unexpectedly.

Step 3: The Configuration File (redis.conf)

Using a redis.conf file gives you much more control than command-line flags. Here, we will configure the server to accept connections from outside the container, set a password, and enable persistence (so you don't lose data on restart).

Create redis/redis.conf and add these essentials:

# 1. Allow connections from outside the container (essential for Docker)
bind 0.0.0.0

# 2. Set the password for the 'default' user
requirepass "securepassword"

# 3. Enable AOF (Append Only File) persistence
# This saves every write operation to disk, making data durable
appendonly yes

# 4. (Optional) Set a memory limit so Redis doesn't eat all your RAM
maxmemory 256mb
maxmemory-policy allkeys-lru

Enter fullscreen mode Exit fullscreen mode

The appendonly yes line is crucial—without it, Redis defaults to snapshots, which might cause you to lose the last few minutes of data if the container crashes. AOF keeps your data safer.

Step 4: The docker-compose.yml File

This is the heart of our setup. It mounts the config file and tells Redis to actually use it.

Place this in your root docker-compose.yml:

version: '3.8'

services:
  cache:
    # 1. Build from our local folder
    build: ./redis
    container_name: redis-cache
    ports:
      # 2. Expose the standard Redis port
      - "6379:6379"
    volumes:
      # 3. Mount our custom config file into the container
      - ./redis/redis.conf:/usr/local/etc/redis/redis.conf
      # 4. Mount a named volume for persistent data
      - redis-data:/data
    # 5. The command to start Redis using our config file
    command: redis-server /usr/local/etc/redis/redis.conf

volumes:
  # 6. Define the volume so data survives container removal
  redis-data:
    name: redis_cache_data

Enter fullscreen mode Exit fullscreen mode

Let's break down the important bits:

  • volumes: We map our local redis.conf to /usr/local/etc/redis/redis.conf inside the container.
  • command: This is key. By default, the Redis image starts without a config file. We explicitly tell it to run redis-server using the file we just mounted.
  • redis-data: This volume mapping ensures that the appendonly.aof file created by Redis is stored safely on your host machine, not just inside the container.

Step 5: How to Run It

Now for the easy part. Open your terminal in the my-project directory and run:

docker compose up -d

Enter fullscreen mode Exit fullscreen mode

The -d flag runs it in "detached" mode.
Your Redis instance is now running! The first time you run this, it will:

  1. Pull the redis:7.4-alpine image.
  2. Start the container.
  3. Load your redis.conf settings (including the password).

You can check the status with docker compose ps.

Step 6: How to Connect to It

You have two main ways to connect.

1. From Your Local Machine (e.g., RedisInsight, TablePlus, or CLI)
You can connect directly using the exposed port (6379).

  • Host: localhost
  • Port: 6379
  • User: default (or leave blank depending on the client)
  • Password: securepassword

If you have redis-cli installed, you can test it immediately:

redis-cli -h localhost -p 6379 -a securepassword

Enter fullscreen mode Exit fullscreen mode

2. From Another Docker Container (e.g., Your Go Backend)
If you are running your application in the same Docker Compose network, do not use localhost. Use the service name defined in the YAML file.

  • Host: cache
  • Port: 6379

What's Next?

You now have a secure, password-protected Redis instance with data persistence enabled. Whether you're caching database queries or managing task queues, this setup is production-ready for your dev environment.


FreeDevTools

I’ve been building for FreeDevTools.

A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction when searching for tools and materials.

Any feedback or contributions are welcome!

It’s online, open-source, and ready for anyone to use.

👉 Check it out: FreeDevTools

⭐ Star it on GitHub: freedevtools

Top comments (0)