DEV Community

Cover image for Running Redis with Docker
Kunal Agrawal
Kunal Agrawal

Posted on

Running Redis with Docker

Redis is an open-source, in-memory data structure store that brings high performance to your applications. Imagine needing to store data for quick retrievals, like shopping cart contents in an e-commerce app. Redis is perfect for this task, acting as a super-fast cache or even a database for specific use cases.

Redis Functionalities

  • Data Structures: Redis offers flexibility by supporting various data structures like strings, lists, sets, and sorted sets. You can choose the most efficient structure for your data type.

  • Blazing Speed: In-memory storage makes Redis extremely fast for data access, ideal for real-time applications and caching frequently used data.
    Optional Persistence: While primarily working with in-memory data, Redis allows you to persist data to disk for recovery after restarts using snapshots or append-only files (AOF).

  • Publish/Subscribe Messaging: Redis can act as a message broker, enabling applications to communicate with each other using a publish-subscribe pattern.

Prerequisites

  • Docker

Why Dockerize Your Redis?

While traditional Redis installation gets the job done, Docker offers a compelling alternative for running Redis, especially in development environments. Here's why you should consider the Docker approach:

  • Isolation and Consistency: Docker creates a self-contained environment for Redis, isolating it from your system dependencies. This prevents conflicts and ensures a consistent Redis experience across different development machines, regardless of the underlying operating system.

  • Effortless Setup and Teardown: Forget complex installation steps! Docker allows you to pull a pre-built Redis image and launch it with a single command. Similarly, removing a Redis instance is as easy as stopping and deleting the container. This streamlines your development workflow.

  • Version Control Made Easy: Docker excels at managing different versions of software. You can easily switch between Redis versions by pulling the desired image, enabling you to test compatibility and experiment with different functionalities.

  • Improved Portability with Docker Volumes: Docker volumes are directories on your host machine that persist data outside of containers. By mounting a volume to your Redis container's data directory, you can ensure your data survives container restarts and even transfers between machines. This makes your Redis setup truly portable.

  • Run Different Databases with Different Versions: Docker excels at managing isolated environments. You can run multiple database containers, each with a different version of the software (e.g., MySQL 8 and PostgreSQL 14) on the same machine. Docker ensures each container operates independently, preventing conflicts between database versions or configurations.

  • Clean Development Environment: By keeping Redis (and potentially other databases) within containers, you avoid cluttering your local system with their dependencies. This maintains a clean development environment and simplifies troubleshooting.

  • Scalability Potential: While Docker containers excel at single instances, they also pave the way for future exploration. Docker provides the foundation for easily scaling your Redis setup by managing multiple containers in a cluster if your needs evolve.

Configure Redis on the Host Machine

Pull the Redis Image:

Open your terminal and run the following command to download the official Redis image from Docker Hub:

docker pull redis
Enter fullscreen mode Exit fullscreen mode

Run the Redis Container:

Image description

docker run -d --name my-redis -p 6379:6379 redis
Enter fullscreen mode Exit fullscreen mode
  • -d: Detaches the container from the terminal, allowing it to run in the background.
  • --name my-redis: Assigns a custom name (my-redis) to the container for easier identification.
  • -p 6379:6379: Maps the container's port 6379 (default Redis port) to the host machine's port 6379. This allows you to connect to Redis from the host using localhost:6379. If you omit this flag, Redis will be accessible only from within the container itself.
  • redis: Specifies the image to use, which is redis in this case.

Test Redis Functionality:

To connect with the container running Redis database, enter the following command.

docker exec -it my-redis redis-cli

or you may need to use this command, while using git bash in windows.

winpty docker exec -it my-redis redis-cli

  • docker: This is the Docker command-line tool used to interact with Docker containers.
  • exec: This subcommand instructs Docker to execute a process within a running container.
  • -i: This flag stands for "interactive" and tells Docker to keep the standard input (STDIN) open for the container. This allows you to type commands within the container.
  • -t: This flag stands for "pseudo-tty" and allocates a pseudo-terminal for the container. This provides a shell-like experience within the container.
  • my-redis: This is the name you assigned to your Redis container when you ran it using docker run. It specifies which container you want to execute the command in.
  • redis-cli: This is the actual command you want to run within the container. In this case, it's the redis-cli tool, which is the command-line interface for interacting with Redis.

Redis Insight: A GUI Suite for Effortless Redis Management

Redis Insight simplifies working with Redis by offering a user-friendly GUI for developers. Here are its key strengths:

  • Visualize & Manage Data: Effortlessly browse, filter, and interact with your Redis data structures.
  • Supports Various Structures: Work seamlessly with strings, lists, sets, sorted sets, and hashes.
  • Flexible Data Display: Choose from various data formats like JSON, hexadecimal, and more.
  • Redis Module Friendly: Interact with custom functionalities provided by Redis modules.
  • Docker Integration: Manage Redis instances running within Docker containers.

Setup with Redis Insight

docker run -d --name my-redis -p 6379:6379 -p 8001:8001 -e REDIS_ARGS="--requirepass mypassword" redis/redis-stack:latest

Using this you can have a nice GUI for your database along with the terminal available at localhost:8001.

Do share your thoughts about this article, and queries related to Redis in the comments.

Top comments (0)