Objective
Run Redis in standalone mode in a Docker container, and interact with it using the Redis CLI and the Node.js client.
Launch the server
Pull the Redis image from Docker Hub
docker pull redis
Start the server in a container
docker run --name redis-server -d redis
Redis CLI
To interact with the server, we will use the Redis CLI (command-line interface). The CLI is included in the same image as the server. To launch the CLI in a separate container, create another instance from the image executing a different command.
docker run -it --link redis-server:redis --rm redis redis-cli -h redis -p 6379
Options
- -it: interactive, as we want to use the CLI.
- –-link redis-server:redis: links to the Redis server container (“redis-server”) and gives it the alias “redis”.
- –rm: automatic cleanup when stopping the container.
- redis: image name.
- redis-cli -h redis -p 6379: command to execute in the container.
- -h: specify host. In this case, “redis” is the alias for the container running the server (refer to the –link flag). This alias was defined using –link.
- -p: port.
Try the following CLI commands to create a new key with an associated string value (set), retrieve the value (get), and get all keys matching a pattern (keys).
set testkey "content" get testkey keys t* quit
The quit command terminates the CLI. Due to the –rm flag, the container is removed. If you relaunch the CLI container, the key-value pair will still be there, as it’s stored in the server container.
Node.js Client
In this section, we will interact with Redis using the Node.js client. We will run Node.js in a separate container. Pull the image from Docker Hub and refer to its documentation.
docker pull node
Create a folder for the Node application. In this example, we will use /home/vagrant/nodeSample. Create hello.js in the folder with the following code in it.
var redis = require("redis"); var client = redis.createClient({host: "redis", port: 6379}); client.set("testkey", "Hello World"); client.get("testkey", redis.print); client.quit();
Notes
- Specify host and port in the createClient() method. For the host, use the alias we will define to refer to the Redis server instance.
- The client.get() method takes a callback function.
Run the script
docker run -it --rm --name my-script --link redis-server:redis -v /home/vagrant/nodeSample:/usr/src/app -w /usr/src/app node node hello.js
Options
- –link: to connect to Redis server container.
- -v: create a volume (bind mount). The syntax is host folder:container folder.
- -w: specifies the working directory when the container is launched. In this case, it will be the same as where the volume is mounted.
- The first node is the image name. The second one is the command to run the script.
The command will generate an error saying that Node cannot find the redis module (required by the script). We need to install the module before running the script.
Instead of launching a script directly, let’s get a shell inside the Node.js container.
docker run -it --rm --name hello-script --link redis-server:redis -v /home/vagrant/nodeSample:/usr/src/app -w /usr/src/app node bash
The command to execute in the container was changed to bash to get a shell. In the shell, then run
npm install redis node hello.js
The script will reply with “Hello World”.
Recommended Reading
Redis Essentials (ISBN: 1784392456)
Coverage of Redis data types with sample applications. Overview of cluster architectures.
Leave a Reply