Github Education
  • Github學生免費服務推薦
  • name.com免費網域申請
  • SendGrid - Email Service
  • Configcat - Global config setting
  • Transloadit - File conversion to cloud storage
  • i18n 管理平台 - lingohub
  • Push Notifications - PushBots
  • BrowserStack
  • Page
  • Ai
    • Stable diffusion
    • Changing Images Backgrounds
  • Copy of Crypto currency games
    • Gods Unchained
  • Digital Ocean
    • How to create cloud instance and access it on Digital Ocean
    • Deploy nodeJs on DigitalOcean droplet using docker
    • Deploy Redis to your local
  • Heroku
    • Heroku Cli
    • How to deploy Hello world to Heroku using docker
    • How to deploy NodeJS application to Heroku using docker
  • APIs
    • Google Geocoding API
    • FourSquare
    • Building APIs with Swagger
  • Util
    • Google Cloud Storage - Object storage
    • Google Search Console
    • Google Sign-in with Angular Front End
    • Google Sign-in with Nodejs Backend
    • Github Package
  • 推薦課程
  • Currently interested in
  • Useful info
  • Become a Front End Web Developer | Udacity
    • 2. CSS, Website Layout, Website Components
      • Lesson 2: CSS
      • Lesson 3: Flexbox
      • Lesson 4: CSS Grid
      • Lesson 5: Creating Responsive Layouts
      • How to use Adobe Design tokens - Spectrum
    • 3. Javascript & The DOM
      • Lesson 1: Syntax
      • Lesson 2: The Document Object Model
      • Lesson 3: Creating Content with JavaScript
      • Lesson 4: Working with Browser Events
  • Some tips
    • Github Blame View
  • Free
    • Openshift(WIP)
Powered by GitBook
On this page
  • Create a project in your local
  • Create simple NodeJS application
  • Write your docker file
  • Login to heroku cli
  • Login to heroku container
  • Create target application
  • Connect you local git to remote heroku git
  • Build the image and push to Container Registry
  • Then release the image to your app
  • Verify the nodeJS application in your browser
  • Logout the container when you're done

Was this helpful?

  1. Heroku

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

You cannot deploy redis to heroku docker since they did not expose other ports.

PreviousHow to deploy Hello world to Heroku using dockerNextGoogle Geocoding API

Last updated 4 years ago

Was this helpful?

https://stackoverflow.com/a/44548327/3117474
https://stackoverflow.com/questions/63229364/cant-connect-to-dockerised-redis-on-heroku
https://stackoverflow.com/questions/52814219/how-to-find-process-env-port-port-number-in-heroku