Provision Rails and Postgres Servers
To configure and launch your Rails app, you can usefly launch and follow the wizard.
Deploy your application
Deploying your application is done with the following command:Troubleshooting your initial deployment
Since this is an existing Rails app, its highly likely it might not boot because you probably need to configure secrets or other service dependencies. Let’s walk through how to troubleshoot these issues so you can get your app running.View log files
If your application didn’t boot on the first deploy, runfly logs to see what’s going on.
fly logs command, try running fly dashboard, and select Monitoring
in the left-hand column.
Open a Rails console
It can be helpful to open a Rails console to run commands and diagnose production issues. If you are not running with a sqlite3 or a volume, the recommended way to do this is to run a console in an ephemeral Machine:Common initial deployment issues
Now that you know the basics of troubleshooting production deployments, lets have a look at some common issues people have when migrating their existing Rails applications to Fly.Access to Environment Variables at Build Time
Some third-party gems and services require configuration including the setting of secrets/environment variables. Theassets:precompile step
will load your configuration and may fail if those secrets aren’t set
even if they aren’t actually used by the assets:precompile step.
Rails itself has such a variable, and you will see some combination of
SECRET_KEY_BASE and DUMMY in most Dockerfiles.
In many cases, you can avoid the problem by adding an if statement. For example, if your code looks like:
Dockerfile. Just be sure that any such values you add
to your Dockerfile don’t contain actual secrets as your Dockerfile will
generally be committed to git or otherwise may be visible.
If you have need for actual secrets at build time, take a look
at Build Secrets.
Finally, if there are no other options you can generate a Dockerfile that will
run assets:precompile at deployment time with the following command:
- The precompile step will be run for each server you deploy rather that once during build time to produce an image that can be deployed multiple times.
- Normally Dockerfiles are structured so that packages that are only needed
at build time (e.g. Node.js) are not present on the deployed machine.
If you defer theassets:precompilestep, these packages will need to be present in order to deploy.
Language Runtime Versions
Having different runtime versions of language runtimes on your development machine and on production VMs can lead to problems. Run the following commands to see what versions you are using in development:.ruby-version, and .node-version.
Finally, package.json files may have version numbers in engines.node
and packageManager values.
Whenever you update your tools, run the following command to update your
Dockerfile:
ARG in your Dockerfile.
ActiveStorage
From the documentation:Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects. It comes with a local disk-based service for development and testing and supports mirroring files to subordinate services for backups and migrations.Accordingly:
- Don’t use Disk service unless you put the data on a Volume and are prepared to sync the data between machines
- If you want to have your active storage data hosted on fly.io, consider using the postgres adapter or MinIO.
- Of course, you are welcome to use Amazon S3, Microsoft Azure, or Google Cloud Services.
Postgres database drivers
If you didn’t initially deploy with a postgres database but want to add one later, you can create a database usingfly postgres create.
Next, update your dockerfile to include the postgres libraries using:
fly postgres attach.
Multiple Rails applications can use the same PostgresQL server. Just take
care to make sure that each Rails application uses a different database name.
ActiveSupport::MessageEncryptor::InvalidMessage
Generally, this means that there is a problem with yourRAILS_MASTER_KEY. It is a common initial setup problem, but once it works it tends to keep working.
fly launch will extract your master key if your project has one and make it
available to your deployed application as a
secret.
If you’ve already run fly launch on a project that doesn’t have a master key
(commonly because files containing these values are excluded from being pushed by being listed in your .gitignore file), you will need to generate a key
and set the secret yourself. The Ruby on Rails Guides contains information on generating new credentials.
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:
RAILS_MASTER_KEY is deployed using: