Deploy A Docker Container Free (on Heroku)

Justin Krup
2 min readMar 31, 2019

--

I was a big fan of https://zeit.co/now V1, in particular for the ability to deploy any Docker container to a public URL (with some restrictions) starting at $0!

There’s a tremendous amount of utility in being able to deploy any arbitrary application for free. Many hobbies or side-projects don’t warrant monthly costs, and run in a unique way. Making Docker support a great way of covering many use cases. Get your app running in a docker container, and you can deploy it.

Unfortunately, Now is deprecating their V1 and arbitrary docker container support. So for those who want the ability to quickly get a Docker container up and running on a public URL, I want to share a solution using Heroku.

Prerequisites

If you want to follow along you’ll need a Heroku account and the following:

Deploying the App

If you already have a repo with a Dockerfile you can just hop in that repo, but I’ll be demonstrating this using a super tiny ExpressJS example (source available here: https://github.com/jkrup/express-docker-up).

In a fresh directory we want to install ExpressJS.

Alternatively feel free to git clone https://github.com/jkrup/express-docker-up and skip a few steps.

Then we’ll make an index.js for our server application code. That runs a server on a port specified by the PORT env variable.

Mostly copied from http://expressjs.com/en/starter/hello-world.html, but notice the port variable

Now let’s make a Dockerfile for this app.

First make sure you’re logged in Heroku on the CLI with a heroku login (you won’t need to do this every time, but just once to make sure you’re logged in).

Then run the following:

heroku container:login
heroku create
heroku container:push -a APP_NAME
heroku container:release -a APP_NAME
  • heroku container:login logs you into the container aspect of Heroku. Or something like that, I think? I’m not super sure why it’s necessary if you’re already heroku login'd to be honest, but I got an error and running that fixed it.
  • After running heroku create that should give you a url https://stark-temple-72071.herokuapp.com the APP_NAME in that case is stark-temple-72071.
  • heroku container:push builds the docker container locally and pushes the image to Heroku so it has it. Note this step will probably take the longest.
  • Finally heroku container:release tells Heroku to release that container to the world.

And we’re done! The app should be up at https://APP_NAME.herokuapps.com.

Making it Easier?

If don’t want to spend the rest of your days re-googling this post to remember the exact steps you need to do, then lucky for you I made an npm package that just runs those 4 commands for you.

So instead of typing out heroku container:..., once you npm i -g docker-up you can just do docker-up or docker-up --update to push a new version on the same appname, rather than heroku create a new Heroku app/URL.

--

--

Justin Krup

Full-Stack Web • Deployments • Docker • Unity3D • Entrepreneur • 中文 • 日本語