How to deploy to Fly.io

Fly.io is a great hosting company that lets you deploy your Phoenix application for free. And when you scale out of the free plan you can easily upgrade to the paid tiers. They also let you scale to servers around the world so you can always be close to your users.

The SAAS Starter Kit can more or less easily be deployed to Fly with just a few easy steps. First of all, make sure to get your Fly.io account and then we are ready to go.

Install Fly

Head over to https://fly.io/ to see the latest version of their installation guide.

However, on OS X and Homebrew, you can install it with:

brew install flyctl

First deploy

The first deploy wont succeed but will let Fly analyze the code and generate some files that are needed.

fly launch

First question is the app name. You can just leave it blank and let Fly autogenerate one:

Picking Region

When it comes to selecting region, I usually pick one on US eastcoast. Since I am in Europe and most of my expected visitors are from US and Europe, my thinking is that the server will be fast enough. And also, its Fly, so it easy to deploy on new locations.

Picking database

When it comes to the database, answer Yes, and then pick a suitable one from the list. You can come prtty far with the Development one and upgrade later.

After the database is picked, the launch goes ahead and creates some files and later ask if I would like to deploy not. Pick No. There are some things in the code that needs to be updated.

Production Secrets

To be able to run Phoenix in production, you need a new SECRET_KEY_BASE. You can generate a new one with the command:

mix phx.gen.secret

And it will genereate a random string like:

You can set it on fly by using the command:

fly secrets set SECRET_KEY_BASE="/eA8tKVjSjmN9UH79zovIZ2nx ......"

Update Dockerfile

There are a few minor modifications that you need to do on the Dockerfile. First is to make sure to install npm as you need it for the javascript bundling.

RUN apt-get update -y && apt-get install -y build-essential git npm \

Insert a new line before assets.deploy so that the javascript is installed. It is unlikely that you wont have any javascipt in your app so you might as well go through this step now.

# compile assets
RUN cd assets && npm install
RUN mix assets.deploy


Update configuration

In runtime.exs there need to be some updates.


With these changes, I should now be able to deploy and view the site.

fly deploy

Make sure that all steps run through and the the migrations are run. If you use the SAAS Starter Kit and havent done any modifications, this should be the case.

And when I visit the site with the temporary url, I get: