10 Steps to Deploy a New Django App on Fly.io
This article provides a quick and structured guide to setting up and deploying a fresh Django project onto the Fly.io platform, utilizing its automated configuration and powerful command-line interface, flyctl.
10 Steps to Deploy a New Django App on Fly.io
This article provides a quick and structured guide to setting up and deploying a fresh Django project onto the Fly.io platform, utilizing its automated configuration and powerful command-line interface, flyctl.
This workflow assumes you have a basic Django project initialized locally, have the flyctl CLI installed, and are logged into your Fly.io account.
Part 1: Preparing Your Django Project for Production
- Generate Dependency File:
Create a requirements.txt file listing all your Python packages and their versions to ensure the build environment is consistent with your local setup.
Command: pip freeze > requirements.txt (assuming you are in a virtual environment).
- Install Production Dependencies:
Install necessary packages for production, such as a production web server (e.g., Gunicorn), a PostgreSQL adapter (e.g., psycopg), and a static file handler (e.g., WhiteNoise).
Example: pip install gunicorn psycopg[binary] whitenoise
- Configure
settings.pyfor Production:
-
Set
DEBUG = False. -
Configure
ALLOWED_HOSTSto accept the deployed host (e.g., using a.fly.devsubdomain or theFLY_APP_NAMEenvironment variable). -
Integrate WhiteNoise into your
MIDDLEWAREand setSTATIC_ROOTandSTATICFILES_STORAGEfor serving static assets.
- Create
.dockerignoreand Project Structure:
Add files you don't want in your production Docker image (like .env, __pycache__, db.sqlite3) to a .dockerignore file.
`# Python
__pycache__/
*.pyc
*.pyo
*.pyd
*.db
# Django
*.sqlite3
*.log
staticfiles/
media/
# Environments
.env
.venv/
venv/
env/
# Git
.git
.gitignore
# OS
.DS_Store
Thumbs.db
# Docker
Dockerfile
docker-compose.yml
`
Ensure your project is in a clean directory ready for deployment.
Part 2: Launching and Deploying with Fly.io
- Initialize the Fly.io Application:
Run the launch command from your project's root directory. Fly.io will auto-detect your Django project and prompt you for configuration details.
Command: fly launch
Output: This command will generate a Dockerfile (for building the app image) and a fly.toml file (for Fly.io configuration) and may offer to provision a PostgreSQL database.
- Provision and Attach PostgreSQL Database (Optional but Recommended):
If prompted by fly launch, select Yes to set up a PostgreSQL cluster. This will automatically create and attach a database, setting a DATABASE_URL secret.
Alternative Command: fly postgres create followed by fly postgres attach <database-app-name>
- Set Secrets for Security:
Set your Django SECRET_KEY and other sensitive variables as secrets on Fly.io. This prevents them from being exposed in your code or Docker image.
Command: fly secrets set SECRET_KEY='your-secure-random-key-here'
- Run the Deployment:
Execute the deploy command. This triggers Fly.io to build the Docker image (based on your Dockerfile), push it to the registry, and deploy it to a new VM.
Command: fly deploy
- Execute Initial Migrations:
Once the app is deployed, run database migrations to create the necessary tables in your attached PostgreSQL database.
Command: fly ssh console --pty -C "python manage.py migrate"
- Open the Deployed Application:
Use the fly apps open command to quickly launch your default web browser and view your live Django application.
Command: fly apps open