New from O’Reilly: The memory architecture behind adaptive AI agents

Read the report
For operatorsHow to build and run a Node.js application using Nginx, Docker and Redis
Ajeet Raina
Ajeet Raina

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.

Image

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

What do you need?

  • 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.

Project structure

Prerequisites:

– Install Docker Desktop

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

Image

INFO

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

Step 1. Create a Docker compose file

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.

Step 2. Create an nginx directory and add the following files:

File: nginx/nginx.conf

File: Dockerfile

Step 3. Create a web directory and add the following files:

File: web/Dockerfile

File: web/package.json

File: web/server.js

Step 4. Deploy the application

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

Expected result

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

Step 5. Testing the app

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

Step 6. Monitoring Redis keys

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:

Further References