Dokku — Host your own Heroku App!

Deploy your service to EC2 in a heroku-ish way!

袁浩 Harry Yuan
4 min readSep 22, 2020

Say you have been working for some time, using lots of different cloud services (e.g. GCP App Engines, AWS ECS/EC2, etc.).

And you just miss the old days when you build your personal projects. Commit, push to heroku, and then it integrates and deploys the application for you.

Or simply, your boss ask you to launch a new service within a day, and you don’t have time to build another CI/CD flow.

This is the time when Dokku comes in handy.

What is Dokku?

dokku is another cute whale

Dokku is the smallest PaaS you can ever find in the world, powered by Docker. Also, it is an open source project.

Docker…Ku?

Yes. It works mostly like heroku, but this time

  1. On your own machine. (local or in cloud)
  2. Dokku runs your application as docker containers

Overview

You set up an EC2 Instance, install Dokku, and link to your project…. Bam! The old “Deploy on push” came back to life.

Getting Started

What you’ll need at least

  1. An instance on the cloud, and permissions to SSH to it.
  2. You application with git

Note that Dokku only supports these types of instances:

  • Ubuntu x64 — Any currently supported release
  • Debian 8.2+ x64
  • CentOS 7 x64 (experimental)
  • Arch Linux x64 (experimental)

First, SSH to your instance

$ ssh -i ~/.ssh/mykey.pem ubuntu@xxx.xxx.xxx.xxx

Install Dokku engine on it

$ wget https://raw.githubusercontent.com/dokku/dokku/v0.21.4/boots
trap.sh
$ sudo DOKKU_TAG=v0.21.4 bash bootstrap.sh

To verify your installation, type

$ dokku

If succeeded, it says

Usage: dokku [--quiet|--trace|--rm-container|--rm|--force] COMMAND <app> [command-specific-options]Primary help options, type "dokku COMMAND:help" for more details, or dokku help --all to see all commands.Commands:apps                     Manage Dokku apps
buildpacks Manages buildpack settings for an app
certs Manage Dokku apps SSL (TLS) certs
checks Manage zero-downtime settings
config Manages global and app-specific config ....

Add your public key to Dokku

Go back to your local terminal and find your local ~/.ssh/id_rsa.pub file.

If you don’t have one, create one using:

$ ssh-keygen

Once you have your id_rsa.pub, we need to add it to Dokku on your instance. This is in one line.

$ cat ~/.ssh/id_rsa.pub | ssh -i ~/.ssh/mykey.pem ubuntu@xxx.xxx.xxx.xxx sudo dokku ssh-keys:add key_of_my_app

To verify this

1. Go to your instance

$ dokku ssh-keys:listSHA256: YOURKEYANDSOMEHAMBUGERWITHNOSAUCENOPICKLES  # this should be your public key

2. Go back to local terminal, and type

$ ssh dokku@xxx.xxx.xxx.xxx

Succeeded if outputs these

Usage: dokku [--quiet|--trace|--rm-container|--rm|--force] COMMAND <app> [command-specific-options]Primary help options, type "dokku COMMAND:help" for more details, or dokku help --all to see all commands.Commands:apps                     Manage Dokku apps
buildpacks Manages buildpack settings for an app
certs Manage Dokku apps SSL (TLS) certs
checks Manage zero-downtime settings
config Manages global and app-specific config ....

Good, now we can push commit to Dokku on our instance!

NOTE: Adding a new user

If you are adding a new user to access dokku, put their key in a file e.g. himarks_pubkey

ssh-rsa AAAxxxxxxx himark@xxx-MacBook-Pro.local
# Note here is a line break

then you can run command again

cat ./himarks_pubkey | ssh -i ~/.ssh/mykey.pem ubuntu@xxx.xxx.xxx.xxx sudo dokku ssh-keys:add key_himark

Create application

Let’s create application from EC2 terminal.

$ dokku app:create myrailsapp

Then to back to local, add dokku as a git remote:

$ git remote add dokku dokku@xxx.xxx.xxx.xxx:myrailsapp

Done!

Congratulations! You can push commits to dokku now. It will do the rest and deploy your application on this instance.

$ git add .
$ git commit -m 'feat: introducing dokku, who is my friend'
$ git push dokku master

How do I access the deployed application?

You need to configure your instance to allow public access and better host it with a domain name.

For example, when using EC2 on AWS, you can do that via setting the inbound rules for HTTP coming from all/specific IPs.

Dokku has plugins!

Yes, you hear it. It has lots of plugins to use! Just like heroku’s add-ons.

Say you want a postgres database on the same instance, you can use Dokku’s postgres plugin.

$ sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git

$ dokku postgres:create railsdatabase
# creates a postgres service with the name railsdatabase

Or, say you want to setup SSL for it, you can use the letsencrypt plugin.

You can find all the plugins here

https://github.com/dokku/dokku/blob/master/docs/community/plugins.md

The rest is up to you.

Go on and explore with dokku !

--

--

袁浩 Harry Yuan
袁浩 Harry Yuan

Written by 袁浩 Harry Yuan

Software Engineer | Ruby on Rails 喜歡學習前後端技術。希望文章白話到阿嬤都看得懂。

No responses yet