How to host your Node app in a Docker Container on Heroku

Introduction

We got to add a mongo database as a service and maybe you added a couple of endpoints to test it out, but the whole app is in runs locally and you might want to let the whole world have the opportunity to test out your Node app well not to worry, Werick is here.

In this part, we are going to host our application on Heroku.

Note: Since we are introducing a new database service, by doing so this also makes our Node app also a service

Why Heroku?

You might ask yourself why I chose Heroku instead of Digital Ocean, Linode, GCP or AWS...🤔the reason is that Heroku allows you to get up and running quickly and deploy your code without worrying about how your infrastructure runs underneath.

For the other platforms well you will be assigned a CPU(s) in which you will set up the whole thing including installing software, libraries, securing your server with SSH which will kinda consume most of your time and you just want to host your simple express server.

Getting Started

Well enough promoting let host something. First of all, you will have to create an account on Heroku

After creating your account on Heroku, you will have to install its CLI. The Heroku CLI makes it easy to create and manage your Heroku apps directly from the terminal. It’s an essential part of using Heroku.

Mac

Windows

To install it on Windows you just need to know which type works with your computer, is it a 32-bit or 64-bit

Ubuntu

For Ubuntu, you will need to install it with snap:

$ sudo snap install --classic heroku

Let's get into deploying

To check if you successfully installed it globally, type this in your terminal

$ heroku --version

In your terminal cd into the directory where our node app is housed. Next, we have log in to Heroku through the CLI.

$ heroku login

Press any key to log in, this launches your default browser where you will log in successfully return to your terminal.

You will also need to log in to the container registry, basically, this is a service that Heroku offers to host our docker container.

$ heroku container:login

We have to create our Heroku app where our code will be held and built.

$ heroku create docker-nodejs-app

You should see something similar on your terminal

Before we build and deploy our app we need to change the port our Express server runs on. The default port was 3000 but when we build and deploy our app on Heroku we might get an error about our PORT already being used, so we need to create a condition whereby if Heroku does not give us a port we use our default 3000.