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.

redisHelper.js

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

server.js

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

Dockerfile

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 🙂

 

Knoldus-blog-footer-image

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.

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!