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

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

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

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

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

Once connected, authenticate using:

AUTH your_strong_redis_password

For remote connections, use:

redis-cli -h your_server_ip -p 6379 -a your_strong_redis_password

Or programmatically with a Redis URI:

redis://default:your_strong_redis_password@your_server_ip:6379

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

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

Create a file named redis.conf in this directory:

nano ~/redis_config/redis.conf

Add your configuration settings, for example:

requirepass your_strong_redis_password
appendonly yes
maxmemory 256mb
maxmemory-policy allkeys-lru

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

Step 8: Security Recommendations

  1. Network Security: Restrict access to port 6379
ufw allow from trusted_ip_address to any port 6379
  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

Step 9: Monitoring Redis

To monitor your Redis instance:

docker exec -it redis redis-cli -a your_strong_redis_password INFO

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.