DEV Community

gourab yousuf basir
gourab yousuf basir

Posted on

1

Setting Up Redis with Docker: A Step-by-Step Guide

Redis, an open-source, in-memory data structure store, is widely used as a database, cache, message broker, and streaming engine. In this guide, I'll walk you through deploying Redis using Docker, ensuring data persistence and remote connectivity.

Prerequisites

Before starting, ensure you have:

  • A VPS or server with Linux installed
  • Docker installed and running
  • Basic familiarity with command line interfaces
  • SSH access to your server

Step 1: Pull the Redis Image

First, let's download the official Redis image from Docker Hub:

docker pull redis:latest
Enter fullscreen mode Exit fullscreen mode

This fetches the latest Redis image. If you need a specific version, replace latest with the version number (e.g., redis:7.0).

Step 2: Create a Directory for Data Persistence

To ensure your Redis data survives container restarts or removal, create a directory for data persistence:

mkdir -p ~/redis_data
Enter fullscreen mode Exit fullscreen mode

Step 3: Run the Redis Container

Now, deploy Redis with appropriate settings for security and persistence:

docker run -d \
  --name redis \
  -p 6379:6379 \
  -v ~/redis_data:/data \
  --restart always \
  redis:latest \
  redis-server --requirepass your_strong_redis_password
Enter fullscreen mode Exit fullscreen mode

Let's examine each part of this command:

  • -d: Runs the container in detached mode
  • --name redis: Names the container "redis" for easy reference
  • -p 6379:6379: Maps the container's Redis port to the host's port
  • -v ~/redis_data:/data: Mounts the host directory for data persistence
  • --restart always: Ensures automatic restart after system reboots
  • redis-server --requirepass your_strong_redis_password: Starts Redis with password protection

Step 4: Verify the Installation

To confirm Redis is running properly:

docker ps
Enter fullscreen mode Exit fullscreen mode

You should see your Redis container in the list of running containers.

Step 5: Connecting to Redis

To connect to your Redis instance locally:

docker exec -it redis redis-cli
Enter fullscreen mode Exit fullscreen mode

Once connected, authenticate using:

AUTH your_strong_redis_password
Enter fullscreen mode Exit fullscreen mode

For remote connections, use:

redis-cli -h your_server_ip -p 6379 -a your_strong_redis_password
Enter fullscreen mode Exit fullscreen mode

Or programmatically with a Redis URI:

redis://default:your_strong_redis_password@your_server_ip:6379
Enter fullscreen mode Exit fullscreen mode

Step 6: Setting Up Persistence

Redis offers different persistence options. By default, the Redis Docker image uses the RDB (Redis Database) persistence model, which takes snapshots of your dataset at specified intervals.

To enable AOF (Append Only File) persistence for more durability, modify your run command:

docker run -d \
  --name redis \
  -p 6379:6379 \
  -v ~/redis_data:/data \
  --restart always \
  redis:latest \
  redis-server --requirepass your_strong_redis_password --appendonly yes
Enter fullscreen mode Exit fullscreen mode

The --appendonly yes option enables AOF persistence, which logs every write operation.

Step 7: Configuring Redis with a Custom Configuration File

For more advanced configurations, create a custom Redis configuration file:

mkdir -p ~/redis_config
Enter fullscreen mode Exit fullscreen mode

Create a file named redis.conf in this directory:

nano ~/redis_config/redis.conf
Enter fullscreen mode Exit fullscreen mode

Add your configuration settings, for example:

requirepass your_strong_redis_password
appendonly yes
maxmemory 256mb
maxmemory-policy allkeys-lru
Enter fullscreen mode Exit fullscreen mode

Run Redis with your custom configuration:

docker run -d \
  --name redis \
  -p 6379:6379 \
  -v ~/redis_data:/data \
  -v ~/redis_config/redis.conf:/usr/local/etc/redis/redis.conf \
  --restart always \
  redis:latest \
  redis-server /usr/local/etc/redis/redis.conf
Enter fullscreen mode Exit fullscreen mode

Step 8: Security Recommendations

  1. Network Security: Restrict access to port 6379
   ufw allow from trusted_ip_address to any port 6379
Enter fullscreen mode Exit fullscreen mode
  1. Strong Authentication: Use complex passwords for Redis

  2. Binding: In production environments, consider binding Redis to localhost and using an SSH tunnel for remote connections

  3. Regular Backups: Set up automated backups with a cron job

   crontab -e
   # Add this line to create daily backups
   0 3 * * * docker exec redis redis-cli -a your_strong_redis_password SAVE
Enter fullscreen mode Exit fullscreen mode

Step 9: Monitoring Redis

To monitor your Redis instance:

docker exec -it redis redis-cli -a your_strong_redis_password INFO
Enter fullscreen mode Exit fullscreen mode

This provides comprehensive information about your Redis server, including memory usage, client connections, and persistence status.

Conclusion

You've successfully deployed Redis using Docker with data persistence and remote connectivity. This containerized approach offers flexibility, isolation, and easy management of your Redis instance.

Whether you're using Redis as a cache, message broker, or primary database, this setup provides a solid foundation for your applications.

Remember to adjust configurations based on your specific requirements and regularly monitor your Redis instance for optimal performance.

Happy caching!


Note: Always replace placeholder values like your_strong_redis_password and your_server_ip with your actual secure credentials and server information.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay