Run a Rails App

Getting an application running on Fly is essentially working out how to package it as a deployable image. Once packaged it can be deployed to the Fly infrastructure to run on the global application platform.

In this guide we'll learn how to deploy a Rails application on Fly.

We'll take a look at the short versions first: deploying with a preconfigured example Rails app. You can get the code for the example from the Fly-Examples GitHub repository. Clone the repo by running:

git clone https://github.com/fly-apps/hello-rails

Install Flyctl and Login

We are ready to start working with Fly and that means we need flyctl, our CLI app for managing apps on Fly. If you've already installed it, carry on. If not, hop over to our installation guide. Once thats installed you'll want to log in to Fly.

Provision Rails and Postgres Servers

To configure and launch the app, you can use fly launch and follow the wizard:

fly launch
Creating app in ~/Projects/hello-rails
Scanning source code
Detected a Rails app
? App Name (leave blank to use an auto-generated name): hello-rails
Automatically selected personal organization: John Smith
? Select region: sjc (San Jose, California (US))
Created app hello-rails in organization personal
Set secrets on hello-rails: RAILS_MASTER_KEY
Wrote config file fly.toml
? Would you like to setup a Postgresql database now? Yes
For pricing information visit: https://fly.io/docs/about/pricing/#postgresql-clusters
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
Creating postgres cluster hello-rails-db in organization personal
Postgres cluster hello-rails-db created
  Username:    postgres
  Password:    <redacted>
  Hostname:    hello-rails-db.internal
  Proxy Port:  5432
  PG Port: 5433
Save your credentials in a secure place, you won't be able to see them again!

You can set a name for the app, choose a default region, and choose to launch and attach a Postgresql database.

Deploying your application is done with the following command:

fly deploy

Once it's done, you can use fly status to check the app's status and deployment, and you should see something like this:

fly status
App
  Name     = hello-rails
  Owner    = personal
  Version  = 0
  Status   = running
  Hostname = hello-rails.fly.dev

Deployment Status
  ID          = c3ca80bf-4034-837a-ce92-682e6528c5c7
  Version     = v0
  Status      = successful
  Description = Deployment completed successfully
  Instances   = 1 desired, 1 placed, 1 healthy, 0 unhealthy

Instances
ID        PROCESS VERSION REGION  DESIRED STATUS  HEALTH CHECKS       RESTARTS  CREATED
748868bc  app     0       lhr     run     running 1 total, 1 passing  0         20s ago

If you elected to provision a Postgres database during setup, it's deployed as a seperate Fly app to hello-rails-db.

That's it! Run fly open to see your deployed app in action.

Try a few other commands:

  • fly logs - Tail your application logs
  • fly ssh console - Open a terminal on your VM

Configuring an Existing Rails Application

If you've got an existing Rails app, and you want to set it up for Fly, the process is pretty similar; fly launch will take you through the setup steps, but there's a couple of things to look at first.

If your Rails application wasn't created with the --database=postgresql option and you didn't subsequently add the Postgres gem to your bundle, do so now via:

bundle add pg --group production

Your database settings will be passed to your app via a DATABASE_URL environment variable (which Rails picks up automatically). That means you can drop a lot of the production configuration from config/database.yml.

If you've got your app's secrets stored in an encrypted credentials file such as config/credentials.yml.enc or config/credentials/production.yml.enc, you'll need to provide the master key to your app via fly secrets set. For example, if your master key is stored in config/master.key, you can run:

fly secrets set RAILS_MASTER_KEY=$(cat config/master.key)

Next Steps

At this point you now have a basic Rails application running that points at a Postgres database, but most Rails applications need more then that like access to a Redis database for caching and background workers. Read through the basics to setup the remainder of your Rails application.

Additional resources