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

Search for a command to run...

"heroku container:release -a docker-nodejs-app web", instead of "heroku container:push web". -> for release part
"heroku container:push -a docker-nodejs-app web" works for me, instead of "heroku container:push web".
Setup Requirements Before you go through this fun tutorial ensure: You at least have an understanding of javascript and terminal commands. You should know what Docker is and in theory how it works. You have should node and npm installed on your comp...
Recently I started building tools that will help me automate my app deployment process easier and cheaper, to do that I knew CLIs are definitely going to be involved one way or another. So I started building two CLIs: Simp CLI: It's like Makefile ju...

Let's jump into it... fast. You got here because you want to setup Dokku and the other ways don't work... let's goooo I will setup for Dokku only Point your domain to the VPS of choice Spin up new VPS server here: Digital Ocean Linode Buy a dom...

So what is Dokku, another bit, another TL;DR; Let's jump into it. You might know Heroku, (my opinion) the greatest platform to ever make it easy to deploy server and client-side applications so fast and easy. Well Heroku is a business and they have t...

Okay for this one I highly recommend, the speeds is just amazing. I loved it. I am gonna do a comparison real quick, between browser-image-compression and react-image-file-resizer. browser-image-compressionreact-image-file-resizer Really fastCo...

I recently got interested in compressing images on the client-side for a couple of reasons: Reduce my cloudinary bandwidth and storage usage Users were complaining about long waits while uploading images So I ventured to look for a library that wil...

This is the third part of the series for Docker meets NodeJS.
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
Note: Ensure you go through the first part of this series and the second part too.
Note: Remember to clone the
docker_nodejs_apprepo here
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.
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.
To install the CLI on Mac you can do it in two ways:
homebrew:$ brew tap heroku/brew && brew install heroku
To install it on Windows you just need to know which type works with your computer, is it a 32-bit or 64-bit
For Ubuntu, you will need to install it with snap:
$ sudo snap install --classic heroku
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.
Change the line you set your port default to this:
app.set("port", process.env.PORT || 3000);
Your app.js file should look like this
"use strict"; // Ensures our code is compiled in strict mode
// Lets import our web framework
var express = require("express");
var mongoose = require("mongoose");
// Initialise our app
const app = express();
// Lets set our port
/**
* The default port number is `3000` if Heroku does not provide us a port
* Take note on that as we will come to that.
*/
app.set("port", process.env.PORT || 3000);
// Connect to database
mongoose.connect("mongodb://mongo:27017/docker_nodejs_app", {
useNewUrlParser: true,
useCreateIndex: true
});
mongoose.connection.on("open", err => {
if (err) console.log("Error connecting to our mongo database");
console.log("Connected to mongo database successfully");
});
/**
* To ensure works as it should we will create a
* simple endpoint to return a json response
*/
// Define our json response
const data = {
blog_name: "docker_nodejs_app",
blog_author: "wachira (tesh254)",
blog_author_twitter: "@wachira_dev"
};
// Define out GET request endpoint
app.get("/", (req, res) => {
res.status(200).json(data);
});
// Initialize our server
app.listen(app.get("port"), () => {
console.log(`Server listening on port ${app.get("port")}`);
});
Next, we will build our image and push to Container Registry
$ heroku container:push web
You should see something similar on your terminal

What happens in the background?
Dockerfile and the docker-compose.yml configuration.built image to our created app through git.Next, we need to release our app so that it can be accessible to the whole world.
$ heroku container:push web
// or
$ heroku container:push -a docker-nodejs-app web
This should appear in your terminal

You have successfully deployed your app on Heroku🎉🍾🎊🙌
To summarise, in this awesome post we have:
In the next part:
Link to repo https://github.com/werickblog/docker_nodejs_app
Link to app https://docker-nodejs-app.herokuapp.com/
Link to download NodeJS https://nodejs.org/en/download/
Link to download Docker https://www.docker.com/get-started
Understanding what docker is https://docs.docker.com/engine/docker-overview/