Redis Modules in a Docker Container

Author: Ajeet Singh Raina

This simple container image bundles together the latest stable releases of Redis and select Redis modules from Redis Labs. This image is based on the official image of Redis from Docker. By default, the container starts with Redis' default configuration and all included modules loaded.

Modules included in the container#

important

The latest redismod:preview Docker image holds RedisJSON 2.0 and RediSearch 2.2 Private Preview Release. This Private Preview Releases delivers real-time document capabilities with active-active support. The ability to index, query, and use full-text search on JSON documents using RediSearch is the coolest new feature of this release. Try it out today -

docker run -p 6379:6379 redislabs/redismod:preview

Refer to the link provided in the References section for further details.

Step 1. Install Docker#

To use RedisMod on a local Mac, the first step is to install Docker for your operating system. Run the docker version command in a terminal window to make sure that docker is installed correctly.

docker version

It should display Docker Engine Server and Client version successfully.

Step 2. Running Redismod Docker container#

docker run -d -p 6379:6379 redislabs/redismod

Step 3. Connect to Redis database#

You can either use redis-cli or use RedisInsight to connect to Redis database. Let's try using redis-cli as shown below:

redis-cli

Step 4. Verify if all the Redis modules are getting loaded#

$ redis-cli
127.0.0.1:6379> info modules
# Modules
module:name=rg,ver=10006,api=1,filters=0,usedby=[],using=[ai],options=[]
module:name=ai,ver=10002,api=1,filters=0,usedby=[rg],using=[],options=[]
module:name=timeseries,ver=10408,api=1,filters=0,usedby=[],using=[],options=[]
module:name=bf,ver=20205,api=1,filters=0,usedby=[],using=[],options=[]
module:name=graph,ver=20402,api=1,filters=0,usedby=[],using=[],options=[]
module:name=ReJSON,ver=10007,api=1,filters=0,usedby=[],using=[],options=[]
module:name=search,ver=20006,api=1,filters=0,usedby=[],using=[],options=[]

Step 5. Testing Redis Modules#

Let us test drive RediSearch modules as discussed below in detail.

Insert data into RediSearch#

We are now ready to insert some data. This example uses movies data stored as Redis Hashes, so let’s insert a couple of movies:

HSET movies:11002 title "Star Wars: Episode V - The Empire Strikes Back" plot "Luke Skywalker begins Jedi training with Yoda." release_year 1980 genre "Action"
rating 8.7 votes 1127635
HSET movies:11003 title "The Godfather" plot "The aging patriarch of an organized crime dynasty transfers control of his empire to his son." release_year 1972
genre "Drama" rating 9.2 votes 1563839

Your Redis database now contains two Hashes. It is simple to retrieve information using the HMGET command, if you know the key of the movies (movies:11002):

HMGET movies:11002 title rating

Create an index in RediSearch#

To be able to query the hashes on the field for title, say, or genre, you must first create an index. To create an index, you must define a schema to list the fields and their types that are indexed, and that you can use in your queries.

Use the FT.CREATE command to create an index, as shown here:

FT.CREATE idx:movies ON hash PREFIX 1 "movies:" SCHEMA title TEXT SORTABLE release_year NUMERIC SORTABLE rating NUMERIC SORTABLE genre TAG SORTABLE

Search the movies in the RediSearch index#

You can now use the FT.SEARCH to search your database, for example, to search all movies sorted by release year:

FT.SEARCH idx:movies * SORTBY release_year ASC RETURN 2 title release_year

To test drive rest of Redis modules, please visit the links mentioned under "References" section.

References#