How to deploy a Node.js app on Patr

Aditi Shah
The Patr-onus Deployment Blog
5 min readJun 29, 2022

--

In this article, we are going to deploy a NodeJS application on Patr. Before we get started, let’s get a quick overview of NodeJS.

What is NodeJS?

In the simplest terms, NodeJS is a runtime for JavaScript. Node.js is not a silver-bullet new platform that will dominate the web development world, it instead, serves as a platform that fulfils a particular requirement. When it comes to real-time applications such as chat, news feeds and web push notifications, this open-source server-side JavaScript execution platform is useful for developing persistent connections from a browser to a server.

With the basic understanding of NodeJS let’s see move on to deploying a NodeJS app on Patr.

Step 1: Create a Patr account

Platforms shouldn’t make deploying NodeJS applications harder than it already is, right? We’ve chosen Patr for deploying our app, as it completely simplifies the entire process. To get started all you need to do is sign up on app.patr.cloud.

Step 2: Create a Docker repository on Patr

Docker has become the preferred way to containerize modern applications and services. Patr provides first-class support for running docker images. We will create a docker registry for your docker images.

For deploying docker images on Patr, we first need to create a private Docker Repository on Patr.

  1. Go to Docker Repository -> Create Repository

2. Enter the repository name of your choice, for this example let it be “NodeJS”.

3. Then click Create. Once you open you can view all the information related to your docker repo — repository URL, size etc.

Step 3: Dockerize your NodeJS app

To deploy your NodeJS application you need to dockerize your NodeJS app. You can dockerize your app using the following steps in Docker File. Now in your project directory create a file Docker File with:

# base image
FROM node:latest

# working directory inside container
WORKDIR /app

# copy everything to working directory, can add .dockerignore file to ignore node_modules
COPY . .

# install all the dependencies
RUN npm install

# run the server
CMD ["node", "server.js"]

Note: you should use a package like pm2 for running your production server.

Step 4: Build and push your Docker image to Patr

Now that you have your Docker File, it’s time to build it and push it to the Docker registry that you have created in Step 3.

# Build you docker image from Docker File you created
docker build -t my-node-app .

# tag the node image to your Patr's private NodeJs repo
docker tag my-node-app:latest {link_to_your_node_docker_repo_in_patr}:latest

# login to patr's docker registry with your Patr credentials
docker login registry.patr.cloud

# push the tagged image to your Patr's NodeJS docker repo
docker push {link_to_your_node_docker_repo_in_patr}:latest

You can now see the change in the size of the image.

Step 5: Create a deployment for your app

Go to Infrastructure -> Deployment -> Create a new deployment on Patr

i. Enter the name as “nodejs”.

ii. Choose image details as “nodejs”.

iii. Choose tag as “latest”.

iv. Choose a region nearest to your location in the dropdown menu.

v. Click “Next”.

Based on your app select the port, environment variables or secrets in case you have created any. In this tutorial, we will proceed with port only.

vi. Then press next and select the deployment strategy on push or on create.

vii. Select the number of replications for the app.

viii. Choose the machine type and press Create.

You should witness a status similar to the image attached below.

Step 6: Copy Deployment ID

I. Within seconds, your application will be live on Patr.

II. To access your application from your browser, click on the created deployment and copy the deployment-id in the URL.

III. Using your app — You can now access your application by using the URL

{port}-{deployment-id}.patr.cloud

where — {port} is the port which we allowed during the deployment (in our case 8000) — {deployment-id} is the id which we copied in the previous step.

Voila! You have successfully deployed a NodeJS app using Patr within seconds.

--

--