10 Steps to Deploy a New Django App on Fly.io

Published on 11 Dec 2025 Tech DevOps
image
Photo by Gabriel Castles on Unsplash
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.

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

  1. 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).

  2. 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

  3. Configure settings.py for Production:
    • Set DEBUG = False.
    • Configure ALLOWED_HOSTS to accept the deployed host (e.g., using a .fly.dev subdomain or the FLY_APP_NAME environment variable).
    • Integrate WhiteNoise into your MIDDLEWARE and set STATIC_ROOT and STATICFILES_STORAGE for serving static assets.
  4. Create .dockerignore and 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

  1. 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.

  2. 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>

  3. 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'

  4. 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

  5. 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"

  6. 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

Comments