Redis Installation and Usage with Docker

Redis Installation and Usage with Docker

Redis is a powerful in-memory data store commonly used as a cache, message broker, and database. This guide covers how to install and use Redis using Docker for development purposes.

Why Use Redis?

  • Performance: Extremely fast due to in-memory data storage.
  • Simplicity: Key-value storage with minimal setup.
  • Versatility: Suitable for caching, real-time analytics, session storage, and more.

Installing Redis with Docker

Step 1: Pull the Redis Docker Image

docker pull redis:latest

Step 2: Run the Redis Container

docker run --name my-redis -d -p 6379:6379 redis

Explanation:

  • --name my-redis: Assigns a custom name to the container.
  • -d: Runs the container in detached mode.
  • -p 6379:6379: Maps the default Redis port to the host machine.

Step 3: Verify Redis is Running

docker ps

Ensure the Redis container is listed and running.

Step 4: Access Redis CLI

docker exec -it my-redis redis-cli

You can now interact with Redis directly.

Step 5: Basic Redis Commands

SET key "value"
GET key
DEL key

Step 6: Advanced Redis CLI Commands

  • Check Key Expiration:
    TTL key
    
  • Increment a Key Value:
    INCR counter
    
  • Decrement a Key Value:
    DECR counter
    
  • List All Keys:
    KEYS *
    
  • Check Memory Usage:
    INFO memory
    

Using Docker Compose for Redis

You can use Docker Compose to simplify Redis setup.

docker-compose.yml

version: '3'
services:
  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    command: ["redis-server", "--save", "60", "1", "--loglevel", "warning"]
volumes:
  redis_data:
    driver: local

Step 1: Run Docker Compose

docker-compose up -d

Step 2: Verify the Setup

docker-compose ps

Step 3: Access Redis CLI

docker exec -it <container_name> redis-cli

Connecting Redis with PHP/Laravel (Example)

Install Redis Client for PHP

composer require predis/predis

Configure Redis in .env

CACHE_DRIVER=redis
REDIS_HOST=localhost
REDIS_PASSWORD=null
REDIS_PORT=6379

Testing Redis Cache in Laravel

use Illuminate\Support\Facades\Cache;

Cache::put('key', 'value', 3600);
echo Cache::get('key');

Managing Redis Data

Check All Keys

KEYS *

Clear All Data

FLUSHALL

Best Practices

  • Use Password Protection: Set a secure password for production environments.
  • Persistent Storage: Use volumes for data persistence.
  • Resource Management: Limit memory usage for better performance.

Conclusion

Running Redis with Docker is straightforward and highly efficient for development purposes. By leveraging Docker Compose, you can simplify the setup even further. Redis is a versatile tool suitable for caching, messaging, and real-time data processing in modern applications.

Recent blogs
Структурные паттерны в программировании

Структурные паттерны в программировании

Порождающие паттерны в программировании

Порождающие паттерны в программировании

Генераторы и итераторы в PHP

Генераторы и итераторы в PHP

Объектно-ориентированное программирование в PHP

Объектно-ориентированное программирование в PHP

Структуры данных в PHP

Структуры данных в PHP