Deploy Docker Containers to Heroku from GitHub

mohit thakur
Geek Culture
Published in
2 min readJun 3, 2022

--

Are you an Heroku Fan , I personally enjoy deploying my prototypes on Heroku and its works great out of the box using GitHub Repositories.

What about my docker applications , recently i deployed Font-end + Back-end Monolith app using docker to Heroku.

Who is the audience ?

You should be familiar with Modern JavaScript Frameworks , GitHub— we will use NodeJs application as an example.

You can do this in 3 simple steps

  1. Create your node application — (express)
  • create app folder
  • npm i express
  • create file main.jsand copy the code below
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})

Test it by running node main.js . If you get above message all is good to go.

Step 2 — Containerize your application

  • Create Docker File — Dockerfile (no file extension)
FROM node:14-alpine3.12
ADD ./app /app # we copied our app to container
# change working directory
WORKDIR /app
RUN npm install
# start application
CMD [ "node", "main.js" ]

Step- 3 Push your code on github

  • If you are new to GitHub , please follow the GitHub docs
  • Go to Actions tab in your GitHub repository.
click actions tab
  • Create new workflow
click new workflow button
  • Add following code to action.yml file
name: Deployon:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: akhileshns/heroku-deploy@v3.12.12 # This is the action
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "app-name" // name of app
heroku_email: "mohit.XXXX@gmail.com"
usedocker: true // make sure to be true
docker_build_args: |
NODE_ENV
env:
NODE_ENV: production
SECRET_KEY: ${{ secrets.MY_SECRET_KEY }}
  • inside env: section you can provide name of GitHub secrets for protected info such as Heroku token
  • You need to login to Heroku and generate Secure Token. here
heroku_api_key: ${{secrets.HEROKU_API_KEY}}

Note: — You need to create a token from Heroku to be used in GitHub, so that GitHub can deploy your application.

How to Test?

Just make some changes and commit, it should be deployed directly to Heroku server.

https://app-name.herokuapp.com/

Please Leave comments if you are stuck will try to improve sections that might be too short for beginners.

--

--