Skip to content

Instantly share code, notes, and snippets.

@yariksheptykin
Last active February 12, 2024 15:32
Show Gist options
  • Save yariksheptykin/918271aab9b051a7bba4765a5ee94931 to your computer and use it in GitHub Desktop.
Save yariksheptykin/918271aab9b051a7bba4765a5ee94931 to your computer and use it in GitHub Desktop.

Playing with Redis using Docker Compose

System Requirements

This tutorial requires the following software:

Please ensure you have these components installed before proceeding. Installation instructions are available at the provided links.

Enjoy your Redis exploration!

Tutorial:

1. Start Redis container with Docker Compose:

Create a file named compose.yaml with the following content:

services:
  redis:
    image: redis:alpine
    ports:
      - "6379:6379"

2. Connect to the container and Redis:

Option 1: Use Docker exec

  1. Run the following command to open a shell inside the Redis container:
docker compose exec redis ash
  1. Inside the container, you can directly run Redis commands.

Option 2: Use Redis CLI client

  1. Install the Redis CLI client on your system.
  2. Connect to the running container using:
redis-cli -h localhost -p 6379

3. Run basic Redis commands:

INFO: Get information about the server:

INFO

MONITOR: Monitor all commands sent to the server:

MONITOR

SET/GET: Set and get a key-value pair:

SET mykey myvalue
GET mykey

Other commands:

  • DEL: Delete a key: DEL mykey
  • KEYS: List all keys: KEYS *
  • LPUSH/RPUSH: Push elements to a list:
    • LPUSH mylist element1 element2
    • RPUSH mylist element3 element4
  • LRANGE: Get elements from a list: LRANGE mylist 0 2 (retrieves elements 0 to 2)
  • SADD/SMEMBERS: Add/Check members in a set:
    • SADD myset member1 member2
    • SMEMBERS myset

4. Exit and Stop the container:

Option 1:

  1. Exit the Redis CLI with QUIT.
  2. Exit the container shell with exit.

Option 2:

  1. Exit the Redis CLI with QUIT.

Stop the container:

docker compose down

Tips:

  • Explore the official Redis documentation for more advanced commands and features: https://redis.io/commands
  • Remember that packages available in the Alpine image might differ slightly from the standard image. Refer to the specific documentation for details.

Talking to redis from other containers

1. Start the Redis container:

Ensure your existing compose.yaml file, with the Redis container definition using redis:alpine, is present (see the first part above). Start the container using:

docker compose up -d

2. Modify existing compose.yaml:

Add another service for the Ubuntu container in the same compose.yaml file:

# Existing Redis service definition

services:
  # ...

  ubuntu:
    image: ubuntu:latest
    depends_on:
      - redis  # Ensures Redis starts first

# Other changes if needed for your network configuration

3. Connect to the Ubuntu container:

Run the following command to connect to the running Ubuntu container:

docker compose exec ubuntu bash

4. Install the Redis CLI client:

Inside the Ubuntu container, install the Redis CLI client:

apt update && apt install redis-tools

5. Connect to Redis:

Use redis-cli to connect to the Redis container by referencing its hostname within the same network:

redis-cli -h redis -p 6379

6. Test Redis:

Execute basic Redis commands to verify the connection:

SET example_key example_value
GET example_key

7. Exit and Stop containers:

Exit the Redis CLI with QUIT. Exit the Ubuntu container shell with exit.

Stop both containers:

docker compose down
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment