The Ultimate Guide to Dokku and Ruby On Rails 5

Image for post
Image for post

TL:DR; The Heroku Hobby plan will be more efficient in terms of your personal ROI.

I like the simplicity of Deploying an application to Heroku. Recently I checked the Dokku (PaaS alternative).

Pay attention:

  1. Dokku doesn’t support Multi-host.
  2. Dokku doesn’t support Multitenancy. (It can, but there are better solutions for this)

This tutorial will be based on AWS, but the similar process can be done on other VPS services, for example: DigitalOcean

Setup

  1. Create AWS instance. Let’s assume your public ip is: 123.456.789.100
  2. Create DNS record in Cloudflare (or your own DNS manager)
  3. ssh into the new server: ssh root@123.456.789.100
  4. Visit https://github.com/dokku/dokku/releases and install dokku via bootstrap script as described:

5. Create Dokku application: dokku apps:create myapp . Change myapp to your application name.

6. Set locales sudo dpkg-reconfigure locales

7. Add a swap file to avoid memory problems.

Check swap configuration

Mount swap on server reboot

Add the following line and save

Configure Dokku

Next steps should be done from your local computer

  1. Open your browser and visit: http://123.456.789.100
  2. Add your public ssh key. Copy to clipboard and paste in Dokku setup pbcopy < ~/.ssh/id_rsa.pub
  3. Leave other settings unchanged and press FINISH SETUP
Image for post
Image for post
dokku setup

4. Open your Rails project folder and add Dokku to your git settings, as following (change the IP and application name):

5. Let’s deploy our application git push dokku master

If everything is configured correctly, you should get a notice similar to this

Image for post
Image for post
completed setup

Setup SSH for easy access

  1. Add secret.pem file your got from AWS to ~/.ssh/
  2. Set read-only access: git chmod 400 ~/.ssh/secret.pem
  3. Add a record in ~/.ssh/config

Now, you can access your server via ssh my-awesome-server

Application Configuration

  1. SSH to your dokku server and run: dokku config myapp This method is similar to heroku config
  2. Add your ENV values as following:

Note: no need to set SECRET_KEY_BASE because it is done by buildpack

3. Setup Postgresql (dokku plugin)

If you need to import existing DB, do the following

4. Setup Redis (dokku plugin)

5. Dokku uses Procfile similar to Heroku

Procfile example

6. Database migration

If you want automatically migrate after each deploy, add app.json to the root folder of your application. See postdeploy example below:

Setup Domain

Open your DNS provider and set the record

Image for post
Image for post
DNS Rule

Visit: http://mydoman.com You should see your rails application working properly.

Setup SSL certificate using Let’s encrypt

Adding SSL certificate from https://letsencrypt.org

  1. Add dokku plugin

2. Setup SSL

CRON Jobs with Dokku

There is no cron job using dokku. However, you can setup cron jobs on the host, and run dokku run <app> <COMMAND> to execute your task in the container.

Read more here

Example:

Miscellaneous useful commands

  • Run Rake tasks: dokku run myapp rake db:migrate
  • Open Rails console: dokku run myapp rails c
  • Tail Logs: dokku logs myapp -t
  • NGINX logs
  • Restart the app: dokku ps:restart myapp
  • Check docker process: docker ps
  • Remove unused Dokku: dokku cleanup

Other

References:

Written by

Making a positive impact

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store