Deploying Coder on fly.io

Deploy Coder on Fly.io

Fly.io is a platform for deploying applications to the edge. It’s a great fit for Coder because it’s easy to deploy and scale, and it’s easy to manage. In this guide, we’ll walk through deploying Coder on Fly.io.

Prerequisites

Deploy Coder

  1. Log in to Fly.io with the CLI:
flyctl auth login
  1. Create a new fly postgres database:
flyctl postgres create DATABASE-NAME
  1. Create a new fly app:
flyctl apps create APP-NAME
  1. Connect to the database with the coder fly app:
flyctl postgres connect DATABASE-NAME --app APP-NAME

This will print out a connection string. Copy the connection string and save it to be used in the next step.

  1. Edit the fly.toml file and update as per the example below:
app = "APP-NAME"
kill_signal = "SIGINT"
kill_timeout = 5
primary_region = "ams"  # See a list of regions here: https://fly.io/docs/reference/regions/

[experimental]
  auto_rollback = true
  private_network = true  # This is needed for Coder to connect to the database

[build]
   image = "ghcr.io/coder/coder:latest"

[env]
  CODER_ACCESS_URL = "https://APP-NAME.fly.dev" # make sure to replace APP-NAME with your app name
  CODER_HTTP_ADDRESS = "0.0.0.0:3000"
  CODER_PG_CONNECTION_URL = "ADD YOUR POSTGRES CONNECTION STRING HERE"
  #CODER_VERBOSE = "true" # Uncomment this if you want to see more logs
  CODER_TELEMETRY_INSTALL_SOURCE = "fly.io"

[[services]]
  protocol = "tcp"
  internal_port = 3000
  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

  1. Deploy the app:

Run the following command to deploy the app from the directory where the fly.toml file is located:

flyctl deploy
  1. Scale the to use more memory:
flyctl scale memory 1024 --app APP-NAME
  1. Go to the URL of your app and start using Coder!

If you want to use a custom domain, you can do so by following the instructions here.

Update Coder

To update the Coder version, run flyctl deploy --aap APP-NAME again and it will pull the latest version of Coder.

Congratulations! You’ve deployed Coder on Fly.io!

Next Steps

To write your first coder template, check out the template docs.
We have an official fly.io template that you can use as a starting point.

Isn’t it like a terrible idea to add the PG connection string to the fly.toml file?

That should go into the secrets.

3 Likes

It is already in the secrets as DATABASE_URL but I was unable to use that for CODER_PG_CONNECTION_URL .
Any suggestions are welcomed.

Why don’t you just use CODER_PG_CONNECTION_URL in the secrets? :thinking:

1 Like

But I have one question will this be passed as an env variable to the docker image?
The coder docker container needs this to connect to the pg database.

tested this and it works. Thank you @pier. I want to edit the post to reflect this change

1 Like