If you've ever wanted to try out some of the Redis Modules which were introduced in v4.0 it has never been easier than now: "redismod - a Docker image with select Redis Labs modules".
The included modules are:
- RediSearch: a full-featured search engine
- Redis Graph: a graph database
- Redis ML: a machine learning model server
- ReJSON: a native JSON data type
- Rebloom: native Bloom and Cuckoo Filter data types
DIY?
Rather want to do it yourself? There is also a POC kind of blog post from last year on how to build Redis Modules from scratch with a basic proof of concept dockerfile and also showing how to use it with NodeJS
.
But first have a look at the redismod dockerfile
as it is a great example for using the meanwhile available multi-stage builds in Docker container images.
Setup
Go ahead and first pull this "quasi"-official image by redislabs from Docker Hub:
docker pull redislabs/redismod
And then have an optional look at the image sizes:
# optionally: docker pull redis
docker images | grep redis
REPOSITORY TAG IMAGE ID CREATED SIZE
redislabs/redismod latest 954b356d32d6 2 weeks ago 115MB
redis latest bfcb1f6df2db 3 weeks ago 107MB
8 MB only is the difference in size, yet you get a full-featured, multi-model NoSQL database. Of course, admittedly, Redis Modules are not (yet) as capable as other databases but it is impressive that it is possible at all - with this little footprint.
Config Options
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 (source).
However, if you want only certain modules
to be loaded you can pass these in on the the docker cli
as follows:
--loadmodule /usr/lib/redis/modules/rebloom.so \
--loadmodule /usr/lib/redis/modules/other_module.so
Also on the cli
you can pass in the data
directory to use:
--dir /data
Both of the settings can of course also be set via the redis.conf
file.
The rest, as it is based on the official Redis image, is pretty much as usual with Redis:
- Exposed
port
is 6379 ... if you want this container port mapped to your docker host, use-p 6379:6379
to make it externally accessible - Set
config
directory and file is/usr/local/etc/redis/redis.conf
which you canCOPY
into the container (at image build time) or- map into the container (at container runtime) with a volume like
-v /path/on/host/redis.conf:/usr/local/etc/redis/redis.conf
or - use something like Docker Configs for it (only on Docker Swarm)
- A password
authentication
can be set via- the
redis.conf
file or - in an own Dockerfile
CMD
(be careful to not overwrite the base image'sCMD
for modules, see below) or - in Docker Secrets (again only on Docker Swarm)
- the
Image Build Time
If using redismod
as base for an own dockerfile keep in mind that the redismod dockerfile
uses CMD
to load the modules: So if you use a CMD
in your dockerfile (and thus overwrite the base image's CMD
, make sure to also explicitly load the modules you want in your CMD
instruction).
The ENTRYPOINT
in the base image is redis-server
so your CMD
may not start with redis-server
. This is different from the regular official Redis image where the CMD
itself starts with redis-server
So the best probably is to not mess with a custom CMD
instruction at all, but rather provide a custom redis.conf
file or use the above explained arguments
for docker run
.
Container Runtime
The Docker Hub repo page gives plenty of plain docker run ...
based examples and the Github Hub repo page also offers a (very) basic docker-compose.yaml
.
If you just quickly want to test the default container and especially its Redis Modules (without any data persistence, custom configuration or external accessibility) the easiest way to start is probably with a detached, interactive container which you connect to via docker exec
and then enter redis-cli
for issuing Redis (Module) commands
:
docker run -itd redislabs/redismod
docker exec -it <ctr_id> /bin/bash
redis-cli
127.0.0.1:6379> JSON.SET foo . '"bar"'
OK
127.0.0.1:6379> JSON.GET foo
"\"bar\""
# OR
127.0.0.1:6379> GRAPH.QUERY mygraph "CREATE (:person {name: 'Kurt', age:27})"
1) 1) ""
2) 1) Labels added: 1
2) Nodes created: 1
3) Properties set: 2
4) "Query internal execution time: 0.111000 milliseconds"
127.0.0.1:6379> GRAPH.QUERY mygraph "MATCH (n) RETURN (n)"
1) 1) "n.age,n.name\x00"
2) "27.000000,Kurt"
2) 1) "Query internal execution time: 0.097000 milliseconds"
exit
docker rm -vf <ctr_id>
More examples can be found on each module's docs site.
Other clients than redis-cli
Most of the Redis Module docs site provide further information on available clients ... e.g. for rejson
access in node.js
there is node_redis-rejson . For node.js
and node_redis
you can always take the node_redis send_command which should work for any Redis Module (see the mentioned previous blog post towards the end for some more details).
Summary
Go ahead, give it a try ... it only takes some minutes to get started and might open quite a few options on how (far) to use Redis.
Further Information:
Redismod and Redis Docker Images:
https://hub.docker.com/r/redislabs/redismod/
https://github.com/RedisLabs/redismod
https://hub.docker.com/_/redis/
Docs for the Redis Modules:
https://oss.redislabs.com/redisearch/
https://oss.redislabs.com/redisgraph/
https://oss.redislabs.com/redisml/
https://oss.redislabs.com/rejson/
https://oss.redislabs.com/rebloom/
NodeJS clients:
https://github.com/stockholmux/node_redis-rejson
node_redis send_command
Using Redis Modules with NodeJS in a Docker Container:
https://daten-und-bass.io/blog/using-redis-modules-with-nodejs-in-a-docker-container/