Ruby on Rails

Dokku: the best way to host personal projects

17 Jan 2023

Since Heroku discontinued its free tier, hosting small personal projects has become became difficult. In my case, I have projects in Ruby on Rails, React, Next.js and Static HTML and have used a variety of hosting services, as Render and Hostgator. I used to pay around $ 20 for both services, and now I only pay $ 7 with Digital Ocean. Living in a country where dollar is high valuable, I saved around 40%.

Dokku is what make this possible. It's like a open-source Heroku that you run on your own server. Sounds a bit scary: what do you mean? I will have to be responsible for a whole Heroku? That fear keeps me away, but now that I've moved, I can say that I've wasted a lot of time (and money).

It's very ease, that's the rough steps:

  1. 1. Sign up for a Digital Ocean account and create a new droplet.
  2. 2. Use SSH to connect to your server on your local machine.
  3. 3. Execute the one-line script to install Dokku.
  4. 4. Download necessary plugins to run Rails (Ruby and Postgresql)
  5. 5. Create a new app witn one-line code.
  6. 6. Create a new database witn one-line code.
  7. 7. Link the database with Rails (one line either)
  8. 8. Add the domain.
  9. 8. Set up the git repository to push your files.
  10. 10. Generate SSL certificates with Let's Encrypt.

That's all. You can repeat as many times as you want (until server crashes) with how many applications you want, with any language and framework.

I didn't do a full tutorial because I just wanted to spread the word and demonstrate how simple it is. I assumed that I would only be able to do such thing after reading Deployment from Scratch, but it's so easy that you learn the concepts as you go.

If you want to do this for your Ruby on Rails application, you can follow this tutorial or read docs.

I ran into some issues during the migration that took up half of the time that I used to the migration, I will share in case anyone else runs into them.

1. The Dokku Setup config screen did not appear after installation. Older tutorials mentiones this screen, but I believe the Dokku team removed it to make it less vulnerable. I skipped this tutorial step thinking would be irrelevant, but it isn't. If you don't configure your SSH public key on Dokku, you won't be able do to git push dokku latter. The solution is to manually configure through command line. This is how I did it:

echo 'my-public-key' | dokku ssh-keys:add admin

2. If you use Amazon S3 to store media, be careful! It's better to review your current credentials throught EDITOR="code --wait" rails credentials:edit command and copy current keys. I ended up changing them, which may have resulted in error 3. You must correctly configure the credentials and store them in environment variables. Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY the same way you did with RAILS_MASTER_KEY.

3. I believe that as I generate new credentials, that causes my images to not show up in the my blog. I didn't find a solution, so I changed manually. But if it was a large project, that would be a nightmare.

4. To migrate data from old database to the new, the best way that I found was to use pg_dump to download from Render and dokku posgtres:import to import to the new database. This is the code:

# To download data:
PGPASSWORD=<YOUR_DB_PASS> pg_dump \
  -h <HOST-URL> \
  -U <YOUR_DB_USER> \
  --format=custom --no-acl --no-owner \
  <YOUR_DB_NAME> > <YOUR_DUMP_NAME>.dump

# To import.
# First, I send it to Amazon S3
# (or you can send anywhere else)
# After that, I downloaded to a
# folder of the server:
wget <link-of-dump> -P dump/

# After that I imported:
dokku postgres:import <name_database> < dump/<name_of_dump>.dump

After that you can sit back relax, send all the small projects there and be in peace. It was only 5 hours of work that saves me around $ 13 monthly. For me, it was well worth it.

Sign up for my newsletter

Whenever I have updates here I will send it to subscribers.

Join the conversation

I created a post on LinkedIn for comments:

Comment