Node and Redis via Docker

In this tutorial we are going to learn how to set up a small redis development environment and connect to it with a simple node js server.

What is redis

Redis is an open source in-memory data structure store, used as a database, cache and message broker. Amongst its many praises and strengths we find how easy it is to set up and configure and how fast and optimized it works to store non-persistent messages in RAM memory in your computer.


REDIS stands for Remote Dictionary Server. The main idea behind redis is to have an in memory message broker that keeps information in memory to allow your applications or modules in your system to connect and share temporary information between them.


For instance, your website could be storing forms and every time it stored a form, it could connect to redis and emmit a small token of  data. Any other application reading from redis could receive this notification and do more things (such as send an email, print a receipt... the possibilities are limitless).


One of the greatest advantages of Redis is that reading and writing from redis is very fast and easy.  It is designed in principle to be very simple and straight forward. Unlike SQL , it doesn't have tables, rows, columns, functions , procedures..... Redis simply uses data structures to store data , such as Strings, Lists, Sets, Sorted Sets, and Hashes. It is meant to be an in-memory non-persistent database (thought it does have persistence options) and because of this , redis is very fast. (since it normally never has to wait for the operating system to read / write operations in disk).

redis underlying overview

setting up the environment in docker

Well, of course we are going to set docker up! Needless to say, Docker is amazing and if you have reached up to this point and still don't know what docker is, check the internet (or this website) for more information on docker.

So, first things first, we need to set up our docker composer. Head on to a new folder and create the following docker-compose.yml file.

We will be listening to port 6379 and docker will be building the image right there in the same directory. Notice how I am exposing port 6379. While this would normally not be necessary to run your app, this will expose the redis port to other applications outside of the docker environment that may use the port. We will be using this later on, since our node machine will be running from outside of docker.


This is a very simple docker-compose, since we are not using any volumes to store the information. As a software architect when using redis, you have to imagine redis as a non-persistent solution. Some place to store information temporarily for all applications and different parts of your system to read and write messages into and from.

Building your  docker app with compose

on the command line in that very same directory, run the command docker-compose up and you'll see how the docker container starts firing up messages in screen. As of right now, your redis server is alive in the docker container and ready to emmit and receive all sorts of messages from other services or apps. NOTE: You can also easily set up the node.js app to be a part of the docker system, and add the node.js server to your docker compose so everything is fired up by docker containers. However that is not the scope of this tutorial. Lets start off by creating the following app.js. If you feel like it, you can use this docker compose instead.

We will mount our current directory . to /usr/src/service in the container. We will also need to tell docker where our “working directory” is. Meaning — what directory did we put the code in?


This basically means that whatever we do in the current . directory, docker will map to the /usr/src/service folder ,which is the base folde for node to use the folders.

The NODE. JS app

This one is going to be a fairly simple and easy app. If you haven't yet done so, install node.js in your marchine (unless you plan on using a docker container. Once your node js setup is complete keep reading.

If you now head to your local machine address  in your browser (normally localhost or 127.0.0.1)  you will see the Hello world example fully working. Note that if you are running the whole setup in docker, you'll need to use the docker virtual machine IP instead.

Connecting to redis

Connecting to the redis machine is relatively simple, in the previous code example we already connected to redis using redis.createClient() and stored the connection in redisClient variable.


Now, we simply need to suscribe to a channel

redisClient.subscribe('message');

This line of code will suscribe our redisClient to the channel "message" . We now need to set up the function. As you can see, this is also relatively easy.

Bear in mind that this function will run every time someone writes on to the "message" channel. Publishing on to the channel is also relatively easy and we can do this by simply typing in the following.

The data must be transformed into JSON first so we use Stringify to encapsule the information and then access it. The final function is a callback function that is run every time the information is published.

emitting to redis from web 

Emitting to redis server from a webview will be relatively simple. Try to do it yourself simply by changing the get function. As you can see, very straight forward, when we get /our-test, we simply publish a simple message to the notification channel. Take a note on the callback function for when the message has been published. Here we could for example terminate the process.

Emitting to redis server from a webview will be relatively simple. Try to do it yourself simply by changing the get function. As you can see, very straight forward, when we get /our-test, we simply publish a simple message to the notification channel. Take a note on the callback function for when the message has been published. Here we could for example terminate the process.

Remember that to run your javascript from node simply cmd to the folder via the command line and run npm install and run node app.js . There are many other production ways to set this up so that it fires automatically (like using docker) but this should work for development purposes. Also, remember to suscribe the redisClient to the channel first.

Our reader's favorite picks

Joomla Docker Tutorial - Create your first joomla docker container

Creating a Joomla Docker container.

Leave review
In this tutorial we are going to learn how to set up a docker container and...
Read More
Lamp Docker Tutorial

LAMP Docker Tutorial

Leave review
Today we bring you a small pill of knowledge with the hand of Docker for we...
Read More
If you want to work remote then don't make these mistakes!

If you want to work remote then don't make these mistakes!

Leave review
Read More
Telegram Creator acuses Whatsapp of having backdoors for syping

Telegram Creator acuses Whatsapp of having backdoors for syping

Leave review
According to Durov, Whatsapp has never been secure and it has been done lik...
Read More
How app development works in developing countries

How app development works in developing countries

Leave review
Read More
This is what your UI should learn from videogames

This is what your UI should learn from videogames

Leave review
The videogame industry moves over one billion dollars on a yearly basis. It...
Read More
Suscribe to our mailing list

What is this about?

Our latest posts.

How to use docker and wordpress

Wordpress and Docker Tutorial

Leave review
Using docker allows us to run all of our project straight in our own machin...
Read More
Using Redis and Node js 101

Node and Redis via Docker

Leave review
In this tutorial we are going to learn how to set up a small redis developm...
Read More
Answering tricky interview JS Questions

Answering tricky interview JS Questions

Leave review
This is a small comprendum of the sort of questions you may find in a Javas...
Read More
Lamp Docker Tutorial

LAMP Docker Tutorial

Leave review
Today we bring you a small pill of knowledge with the hand of Docker for we...
Read More
5G and the value it adds to your Business

5G and the value it adds to your Business

Leave review
We have heard about the infamous 5G network and the dispute between Huawei,...
Read More

Related topics

Our categories.