Deploy to Flydotio

In this short guide, we will discuss how to deploy to fly.io . Fly.io allows users to deploy apps and databases close to users.

Getting started

Create an account on fly.io and install the CLI.

Fly CLI

The primary way to interact with the platform is via CLI. Follow the instructions here to install the CLI.

Configuration

The project comes with an example fly.toml config file with all the settings you need to deploy to fly.io . Let’s go over the main sections.

text
app = "x40"
primary_region = "lax"
kill_signal = "SIGINT"
kill_timeout = "5s"

[experimental]
  auto_rollback = true

[build]
  dockerfile = "./Dockerfile"

[deploy]
  release_command = "/release.sh"

[env]
  DJANGO_ACCOUNT_ALLOW_REGISTRATION = "False"
  DJANGO_ALLOWED_HOSTS = ".fly.dev,"
  DJANGO_SETTINGS_MODULE = "config.settings.production"
  PORT = "8080"
  S3_CUSTOM_DOMAIN = "base-cdn.advantch.com"
  CDN_URL="https://base-cdn.advantch.com" # has scheme
  S3_ENABLED = "True"
  STRIPE_TEST_PUBLIC_KEY = "pk_test_"
  S3_ENDPOINT_URL="https://<account-id>.r2.cloudflarestorage.com"
  S3_ACCESS_KEY_ID=""
  S3_STORAGE_BUCKET_NAME="base"

[processes]
  app = "uvicorn config.asgi:application --host 0.0.0.0 --port 8080"
  worker = "python manage.py run_huey"

[[services]]
  protocol = "tcp"
  internal_port = 8080
  processes = ["app"]

  [[services.ports]]
    port = 80
    handlers = ["http"]
    force_https = true

  [[services.ports]]
    port = 443
    handlers = ["tls", "http"]
  [services.concurrency]
    type = "connections"
    hard_limit = 25
    soft_limit = 20

  [[services.tcp_checks]]
    interval = "15s"
    timeout = "2s"
    grace_period = "1s"
  • build - this is where you specify your Dockerfile. Notice we are referencing the Dockerfile included in the project’s ops folder. There is one small change you will have to make to the Dockerfile.

  • app - this is your unique app name. You will be prompted to add one later when you launch the app.

  • deploy - this is the release command that will be run when you deploy the app. A basic deployment is: build image → push to fly registry → deploy → run release task. In our case, the release task will run migrations and collect static files to S3 or R2 if like to live on the bleeding edge.

  • process - this section defines your app’s processes. In our case, we are running the web server and the background worker.

  • services - this section includes configuration for mapping application ports to the flu platform. In our example, the web service exposed on port 8080 in the Dockerfile will be mapped to the external port 80 .

Env Variables and Secrets

Add your environment variables to the fly.toml file and set any secrets using fly secrets set SECRET_NAME=secret OTHER_SECRET=next. You are now ready to launch and deploy your app.

Create a new app

Run the following to create a new app and launch it on fly.io . Follow the instructions and respond to the CLI prompts. When asked to create a new database cluster on fly.io , create a new cluster, and it will be automatically attached to the app.

text

$ flyctl launch --vm-memory=1024

> An existing fly.toml file was found for app project
? Would you like to copy its configuration to the new app? (y/N) 

$ y

You can respond ‘y’ to this prompt. Fly will reuse the configuration already specified in the file. It will replace the app name with an automatically generated one or a new one if you specify it.

I have noticed that Fly will replace the [build].. section during this process. Make sure it is still available after you have finished setting up your app.

Postgres Cluster

Remember to take note of the Postgres cluster credentials. They will only be shown once. You can create multiple databases in the same cluster.

Postgres on flydotio

Postgres on fly is NOT a managed database. Please be aware of this if you have selected fly.io as your host. You will have to manage backups, maintenance, and upgrades. See the post below on PG backups.

Further Reading