How to deploy NodeJS application to Heroku using docker

Create a project in your local

>> npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (heroku-nodejs-docker) 
version: (1.0.0) 
description: 
entry point: (server.js) 
test command: 
git repository: (https://github.com/xx/Heroku-NodeJs-docker.git) 
keywords: 
author: 
license: (ISC) 
About to write to /Users/xx/Documents/GitHub/Heroku-NodeJs-docker/package.json:

{
  "name": "heroku-nodejs-docker",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/xx/Heroku-NodeJs-docker.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/xx/Heroku-NodeJs-docker/issues"
  },
  "homepage": "https://github.com/xx/Heroku-NodeJs-docker#readme"
}


Is this OK? (yes) 

Create simple NodeJS application

server.js

const express = require('express');
const app = express();

app.get('/', (req, res) => res.send("<h1>Hello world</h1>"));

app.listen(process.env.PORT || 8080);

Write your docker file

Build the image on top of light weight linux (alpine) and install express.

Dockerfile

FROM node:15.8.0-alpine3.10

WORKDIR /app
ADD server.js package.json ./

RUN npm install express

RUN npm install
CMD node server.js

Login to heroku cli

>> heroku login

Login to heroku container

>> heroku container:login

Create target application

>> heroku create <YOUR_APP_NAME>

Connect you local git to remote heroku git

>> heroku git:remote -a <YOUR_APP_NAME>

Build the image and push to Container Registry

>> heroku container:push web

Then release the image to your app

>> heroku container:release web

Verify the nodeJS application in your browser

>> heroku open

Logout the container when you're done

>> Logout heroku container

Last updated