Sunday, 1 July, 2018 UTC


Summary

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
➜  ~ heroku login
heroku: Enter your login credentials
Email [[email protected]]: [email protected]
Password: ******************
Logged in as [email protected]
Next, we need to login to the container registry. Run
➜  ~ heroku container:login
Login Succeeded
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
➜  ~ docker ps   
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
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
➜  ~ git clone https://github.com/arvindr21/docker-node-app
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
# A node.js v8 box
FROM node:8

# Who(m) to blame if nothing works
MAINTAINER [email protected]

# Create a working directory 
RUN mkdir -p /usr/src/app

# Switch to working directory
WORKDIR /usr/src/app

# Copy contents of local folder to `WORKDIR`
# You can pick individual files based on your need
COPY . .

# Install nodemon globally
RUN npm install -g nodemon

# Install dependencies (if any) in package.json
RUN npm install

# Expose port from container so host can access $PORT
EXPOSE $PORT

# Start the Node.js app on load
CMD [ "npm", "start" ]
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
➜  docker-node-app git:(master) ✗ heroku create                   
Creating app... done, ⬢ conservative-loonie-82469
https://conservative-loonie-82469.herokuapp.com/ | https://git.heroku.com/conservative-loonie-82469.git
This will create a new Heroku app.
Next, we will push the container
➜  docker-node-app git:(master) ✗ heroku container:push web
=== Building web (/Users/aravulavaru/Documents/blog/code/docker-node-app/Dockerfile)
Sending build context to Docker daemon  79.87kB
Step 1/9 : FROM node:8
 ---> ba6ed54a3479
Step 2/9 : MAINTAINER [email protected]
 ---> Using cache
 ---> 260d8c31364f
Step 3/9 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> 4651ae6b3eb8
Step 4/9 : WORKDIR /usr/src/app
 ---> Using cache
 ---> 9123d4de2b66
Step 5/9 : COPY . .
 ---> b062ab67904a
Step 6/9 : RUN npm install -g nodemon
 ---> Running in bf0327bc6d79
/usr/local/bin/nodemon -> /usr/local/lib/node_modules/nodemon/bin/nodemon.js

> [email protected] postinstall /usr/local/lib/node_modules/nodemon
> node bin/postinstall || exit 0

Love nodemon? You can now support the project via the open collective:
 > https://opencollective.com/nodemon/donate

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/nodemon/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ [email protected]
added 232 packages in 13.963s
Removing intermediate container bf0327bc6d79
 ---> 54e163e839ba
Step 7/9 : RUN npm install
 ---> Running in 03c2be1c95b6
up to date in 0.084s
Removing intermediate container 03c2be1c95b6
 ---> a5df93ca625f
Step 8/9 : EXPOSE $PORT
 ---> Running in 2114eaf27011
Removing intermediate container 2114eaf27011
 ---> d3f070661c29
Step 9/9 : CMD [ "npm", "start" ]
 ---> Running in 6b6c34bfcced
Removing intermediate container 6b6c34bfcced
 ---> b48b21cca6c4
Successfully built b48b21cca6c4
Successfully tagged registry.heroku.com/conservative-loonie-82469/web:latest
=== Pushing web (/Users/aravulavaru/Documents/blog/code/docker-node-app/Dockerfile)
The push refers to repository [registry.heroku.com/conservative-loonie-82469/web]
c3d6dd6300e1: Pushed 
3bffc6518e64: Pushed 
04ba1a78c8fc: Pushed 
737f5105920d: Pushed 
d10a2225c5fa: Pushed 
f8f3158f1d2f: Pushed 
edff94ac6dc8: Pushed 
7616b19d2166: Pushed 
d714f65bc280: Pushed 
fd6060e25706: Pushed 
d7ed640784f1: Pushed 
1618a71a1198: Pushed 
latest: digest: sha256:2af12f04df6e593373961fd39e6a56ebffc1a3513b621fef3721b8b6668dfe03 size: 2841
Now, we will run the container
➜  docker-node-app git:(master) ✗ heroku container:release web
Releasing images web to conservative-loonie-82469... done
And finally open the deployed app run
➜  docker-node-app git:(master) ✗ heroku open
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
The post Deploying a Node.js Docker container to Heroku appeared first on The Jackal of Javascript.