Deploying Rails Apps to a Caprover Instance

Deploying Rails Apps to a Caprover Instance

A few days ago I wrote an article on deploying rails apps with nginx + puma + mina. Some people in the comments suggested I try Caprover, Dokku and other open source PaaS software. Dokku didn’t cut it for me, it’s a good piece of software it just wasn’t for me.

I’m aware of kamal and it’s also a great piece of software especially since it’s a first class rails citizen, however as always competition can’t hurt. (I also didn’t like how kamal is full of configuration) I mean, rails at it’s core is convention over configuration and kamal in it’s current state is the opposite of that, which I totally understand it’s only on version 2 and writing software takes a lot of work.

Getting Caprover

Back to this article, the first thing you need is to have caprover installed on your vps/server. If you don’t have one, I recommend getting one from Hertzner. After login and setting up your ssh and firewall, we now move to install caprover

Caprover uses docker to deploy apps and caprover itself runs on docker, our first dependency is docker

Follow instructions on this link to install docker

https://docs.docker.com/engine/install/debian/ I prefer to use debian for server, you can select your target OS from the left sidebar

After docker is fully installed, install caprover by following instructions on this link

https://caprover.com/docs/get-started.html

Database Setup

After finishing all the caprover setup it’s time to install our databases. I prefer to use mysql, some of you prefer postgres

Visit your caprover captain dashboard, navigate to apps and click on “one click install” apps

On the next page search for “mariadb”

Select mariadb and set the parameter slike name, root password etc

When you’re done editing click deploy.

Next set allow this database to be accessible from the host machine on port 3306 or whatever port postgres uses. This allows you to login to the database shell as root to create users, databases etc, from the host machine

Select the newly created database app and go to app configs and add port mappings

Setting up database users

This part is entirely up to you, this how I did it when I was deploying Pulse on caprover

SSH to your host machine and connect to the database using the root password you created in the previous step

Create a database user, eg, rails, do not include hostname

Grant that user permissions on all %_% databases which follows the format of database names created by rails eg, for pulse it will be pulse_production

After this you’re done with the server, it’s time to deploy your rails app

Deploy rails apps

In the caprover dash, create an app, tick the “has persistance” flag

After creating go to apps config and add env variables, I prefer to use rails credentials,

Add the RAILS_MASTER_KEY variable with the value from your config/master.key or config/credentials/production.key. If you’re using credentials instead of env vars this is everything

If you’re using active storage with local driver, add persistent path mapping to map the storage directory to actual path on the host machine

Set the database configuration to use the user you created in the previous step, since this database is in a docker network, get the hostname from caprover dash.

Like I said at the start of this article, caprover uses docker, the dockerfile that comes default with rails will suffice without any changes

Navigate to your project directory and run caprover deploy follow the prompts to select app, etc, and that’s it your app is live

Let me know what you think about caprover, I personally think it’s awesome, follow me here on Hashnode for more articles on rails and web apps development