This is an example project demonstrating how to deploy a Django application with Postgres on Fly.io.
The environment variables are stored in the .env
file.
Duplicate .env.dist
file and rename it to .env
. Update the environment variables:
SECRET_KEY=<your-generated-django-secure-super-secret-key>
DEBUG=True
DATABASE_URL=postgres://<user>:<password>@<host>:<port>/<db>
The default
DATABASE_URL
ispostgres://postgres:postgres@localhost:5432/hello_django
(checkhello_django/settings.py
).
flyctl is the command-line utility provided by Fly.io.
If you still don't have installed it, you can follow the instructions here to install it, sign up and log in to Fly.io.
This simple app already contains all the basic configuration for deploying to Fly.io.
Dockerfile
contain commands to build the image..dockerignore
list of files or directories Docker will ignore during the build process.fly.toml
configuration for deployment on Fly.io.
SECRET_KEY
is required when running collectstatic
. A default SECRET_KEY
can be generated by using the get_random_secret_key
function provided by Django:
# settings.py
from django.core.management.utils import get_random_secret_key
...
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env.str('SECRET_KEY', default=get_random_secret_key())
An alternative is to set a random key on Dockerfile
for building purposes:
# Dockerfile
ENV SECRET_KEY "non-secret-key-for-building-purposes" # <-- Set SECRET_KEY for building purposes
RUN python manage.py collectstatic --noinput
When running fly launch
, copy the existing configuration to your own app:
❯ fly launch
An existing fly.toml file was found for app hello-django-postgres
? Would you like to copy its configuration to the new app? Yes
During the fly launch
, the necessary secrets will be set:
SECRET_KEY
DATABASE_URL
❯ fly launch
Creating app in /Projects/flyio/hello-django-postgres
Scanning source code
Detected a Django app
? Choose an app name (leave blank to generate one): hello-django-postgres
...
Hostname: hello-django-postgres.fly.dev
Set secrets on hello-django-postgres: SECRET_KEY # <-- SECRET_KEY is set automatically
Set up a Postgres database during the fly launch
step:
❯ fly launch
Creating app in /Projects/flyio/hello-django-postgres
Scanning source code
Detected a Django app
? Choose an app name (leave blank to generate one): hello-django-postgres
...
? Would you like to set up a Postgresql database now? Yes # <-- You will be asked HERE
? Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk
Creating postgres cluster in organization fly-io
Creating app...
Setting secrets on app hello-django-postgres-db...
Provisioning 1 of 1 machines with image flyio/postgres:14.6
Waiting for machine to start...
....
Postgres cluster hello-django-postgres-db is now attached to hello-django-postgres
The following secret was added to hello-django-postgres:
DATABASE_URL=postgres://hello_django_postgres:****@****:5432/hello_django_postgres?sslmode=disable
Postgres cluster hello-django-postgres-db is now attached to hello-django-postgres # <-- postgres cluster attached to the app
Important: During this step, save your credentials in a secure place - you won't be able to see them again!
Your postgres cluster will be automatically attached to your app.
Before we can deploy it, make sure to update ALLOWED_HOSTS
and CSRF_TRUSTED_ORIGINS
in hello_django/settings.py
with the chosen/generated app name on Fly.io.
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '<your-app-name>.fly.dev']
CSRF_TRUSTED_ORIGINS = ['https://<your-app-name>.fly.dev']
Your Django app is now ready to be deployed!
Once all the previous steps are done, you can deploy your app:
❯ fly deploy
==> Verifying app config
--> Verified app config
==> Building image
...
1 desired, 1 placed, 1 healthy, 0 unhealthy [health checks: 1 total, 1 passing]
--> v0 deployed successfully
Your app is now up and running!
❯ fly open