Dockerizing a NodeJS app with Redis

Reading Time: 2 minutes

Hi folks, in this blog I would walk you through how can we seamlessly dockerize a NodeJS app along with redis, So as we know that docker is a pretty decent way of streamlining and making the process of deployment easier, efficient and pluggable, so let’s gets started but before that, the prerequisite for the blog is a basic understanding of docker

Choosing the Node environment and OS:

Since docker is a booming domain nowadays, so we can easily find the resources that we need, like in this case that’s our application specific node version, let’s say we will go with node 8.12.0, for this just like we have GitHub similarly, docker has a repository of prebuilt images that we can use, but again over DockerHub we would get plenty of images, so which one to use well I really suggest selecting one of the images from there that suit your use case, as for this blog I would be using node:8.12.0-alpine so this line says that we wanna use the alpine’s base image which has the node installation with 8.12.0 version, we are interested in using alpine because being an OS it is pretty small in size, so I have a simple node-server app that requires redis to be up locally, here is redisHelper.js which I have written for redis interaction.


let Redis = require('ioredis');
//Expect that redis server running on host on localhost:6379
let redis = new Redis();
let redisSet = (key, value) => {
redis.set(key, value)
let redisGet = (key) => {
let value = redis.get(key, function (err, result) {
return "value not found first set it"
} else {
return `value for ${key} is ${result}`
module.exports = {
view raw redisHelper.js hosted with ❤ by GitHub

and here is server.js pretty simple and here is server.js


let http = require('http');
let url = require('url');
let redis = require('./redisHelper.js')
http.createServer(function (req, res) {
let paresedUrl = url.parse(req.url, true);
let requestQuery = paresedUrl.query;
let requestPath = paresedUrl.path;
if(Object.keys(requestQuery).length == 1){ //part handles Set for redis
let key = Object.keys(requestQuery);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(`Value for ${key[0]} has been set to ${requestQuery[key[0]]}`);
}else if(requestPath.split('/').length == 3 && requestPath.split('/')[1] == "get"){ //part handles Query from redis
redis.redis.get(requestPath.split('/')[2], function (err, result) {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end("value not found first set it");
} else {
res.end(`value for ${requestPath.split('/')[2]} is ${result}`);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Welcome to redis Example please enter you string to set in redis using url like localhost:1234/?foo=bar and then get using localhost:1234/foo');
console.log('Server Running at localhost:1234')
function handleRedisSet(query) {
let key = Object.keys(query);
redis.redisSet(key[0], query[key[0]])
view raw server.js hosted with ❤ by GitHub

so the server expects redis to be up on localhost:6379 as  line new Redis() implies that it would try to found out redis on localhost and 6379 port but without it app wont work so now let’s Docker file for it


#Base Image node:8.12.0 on alpine
FROM node:8.12.0-alpine
#Copy required code
COPY . /demo-app
#Install build essentials for redis
RUN apk add musl-dev gcc make g++ zlib-dev linux-headers
#Redis Installation script
RUN sh /demo-app/
#Setting Work Dir as demo-app
WORKDIR /demo-app
#Installing node_modules
RUN npm install
#Deploying App With redis
CMD ["sh", "-c", "redis-server > /dev/null 2>&1 & node server.js"]
view raw Dockerfile hosted with ❤ by GitHub

So with regards to above dockerfile there are two main parts that we have to look closely that are lines 8th and 22nd, in 8th we are installing some build dependencies for redis to run and 22nd we are deploying the app along with starting the server,  so now just build the docker image using command docker build -t node-app-with-redis . and the run the image using docker run -p 3000:1234 node-app-with-redis to run the app on locahost:3000 or you may also simply pull the image from docker hub using docker pull shuhamknoldus/demo-redis and here is github repo containing all the files above, so that’s it for this blog keep coding 🙂



Written by 

Shubham Verma is a software consultant. He likes to explore new technologies and trends in the IT world. Shubham is familiar with programming languages such as Java, Scala, C, C++, HTML, Javascript and he is currently working on reactive technologies like Scala, Akka , spark and Kafka. His hobbies includes playing computer games and watching hollywood movies.