Deploying a Python Flask application to Fly

Bjorn Krolsavatar

Bjorn Krols

Published on
16 April 2023

In this tutorial you will learn how to deploy a containerized Python Flask application to Fly.

The final code of this article can be found here.


  • A Fly account
  • A computer with Python, Docker, and Flyctl (the Fly CLI)

Creating the application



from flask import Flask, jsonify
import random

application = Flask(__name__)

port = 8080

def hello():
   return jsonify({"message": "Hello, World!"})

def generate_random_number():
   return jsonify({"number": random.randint(0, 100)})

if __name__ == "__main__":

Containerizing the application


FROM python:3.9-slim-buster

WORKDIR /usr/src/app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app



CMD ["flask", "run", "--host", "", "--port", "8080"]

To build and run the container locally:

docker build . -t hello-world
docker run -dp 8080:8080 hello-world

Deploying the application

  1. fly launch to prepare your fly.toml file, containing your project settings. Inside the fly.toml file, ensure the service's internal_port (8080 by default) matches the port exposed by your container.
  2. fly deploy to deploy your application.
  3. fly open to open the deployed application in your browser.

Cleaning up

fly destroy your-unique-app-id -y removes your application from the Fly platform.

That's all! Don't forget to delete the application once you're done testing.

The final code of this article can be found here.

Subscribe to our newsletter

The latest news, articles, and resources, sent to your inbox weekly.

More like this