Deploying a Node.js Docker container to Heroku

In the last post titled Developing Node.js applications in Docker, we have seen how we can work with Docker as our development environment.

In this post, we are going to take the app that we have built named docker-node-app and deploy it to Heroku as a Docker container.

The sample code used in this post can be found here: docker-node-app.

So, let’s get started.

Setup Heroku

The first thing we need to do is download and install Heroku tool belt on our local machine.

Download Toolbelt

You can follow the instructions given here: Install Heroku Toolbelt to install Heroku tool belt for your Operating System.

Setup Account

If you do not have a Heroku account, you can signup here.

Login

Now that we have Heroku installed, we will login. Open a new command prompt/terminal and run

Next, we need to login to the container registry. Run

Once we have successfully logged in to Heroku & Heroku container registry, let’s work with Docker.

Setup Docker

The first thing we are going to do is download Docker for our Operating System.

Download

To Install Docker on our machine, navigate to here & download the software for your OS.

Test Download

Once Installed, you can open up a command prompt/terminal and run  docker ps and you should see something like

At this point, we have both Docker as well as Heroku installed on our machine.

Next, we are going to containerize our app and deploy it to Heroku.

Setup docker-node-app

If you have not already done, please go through Developing Node.js applications in Docker, where we see how to develop Node.js applications using Docker. We are going to begin at the ending of that post.

Clone docker-node-app

Now, we are going to clone docker-node-app from https://github.com/arvindr21/docker-node-app. From anywhere on your machine, run

And then  cd docker-node-app .

Fix Port

We need to make a change to the Dockerfile  to fix the port. While deploying to Heroku, port would be assigned by Heroku.

Open Dockerfile  and update it as shown below

That is it, we are all set to deploy our awesome container app to Heroku.

Deploy to Heroku

Now, we are going to deploy to Heroku. We are going to follow the below steps

  1. Heroku create new app (one time)
  2. Heroku container push – Create and push the container to Heroku Container Registry
  3. Heroku container release – Release/run the container app
  4. Heroku open – Launch the app

From inside  docker-node-app run

This will create a new Heroku app.

Next, we will push the container

Now, we will run the container

And finally open the deployed app run

And we should see

Voila! Our Node.js that we have containerized while development is now deployed to Heroku with ease.

Development & Deployment simplified!

PS: I am shutting down the above Heroku app, as it does not have any thing specific to showcase.

Hope you got an idea as to how we can deploy Node.js containerized application to Heroku.


Thanks for reading! Do comment.
@arvindr21