How to deploy an application to Heroku using Docker

Sergei Gannochenko
3 min readJul 17, 2019

--

Photo by Samuel Zeller on Unsplash

Personally, I am extremely fascinated with a container-based approach. I try to ship every application as a Docker container because in this case, the application becomes an infrastructure provider agnostic. So this time we will see how to deploy a dockerized application to Heroku — a famous cloud platform.

Step 1: The Code

My application’s code is as simple as 5 cents:

It is written in NodeJS, but it really does not matter, it could be anything else.

⚡ The first important thing: internally Heroku chooses a random port to hang the container on, so your application should look at PORT environment variable.

Let’s create a Dockerfile and put it next to the application:

Just FYI, package.json looks like this:

Step 2: Authentication

From this moment on I assume that we have already signed up at heroku.com and created our application. Let’s say, it will be my-first-unique-app (if this particular name was already taken, pick the other one). Heroku platform offers a really generous free plan which is more than enough not only for experimenting but for building an actual application that may go live.

We are going to need for Heroku CLI tool. The easiest way to get it (from my perspective) is to install it with curl:

Next thing: authenticate through the CLI tool in the interactive mode, using email and password you created while signing up at heroku.com:

This command will create a record in your ~/.netrc file.

There are two ways of how to proceed further. Originally, Heroku was made with the idea of strong bondings between itself and GitHub, so they offer to build a Docker image on their side after we commit to the GitHub repository. Alternatively, we can build an image on our side and then just push it to their own Docker image repository.

From my point of view, the second option gives more flexibility, so we will proceed this way.

Step 3: Deployment

Officially, we are supposed to run the following commands in order to build an image, and then push it to Heroku's own docker repository:

But, there are limitations (so far):

  • the second command should be executed in the same folder where Dockefile is located,
  • the file should be named as Dockerfile or Dockerfile.web, which is not cool in some situations.

If we run the second command with the-v flag, we won’t fail to notice, that this one is basically an alias for a sequence ofdocker commands. So, why don’t we exploit an advantage of using flexible Docker CLI tool itself?

First of all, we authenticate on Heroku’s own repository with this superposition of commands:

The heroku auth:token command returns the current token we got with heroku login . Username _ should stay like this (weird, but true).

Now, we build an image:

Here we are free to choose the Dockerfile to build from, as well as the build context and other options. The tag name should stay like this, there is no need to change it. The web code indicates that this particular container should be automatically exposed to the outer world through a technical domain.

Hopefully, if the image is built with no errors, we can push it:

As soon as the image is there, we can finally put it live by calling

Let’s give it some time to spin up and go have a coffee break. When we are back, visit https://my-first-unique-app.herokuapp.com/

Hopefully, we should see “Hello” message :)

Well, that is basically it. If something goes wrong, it is possible to view logs by typing

As usual, here is a proof-of-concept repository at your service. Enjoy!

Useful links:

Please drop some claps if this was helpful to you!

--

--

Sergei Gannochenko

Business-oriented software dev, in ❤️ with Tech. JS / JS stack, 15+ years in engineering