> For the complete documentation index, see [llms.txt](https://owen31302.gitbook.io/github-education/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://owen31302.gitbook.io/github-education/heroku/how-to-deploy-nodejs-application-to-heroku-using-docker.md).

# How to deploy NodeJS application to Heroku using docker

### Create a project in your local

```bash
>> 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

```javascript
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

```javascript
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

```javascript
>> heroku login
```

### Login to heroku container

```javascript
>> heroku container:login
```

### Create target application

```javascript
>> heroku create <YOUR_APP_NAME>
```

### Connect you local git to remote heroku git

```bash
>> heroku git:remote -a <YOUR_APP_NAME>
```

### Build the image and push to Container Registry

```bash
>> heroku container:push web
```

### Then release the image to your app

```bash
>> heroku container:release web
```

### Verify the nodeJS application in your browser

```bash
>> heroku open
```

### Logout the container when you're done

```bash
>> Logout heroku container
```

{% hint style="info" %}
You cannot deploy redis to heroku docker since they did not expose other ports.

<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>
{% endhint %}
