Writing Cloud-Native Apps In Nodejs
First let's understand what cloud-native apps are, shall we?
By definition Cloud-native techniques enable loosely coupled systems that are resilient, manageable, and observable. Combined with robust automation, they allow engineers to make high-impact changes frequently and predictably with minimal toil.
This means, unlike traditional monolithic apps, cloud-native embraces microservices architecture, deployed in containers using dev-ops techniques, independently scaled.
Cloud-native applications are
Small, lightweight services
Independently deployable and scalable
Exploit the capabilities of the cloud
You can follow this link & this link for more details. I will stop theory here and let's get hands-on.
Developing microservice using nodejs
we will use expressjs, which is the most widely used nodejs framework to create our first app.
Before we start, make sure you install nodejs and npm on your machine. mac users can use homebrew, and Windows users can use chocolatey to install the latest nodejs.
To install express, you can follow instructions - here
Simply run cmd
npm install -g express-generator
This will install express, you can run the below cmd to see different options available
express -h
To create a new project using express run this cmd
express my-first-express-app
go inside created folder and verify package.json file
cd my-first-express-app
cat package.json
as shown, it includes required dependencies and starts the application using "node ./bin/www"
In order to install these dependencies run this cmd
npm install
This will pull all dependencies defined in package.json and add them to node_modules folder.
You are all setup with default express app. Now just start the app using this cmd
npm start
Open your browser, goto localhost:3000 , and you should see something like this
Congratulations, you now have a running Nodejs app on your local machine. Now, let's containerize this app using docker.
Adding a Dockerfile
We will create a docker image of our running application. This docker image will have all the dependencies installed init in order to run our app.
This will allow us to ship our docker image to any environment/server and run exact same copy of our app with the same version of node without any dependency on a given environment, as we already have built the environment for running our app using the docker image.
Cloud-native community has provided a standard Dockerfile to dockerize nodejs app.
This github repository contains Dockerfile that we can use to dockerize our app.
You will need to install wget utility in order to download files. If you are a Mac or linux user then you probably already have it. For Windows users you can download wget utlity from here add it to your path in system environment variables and you will be ready to go.
This sections explains how to download Dockerfile using wget cmd.
simply run these cmds inside you app root directory to download Dockerfile and .dockerignore files
wget.exe https://raw.githubusercontent.com/NodeShift/docker/main/Dockerfile
wget.exe https://raw.githubusercontent.com/NodeShift/docker/main/.dockerignore
This will download Dockerfile in your root folder
.dockerignore files work like .gitignore file, which basically ignores files that are not required to be copied into a docker image.
Now we can build our docker image using below cmd
docker build -t nodeserver -f Dockerfile .
you can verify docker image using below cmd
docker images
finally to run docker image run below cmd
docker run -i -p 3000:3000 -t nodeserver
Congratulation! you now have your node app running as a docker container which you can run on any server.