Learn

How to build and run a Node.js application using Nginx, Docker and Redis

Ajeet Raina
Author
Ajeet Raina, Former Developer Growth Manager at Redis

Thanks to Node.js - Millions of frontend developers that write JavaScript for the browser are now able to write the server-side code in addition to the client-side code without the need to learn a completely different language. Node.js is a free, open-sourced, cross-platform JavaScript run-time environment. It is capable to handle thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency, which could be a significant source of bugs.

In this quickstart guide, you will see how to build a Node.js application (visitor counter) using Nginx, Redis and Docker.

  • Node.js: An open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.
  • Nginx: An open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.
  • Docker: a containerization platform for developing, shipping, and running applications.
  • Docker Compose: A tool for defining and running multi-container Docker applications.

– Install Docker Desktop

Use Docker's install guide to setup Docker Desktop for Mac or Windows on your local system.

INFO

Docker Desktop comes with Docker compose installed by default, hence you don't need to install it separately.

Create an empty file with the below content and save it by name - "docker-compose.yml"

The compose file defines an application with four services redisweb1web2 and nginx. When deploying the application, docker-compose maps port 80 of the web service container to port 80 of the host as specified in the file.

INFO

By default, Redis runs on port 6379. Make sure you don't run another instance of Redis on your system or port 6379 on the host is not being used by another container, otherwise the port should be changed.

Let us deploy the full-fledged app using docker-compose:

Listing the running containers. You should see three containers running and the port mapping as below:

After the application starts, navigate to http://localhost in your web browser or run:

If you want to monitor the Redis keys, you can use the MONITOR command. Install redis-cli on your Mac system using brew install redis and then directly connect to Redis container by issuing the following command: