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, This workflow assumes you have a basic Django project initialized locally, have the Create a Command: 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: Add files you don't want in your production Docker image (like Ensure your project is in a clean directory ready for deployment. 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: Output: This command will generate a If prompted by Alternative Command: Set your Django Command: Execute the deploy command. This triggers Fly.io to build the Docker image (based on your Command: Once the app is deployed, run database migrations to create the necessary tables in your attached PostgreSQL database. Command: Use the Command: 10 Steps to Deploy a New Django App on Fly.io
flyctl.flyctl CLI installed, and are logged into your Fly.io account.Part 1: Preparing Your Django Project for Production
requirements.txt file listing all your Python packages and their versions to ensure the build environment is consistent with your local setup.pip freeze > requirements.txt (assuming you are in a virtual environment).pip install gunicorn psycopg[binary] whitenoisesettings.py for Production:
DEBUG = False.ALLOWED_HOSTS to accept the deployed host (e.g., using a .fly.dev subdomain or the FLY_APP_NAME environment variable).MIDDLEWARE and set STATIC_ROOT and STATICFILES_STORAGE for serving static assets..dockerignore and Project Structure:
.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
Part 2: Launching and Deploying with Fly.io
fly launchDockerfile (for building the app image) and a fly.toml file (for Fly.io configuration) and may offer to provision a PostgreSQL database.fly launch, select Yes to set up a PostgreSQL cluster. This will automatically create and attach a database, setting a DATABASE_URL secret.fly postgres create followed by fly postgres attach <database-app-name>SECRET_KEY and other sensitive variables as secrets on Fly.io. This prevents them from being exposed in your code or Docker image.fly secrets set SECRET_KEY='your-secure-random-key-here'Dockerfile), push it to the registry, and deploy it to a new VM.fly deployfly ssh console --pty -C "python manage.py migrate"fly apps open command to quickly launch your default web browser and view your live Django application.fly apps open
10 Steps to Deploy a New Django App on Fly.io
Disclaimer:
This post was created with the assistance of Gemini and/or ChatGPT for informational purposes. While given a quick look over, readers are encouraged to verify key facts independently.
Comments