Dokku is an open-source Platform as a Service that you can self-host. It's similar to Heroku but allows you to maintain control of your own infrastructure. This guide will show you how to deploy a Ruby on Rails application to Dokku.
- Access to a virtual private server.
- A Ruby on Rails application that you'd like to deploy.
- Knowledge of Git. You'll need to push your application to Dokku using Git.
Install the latest stable version of Dokku. Here is the snippet. Follow 1-2 points from the link.
sudo dokku plugin:install https://github.com/dokku/dokku-postgres.git
sudo dokku plugin:install https://github.com/dokku/dokku-redis.git redis
- Download dokku plugin
dokku plugin:install https://github.com/dokku/dokku-letsencrypt.git
- Config letsencrypt
dokku config:set --global DOKKU_LETSENCRYPT_EMAIL=your-email@domain.tld
- Enable auto-renewal
dokku letsencrypt:cron-job --add
dokku apps:create app_name
- Create PostgreSQL service
dokku postgres:create app_name
- Set DATABASE_URL env variable by linking the service
dokku postgres:link app_name app_name
- Create a Redis service
dokku redis:create app_name
- Set REDIS_URL env variable by linking the service
dokku redis:link app_name app_name
For example you can set RAILS_MASTER_KEY env
dokku config:set app_name RAILS_MASTER_KEY=221341234123421342134
You can also change RAILS_ENV
dokku config:set app_name RAILS_ENV=staging
dokku config:set app_name RACK_ENV=staging
To instruct dokku on deploying the Rails application, generate a Procfile in the app's root directory.
- Create Procfile in you root app folder
touch Procfile
- Now populate this file with the necessary commands:
web: bundle exec puma -C config/puma.rb
release: bundle exec rails db:migrate
worker: bundle exec sidekiq
- Enable the worker
We want to run Rails web server and Sidekiq worker. By default, there is only one process - web: 1
dokku ps:scale app_name worker=1
- Add remote to git git remote add dokku@:
git remote add <alias> dokku@<server IP>:<app_name>
examples:
- git remote add staging dokku@51.83.134.198:app_name
- git remote add production dokku@51.83.134.118:app_name
- Push to remote git
git checkout main
git push staging
or
git checkout 'feature/task_1'
git push staging 'feature/task_1':main
You can enable ssl certifivate only for domains.
- Setup DNS record
A
for the domain, with the value of the IP address of the VPS - Add domain for you app
dokku domains:add app_name example.com
- Enable letsencrypt for you domain:
dokku letsencrypt:enable app_name
Deploying to Dokku involves a few more steps than deploying to Heroku, but it gives you much more control over your infrastructure. This guide should get you up and running, but Dokku has many more features to explore. For more information, check out the Dokku documentation.
- Open the container with the app
dokku enter app_name web/worker
- Run rails console
dokku enter app_name web/worker
rails console
- Nginx logs
dokku nginx:access-logs app_name -t
- Rails logs
dokku logs app_name -t
- check if autorenew for letsencrypt is correctly configured
sudo -u dokku crontab -l
- expected result:
@daily /var/lib/dokku/plugins/available/letsencrypt/cron-job