Let’s make Heroku deployments with Nodejs on Docker look easy
Tags: Nodejs, Heroku, Docker

Getting Alpine Node installed

That’s all the requirements we need for now! Now let’s get started. First of all, creating a traditional NodeJS Docker container from my experience takes WAY too much space. A simple project that takes 1.3 MB was when converted into a Docker container, takes a whopping 1.2GB! So we are going to use Alpine-node (https://github.com/mhart/alpine-node) to reduce that to around a mere 70MB. Run this to see if you have alpine-node installed.

docker run --rm mhart/alpine-node:14 node --version

If you don’t have it installed, then no worries, this command will install it automatically for you 🤗. Run it again then to see the version (should show v14.15.1).

Setting up the project

Let’s start making the project. In your bash/PowerShell/cmd terminal run mkdir <projectname> and cd <projectname> with <projectname> being whatever you want. Inside this, we can npm init -y to create package.json, and npm install to create a package-lock.json. Open up this folder on your preferred editor (for me it’s VScode, so I ran in the terminal code . ).

Create a new file called index.js. For this tutorial, we are going to be using express, but you can easily adjust your needs. Put in something like this:

const express = require("express") const app = express(); app.get("/", (_req,res) => { res.send("Nodejs on Docker on Heroku! 😁") }) const port = process.env.PORT || 8080 app.listen(port, () => { console.log(`Listening on port ${port}`) })

Adjust the code to whatever you want!

Next, we need to create Dockerfile , so go ahead and make that. Inside, put this or something like it

FROM mhart/alpine-node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY index.js ./ EXPOSE $PORT CMD [ "node", "index.js" ]

We don’t want Docker to build our app with node_modules (like pushing with git), so create a .dockerignore and put in it node_modules . I forgot! We need to install express for our package.json and package-lock.json! Run npm install express to install that. There we go, we are done with the code.

Terminal time!

Time to go back to the terminal! On the same path as the project, we can now run some commands to make this visible on the web. Run docker build -t <app> . <app> will be what you want your docker contaire to be named.

If no errors arise then run docker run -p 8080:8080 -d --env PORT=8080 <app> , with of course substituting <app>. If you see an error saying something about the port being taken then change the port variable in index.js to something more random, and do the above command except substituting 8080 with that random number.

Now, if you open up a browser and go to localhost:8080 (or whatever port it’s on), you should see the message from index.js on the screen!

Time to deploy the app.

Run heroku login and heroku container: login to login to Heroku. Run heroku create <webapp> to create an app, substituting <webapp> with what you want your app to be named. Let’s push (just like git :D) this container to Heroku. heroku container:push web -a <webapp> . Hopefully, no errors will show! 🤞

Release the changes with heroku container: release web -a <webapp> . Open the app with heroku open -a <webapp> to open your project, and you should see your project (in a Docker container) published on the web! 🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉🎉

Thank you for reading this! If you’re stuck don’t hesitate to reach out to me in the comments section!