This is an example project demonstrating how to deploy an Elixir and Phoenix application on Fly.io.
This creates a basic Phoenix application that uses a PostgreSQL database. The Elixir deployment approach used here is building a release through Docker.
Dockerfile
- An Elixir release is built through Docker.dockerignore
- Exclude pulling in Elixir deps and Node packages since they might have native compilation from your dev environmentlib/hello_elixir/release.ex
- Executed during deploy fromfly.toml
to run our database migrationsconfig/runtime.exs
- The runtime ENV values we expect for production
fly.toml
- Fly deployment configuration
Elixir has a mix task that can generate a new Phoenix key base secret. Let's use that.
mix phx.gen.secret
It generates a long string of random text. Let's store that as a secret for our app. When we run this command in our project folder, flyctl
uses the fly.toml
file to know which app we are setting the value on.
fly secrets set SECRET_KEY_BASE=<GENERATED>
fly postgres create
? App name: hello-elixir-db
Automatically selected personal organization: Mark Ericksen
? Select region: sea (Seattle, Washington (US))
? Select VM size: shared-cpu-1x - 256
? Volume size (GB): 10
Creating postgres cluster hello-elixir-db in organization personal
Postgres cluster hello-elixir-db created
Username: <USER>
Password: <PASSWORD>
Hostname: hello-elixir-db.internal
Proxy Port: 5432
PG Port: 5433
Save your credentials in a secure place, you won't be able to see them again!
Monitoring Deployment
2 desired, 2 placed, 2 healthy, 0 unhealthy [health checks: 6 total, 6 passing]
--> v0 deployed successfully
Connect to postgres
Any app within the personal organization can connect to postgres using the above credentials and the hostname "hello-elixir-db.internal."
For example: postgres://<USER>:<PASSWORD>@hello-elixir-db.internal:5432
See the postgres docs for more information on next steps, managing postgres, connecting from outside fly: https://fly.io/docs/reference/postgres/
We can take the defaults which select the lowest values for CPU, size, etc. This is perfect for getting started.
We can use flyctl
to attach our app to the database which also sets our needed DATABASE_URL
ENV value.
fly postgres attach --postgres-app hello-elixir-db
Postgres cluster hello-elixir-db is now attached to icy-leaf-7381
The following secret was added to icy-leaf-7381:
DATABASE_URL=postgres://<NEW_USER>:<NEW_PASSWORD>@hello-elixir-db.internal:5432/icy_leaf_7381?sslmode=disable
We can see the secrets that Fly is using for our app like this.
fly secrets list
NAME DIGEST DATE
DATABASE_URL 830d8769ff33cba6c8b29d1cd6a6fbac 1m10s ago
SECRET_KEY_BASE 84c992ac7ef334c21f2aaecd41c43666 9m20s ago
Looks like we're ready to deploy!
Once you've went through the steps of fly launch
:
fly deploy
... will bring up your app!