Skip to main content
Getting an application running on Fly.io is essentially working out how to package it as a deployable image and attach it to a database. Once packaged, it can be deployed to the Fly.io platform. In this guide we’ll learn how to deploy an Elixir Phoenix application on Fly.io and connect it to a PostgreSQL database.
NOTE: This guide is for apps generated on Phoenix 1.6.3 or later, where deployment is streamlined significantly. If you’re on an earlier version and can’t upgrade, check out our legacy deployment guide.
We’ll be using the standard web application generated by the Elixir Phoenix Framework.

Preparation

If you don’t already have Elixir and Phoenix installed, get them set up before starting this guide. When you install Elixir, the Mix build tool ships with it. You can install the Phoenix project generator phx.new from a Hex package as follows:

Generate the app and deploy with Postgres

If you just want to see how Fly.io deployment works, this is the section to focus on. Here we’ll bootstrap the app and deploy it with a Postgres database. First, install flyctl, the Fly.io CLI, and sign up to Fly.io if you haven’t already. Now let’s generate a shiny, new Phoenix app:
The output ends with instructions for configuring the database and starting the app manually. The Fly.io launcher is going to take care of this, so we can ignore them. When you run fly launch from the newly-created project directory, the launcher provides some defaults for your new app, and gives you the option to tweak the settings. Run:
You’ll get a summary of the defaults chosen for your app:
Type y at the prompt to open the Fly Launch page, and make the following changes to your app config:
  • Change the default app name and region, if needed.
  • For Databases, select Fly Postgres, give the Postgres database app a name (for example, your app name with -db appended) and choose a configuration.
Once you confirm your settings, you can return to the terminal, where the launcher will:
  • Run the deployment setup task
  • Build the image
  • Set secrets required by (SECRET_KEY_BASE, for example)
  • Deploy the application in your selected region
Make sure to note your Postgres credentials from the output.
That’s it! Run fly apps open to see your deployed app in action. Try a few other commands:
  • fly logs - Tail your application logs
  • fly status - App deployment details
  • fly status -a postgres-database-app-name - Database deployment details
  • fly deploy - Deploy the application after making changes
To recap, the fly launch command detected that we are using Phoenix, set up a Fly app, created a Fly Postgres app and configured it for us, updated our Dockerfile, and created special modules and scripts to run ecto migrations for us!

Storing secrets on Fly.io

You may also have some secrets you’d like set on your app. Use fly secrets to configure those.

Deploying again

When you want to deploy changes to your application, use fly deploy.
The fly deploy command uses a Fly.io remote builder app to build the image. You can always check on the status of a deploy:
Check your app logs:
If everything looks good, open your app on Fly.io!

Important IPv6 settings

The flyctl command attempts to modify your project’s Dockerfile and append the following lines:
If you customized your Dockerfile or launched without the Dockerfile, this setting may not have been set for you. These values are important and enable your Elixir app to work smoothly in Fly’s private IPv6 network. Check for this if you encounter network-related errors like the following:

What’s Next?

Next up, IEx into Your Running App!