Oct 13, 2021
6 min read

Continuously Deploy a Flask App using Gunicorn on Koyeb

Introduction

Flask is a lightweight web framework written in Python that offers developers powerful tools and features to create web applications with ease and flexibility.

This guide demonstrates how to deploy a Flask application using Gunicorn on the Koyeb Severless Platform. You will create a "Hello World" Flask application, push your code to a GitHub repository, and then deploy your application on the Koyeb platform using its git-driven deployment.

With Koyeb's git-driven deployment, your application benefits from a built-in continuous deployment pipeline because each git push you make to your repository automatically triggers a new build and deployment for your application on the Koyeb Serverless Platform.

For those who prefer deploying Docker containers, we also published a guide explaining how to dockerize and deploy a Python Flask application on the Koyeb Serverless Platform.

Requirements

To successfully follow and complete this guide, you need:

Steps

To successfully create and deploy Flask application on Koyeb using git-driven deployment, you will need to follow these steps:

  1. Create a Flask application
  2. Create a git directory for the Flask application
  3. Deploy the Flask application on Koyeb

Create a Flask application

Begin by creating a directory where our application will live and moving into it by running the following commands:

mkdir flask-on-koyeb
cd flask-on-koyeb

Create a virtual environment

Next, we will create a virtual environment, also known as virtualenv. Virtual environments allow you to create an isolated Python environment to avoid interfering with Python's system packages or other virtual environments.

To create a virtual environment, execute:

python3 -m venv ~/.venv/flask-app

This command creates a new directory flask-app in the ~/.venv directory. The virtualenv contains a copy of the Python interpreter, pip, the standard library, and various supporting files.

To activate the virtualenv we just created, run:

source ~/.venv/flask-app/bin/activate

The virtualenv python and pip executables are now added into your shell’s PATH.

Install Dependencies

Next, install the dependencies required to run the application. In this case, we need Flask and Gunicorn. Flask is the lightweight Python framework we are using for this application and Gunicorn is a web server gateway interface (WSGI) server implementation used to run Python web apps. Gunicorn is a high-performing server better suited for production traffic.

pip install Flask Gunicorn # Install Flask and Gunicorn
pip freeze > requirements.txt # Create the requirements.txt to store the dependencies and version of each package required to run our application.

Create the Flask application

In your flask-on-koyeb directory, create a new app.py file with the following:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run()

This file is storing our Flask application code. In the snipper above, we define a route to handle / requests and return Hello, World! as a response.

Check to see if the application is working locally by running:

python3 app.py

In your browser, go to http://localhost:5000 and you will see Hello, World!.

Create a git directory for the Flask application

Now that we have created our Flask application, we will create a git directory to store it.

Initialize a new git directory for your Flask project by running the following in your terminal:

git init

To keep only the necessary files in the repository, add this .gitignore file to exclude unnecessary files.

curl https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore > .gitignore

Next, execute the following command to add your files and commit the changes to your repository:

git add .
git commit -m "Flask app initial commit"

Open a new tab in your browser and go to GitHub. Create a new repository named flask-on-koyeb and click the Create repository button. Then, go back to your terminal and add GitHub as a remote repository by running:

git remote add origin git@github.com:YOUR_GITHUB_USERNAME/flask-on-koyeb.git

Rename the repository default branch to main executing:

git branch -M main

Next, push your changes to the GitHub repository by running:

git push -u origin main

Now, you are ready to deploy your application on Koyeb.

Deploy the Flask application on Koyeb

On the Overview tab of the Koyeb control panel, click Create Web Service to begin:

  1. Choose GitHub as the deployment method.
  2. Select the repository for your Flask application.
  3. In the Builder section, click the Override toggle associated with the Run command and enter gunicorn app:app in the field.
  4. Choose a name for your App and Service, for example koyeb-flask-demo, and click Deploy.

Note: You can add more regions, change the instance size, set environment variables, and define horizontal scaling according to your needs.

You land on the deployment page of your service where you can follow the progress of your Flask application's deployment. Koyeb will take care to build and deploy your application. Once these steps are completed, you can access your application by clicking your App URL ending with koyeb.app in the Koyeb control panel.

If you want to learn about how Koyeb automatically builds your applications from git, make sure to read our how we build from git documentation.

Your Flask application is now running on Koyeb and benefits from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network.

Conclusion

In this guide, you learned how to deploy a Flask application on Koyeb and benefit from its built-in continuous deployment pipeline. Each change you push to your repository will automatically trigger a new build and deployment on the Koyeb Serverless Platform. Your changes then go live as soon as the deployment passes all necessary health checks. In case of a failure during one of your deployments, we ensure to keep the latest working deployment active so your application is always up and running.

Other great benefits to deploying your Python Flask application on Koyeb include the platform's built-in autoscaling, automatic HTTPS (SSL), high availability, global load-balancing and edge network.

All of these features and more work out-of-the-box and from the getgo. You focus on your code while Koyeb handles all the necessary underlying infrastructure to keep your services scalable, high performing and available.

Want more Flask on Koyeb? Check out our Flask documentation and one-click Flask app to learn how to deploy a Flask application on Koyeb.

Give Koyeb a try with your application! If you would like to read more Koyeb tutorials, checkout out our tutorials collection. Have an idea for a tutorial you'd like us to cover? Let us know by joining the conversation over on the Koyeb community platform!


Deploy AI apps to production in minutes

Koyeb is a developer-friendly serverless platform to deploy apps globally. No-ops, servers, or infrastructure management.
All systems operational
© Koyeb