Build, Deploy And Run A Python Application


In our Hands-On section, we show how to deploy a deployable image file using Flyctl. Now we are going to deploy an application from source. In this Getting Started article, we look at how to deploy a Python application on Fly.

The Hellofly-python Application

You can get the code for the example from the Fly-Examples Github repository. Just git clone to get a local copy.

The Python hellofly application is, as you'd expect for an example, small. It's a Python application that uses the Flask web framework. Here's all the code form

from flask import Flask, render_template

app = Flask(__name__)

def hello(name=None):
    return render_template('hello.html', name=name)

Flask is set up to route request to a hello function which in turn passes a name value (taken from the requests path)to a function to render a template. The template resides in the templates directory under the name hello.html. It too is very simple too:

<!DOCTYPE html>
<html lang="en">
<h1>Hello from Fly</h1>
{% if name %}
<h2>and hello to {{name}}</h2>
{% endif %}

We're using a template as it makes it easier to show what you should do with assets that aren't the actual application.

You will need to install Flask itself, or at least set up virtual environments as recommended in the Flask Install guide.

Once you have activated the virtual environment, run:

python -m pip install -r requirements.txt

This will load Flask and other required packages. One of those packages will be gunicorn which isn't a Flask dependency, but will be used when we deploy the app to Fly.

Testing the Application

Flask apps are run with the flask run command, but before you do that, you need to set an environment variable FLASK_APP to say which app you want to run.

FLASK_APP=hellofly flask run
 * Serving Flask app "hellofly"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on (Press CTRL+C to quit)

This will run our hellofly app and you should be able to connect to it locally on localhost:5000.

Now, let's move on to deploying this app to Fly.

Install Flyctl and Login

We are ready to start working with Fly and that means we need flyctl, our CLI app for managing apps on Fly. If you've already installed it, carry on. If not, hop over to our installation guide. Once that is installed you'll want to login to Fly.

Configure the App for Fly

Each Fly application needs a fly.toml file to tell the system how we'd like to deploy it. That file can be automatically generated with the command flyctl init command. We are going to use one of Fly's builtin deployment configurations for python

flyctl init
? App Name (leave blank to use an auto-generated name) hellofly-python

? Select organization: demo (demo)

? Select builder: python
    Python builtin
Builtins use port 8080
New app created
  Name         = hellofly-python
  Organization = personal
  Version      = 0
  Status       =
  Hostname     = <empty>

App will initially deploy to lhr (London, United Kingdom) region

Wrote config file fly.toml

You'll be asked for an application name first. We recommend that you go with the autogenerated names for apps to avoid namespace collisions. We're using hellofly-python here so you can easily spot it in configuration files.

Next you'll be prompted for an organization. Organizations are a way of sharing applications between Fly users. When you are asked to select an organization, there should be one with your account name; this is your personal organization. Select that.

Flyctl also asks you to select a builder. Builders are responsible for constructing the Docker image of your application which is then deployed to Fly's Firecracker VMs. The simplest to use are the builtin builders, which we recommend you use here. Select Python (Python Builtin). If you want to know more about the various builders, see Builders and Fly.

One thing to know about the builtin Python builder is that it will automatically copy over the contents of the directory to the deployable image. This is how you can move static assets such as templates and other files to your application. The other thing to know is that it uses a Procfile to run the application; Procfiles are used on other platforms to deploy Python applications so we keep it simple. The Procfile contains instructions for starting the application. Here's the contents of ours:

web: gunicorn hellofly:app

This says the web component of the application is served by gunicorn (which we mentioned earlier when talking about dependencies) and that should run the hellofly Flask app as we set up for Flask.

Inside fly.toml

The fly.toml file now contains a default configuration for deploying your app. In the process of creating that file, flyctl has also created a Fly-side application slot of the same name, hellofly. If we look at the fly.toml file we can see the name in there:

# fly.toml file generated for hellofly-python on 2021-01-13T15:21:15Z

app = "hellofly-python"

  builtin = "python"

kill_signal = "SIGINT"
kill_timeout = 5

  internal_port = 8080
  protocol = "tcp"

    hard_limit = 25
    soft_limit = 20

    handlers = ["http"]
    port = "80"

    handlers = ["tls", "http"]
    port = "443"

    interval = 10000
    timeout = 2000

The flyctl command will always refer to this file in the current directory if it exists, specifically for the app name/value at the start. That name will be used to identify the application to the Fly service. The rest of the file contains settings to be applied to the application when it deploys.

We'll have more details about these properties as we progress, but for now, it's enough to say that they mostly configure which ports the application will be visible on.

Deploying to Fly

We are now ready to deploy our app to the Fly platform. At the command line, just run:

flyctl deploy

This will lookup our fly.toml file, and get the app name hellofly-python from there. Then flyctl will start the process of deploying our application to the Fly platform. Flyctl will return you to the command line when it's done.

Viewing the Deployed App

Now the application has been deployed, let's find out more about its deployment. The command flyctl status will give you all the essential details.

flyctl status
  Name     = hellofly-python
  Owner    = demo
  Version  = 0
  Status   = running
  Hostname =

Deployment Status
  ID          = 0cdc72fe-3db9-aa52-eb84-5c3552053b1e
  Version     = v0
  Status      = successful
  Description = Deployment completed successfully
  Instances   = 1 desired, 1 placed, 1 healthy, 0 unhealthy

0530d622 0       lhr    run     running 1 total, 1 passing 0        40s ago

As you can see, the application has been with a DNS hostname of, and an instance is running in London. Your deployment's name will, of course, be different.

Connecting to the App

The quickest way to connect to your deployed app is with the flyctl open command. This will open a browser on the HTTP version of the site. That will automatically be upgraded to an HTTPS secured connection (for the domain).

to connect to it securely. Add /name to flyctl open and it'll be appended to the URL as the path and you'll get an extra greeting from the hellofly-python application.

Bonus Points

If you want to know what IP addresses the app is using, try flyctl ips list:

fly ips list
TYPE ADDRESS                             CREATED AT
v4                      2m1s ago
v6   2a09:8280:1:502e:4ce8:6058:7f09:62a 1m58s ago

And you can always run flyctl as fly if you want to save a few keystrokes.

Arrived at Destination

You have successfully built, deployed, and connected to your first Python application on Fly.