← Back to Blog

Setup Dokku (a free Heroku alternative) and Deploy a Ruby on Rails 7 Application

August 20, 2023 Platform as a Rails Bootstrap

Create your own Heroku clone by using Dokku

Heroku was a popular solution for quick and easy deployments for Ruby on Rails developers. It was handy for testing and learning purposes with its free tier option, offering Platform as a Service (PaaS) solutions for everyone. Unfortunately, Heroku dropped its free tier solution, making it necessary to pay for every service offered by Heroku. On the hunt for a new solution to quickly and easily deploy Ruby on Rails applications, I found a Heroku alternative called Dokku.

Dokku is a free open-source Platform as a Service solution that allows you to build your own Heroku clone on your server. It allows you to do git push-based deployments directly to your own Dokku instance running on your server with useful auto-deployment features. Dokku supports pretty much anything which could have been deployed to Heroku.

The main use case I’m covering with Dokku is for testing and development, however, I don’t see any problems running smaller applications also in production using Dokku.

This guide shows you how to set up Dokku on your server and explains how to deploy and configure a Ruby on Rails 7 application (including Postgres, Let’s Encrypt and Database Encryption) as well as how to work with your application (executing commands, logging and configuration).

Prerequisites

What do you need?

Part 1 - Install and Setup Dokku

The first part of this article explains how to install and set up Dokku on your server. For this, the following steps have to be executed:

  ssh root@your\_server\_ip
  wget -NP . https://dokku.com/install/v0.31.0/bootstrap.sh  
  sudo DOKKU\_TAG=v0.31.0 bash bootstrap.sh
  dokku domains:set-global acme.com
  dokku domains:set-global <your servers IP address>  
  dokku domains:set-global <your servers IP address> # add `.sslip.io` for subdomain support

That’s pretty much it for the Dokku setup. After these steps, your Dokku instance is ready for its first deployment. If you need more instructions, check their excellent documentation here.

Part 2 - Ruby on Rails 7 Application Deployment

You can skip the first two steps if you already have an existing application. If not, follow along.

  rails new myapp && cd myapp
  git add .   
  git commit -m "chore: Initial commit"
  git remote add dokku dokku@<global dokku domain or IP address>:myapp
  git push dokku master

After a successful deployment, you can access your Rails application by visiting: http://your-domain.com. Dokku also prints the deployment log to your terminal so you can follow along. In the end, the URL of your deployed application is printed for you to access your application.

Part 3 - Database Configuration

To get your database set up correctly the following steps are necessary. For the sake of this tutorial, it’s assumed that you use Postgres as your production database.

  production:  
    adapter: postgresql  
    encoding: unicode  
    url:   
    pool: 5
  dokku plugin:install https://github.com/dokku/dokku-postgres.git
  dokku postgres:create mydatabase  
  dokku postgres:link mydatabase myapp

Additionally, if you work with the Rails migration system, it’s wise to set up an automatic migration command for each deployment. For this, add an “app.json” file to your Rails project’s root path, containing the following lines:

{  
"name": "<Name of Application>",  
"description": "<Description of your application>",  
"scripts": {  
  "dokku": {  
    "postdeploy": "bundle exec rake db:migrate"  
  }  
}  
}

This will automatically execute the migration command after every deployment. You can also configure any other command you might need here. Commit and push this file and your deployment should execute the migration automatically.

If you want to use Rails Active Record Encryption, you need to configure your Rails deployment with the “RAILS_MASTER_KEY”, containing the content of the “master.key” file. This can be done by adding the environment variable to the deployment by using the following Dokku command:

dokku config:set <name-of-application> RAILS\_MASTER\_KEY=<content of master.key>  
dokku config:show <name-of-application> # shows all environment variables set for the application

With this command, it’s also possible to set up any other environment variable you might need in production.

Part 4 - Redis

If your Rails application uses Redis as a cache or session store, Dokku offers a solution for that. As with the Postgres setup, you need to install a plugin and configure some environment variables:

  dokku plugin:install https://github.com/dokku/dokku-redis.git redis 
  dokku redis:create myredis  
  dokku redis:link myredis myapp

Part 5— SSL & Let’s Encrypt Setup

If you need SSL for your application, Dokku has a Let’s Encrypt plugin you can easily integrate. For this, the following steps are necessary.

  dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git 
  dokku config:set --no-restart <appname> DOKKU\_LETSENCRYPT\_EMAIL=your@email.tld
  dokku letsencrypt myapp
  dokku letsencrypt:cron-job --add myapp

Now you are all set and your application should be accessible via HTTPS.

Tips and Tricks

Conclusion

The experience of setting up Dokku and deploying a Ruby on Rails 7 application is pretty seamless. It offers everything you expect from a PaaS solution and can be easily configured and extended to your application needs. This tutorial covers the basic configuration as well as the deployment instructions necessary.

For further information, check the reference links down below.

Happy Coding

Michael

Thanks for reading this article. If you want, follow me on Twitter.

Related Links