DEV Community

Anuj Upadhyay
Anuj Upadhyay

Posted on

Docker Images to Heroku

Hey Devs,

So I was wondering how to push my docker image to Heroku.
Turns out it wasn't that hard.
Below is a walk-through of what I did.

So for the starters, I created an express app.

mkdir express-docker-up
cd express-docker-up
git init
npm init -y
npm install --save express

Never forget the .gitignore file to keep the node_modules folder out of you git history. (If the your machine's node_modules get cloned to a different machine, errors may dial in)

/node_modules/

add the index.js

const express = require('express')
const app = express()
const port = process.env.PORT

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Create the Dockerfile

FROM node:10-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY index.js .
CMD ["node", "index.js"]

Create the heroku.yml

build:
    docker:
      web: Dockerfile

Let's start working on heroku now

heroku login

If you don't have a pre-made app, create one

heroku create

Set the stack as container for the app

heroku stack:set container --app APP_NAME

now to commit and push

git add .
git commit

Don't forget to give a meaningful commit message
Check if heroku is present in your remote

git remote

If not, make sure you have the heroku remote set

git remote set heroku <heroku git repo(check your app settings on the web)>

OR

heroku git:remote --app APP_NAME

And now push

git push heroku master

Now you wait, and when it's done, just go to the app url.
Voila!

I've uploaded this code to my github

Top comments (0)