How to Run Dockerized Apps on Heroku… and it’s pretty sweet

Every cloud provider is supporting Docker now, but I have to say, Heroku nailed it. This seems to have run under the radar for the most part since I haven’t heard anyone talk about it. It reminds me of the good ol’ days when Heroku first started. Their entire pitch and tagline was git push heroku master, then magic happened. And now they’ve done the same with docker push. The only other service that is as simple is Hyper.sh, but Heroku has a few advantages like a built in load balancer, the ability to scale and many years of working out the kinks.

Dockerizing an App

First, let’s start with a simple Hello World web app. This is in Go, but could be any language.

And we need a Dockerfile to build our Docker image (requires Docker 17.05+):

You can test this if you want with a simple:

docker build -t treeder/myapp .
docker run --rm -it -p 8080:8080 treeder/myapp

Then surf to http://localhost:8080 to see that it works.

Now I’ll show you two ways to deploy your app to Heroku.

Push to Heroku with Heroku CLI

This assumes you already have the Heroku CLI installed. Install the Heroku container registry plugin for the cli:

heroku plugins:install heroku-container-registry

Login to the registry:

$ heroku container:login

Now create a Heroku app:

heroku create

That command will return an app name, copy it to use it for the next command.

heroku container:push web --app ${YOUR_APP_NAME}

And boom, that’s it. It will build your image and push it to Heroku. Check out your live app with:

heroku open --app ${YOUR_APP_NAME}

Push to Heroku with Docker only

The following is good if you’re deploying to Heroku with your CI tool and/or don’t want to install the Heroku CLI.

You’ll need to get your Heroku token which you can get from the cli on any machine that’s logged into Heroku:

heroku auth:token

Grab the returned token and from this point on, you don’t need Heroku CLI anymore.

Use the token to login to the Heroku Registry:

docker login --username=_ --password=${YOUR_TOKEN} registry.heroku.com

Note: The email and username are actually the underscore, don’t change those.

And then just push your image:

docker build -t registry.heroku.com/${YOUR_APP_NAME}/web .
docker push registry.heroku.com/${YOUR_APP_NAME}/web

Now surf to your Heroku app again and you’ll see it live.

Conclusion

Pretty dang awesome.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.