Deploy a Python Flask App
This guide explains how to deploy a Python Flask application written using the Flask web framework to Koyeb using:
- Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
- Pre-built containers you can deploy from any public or private registry.
To successfully follow this documentation, you will need to have a Koyeb account (opens in a new tab). You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.
You can deploy and preview the Flask application from this guide using the Deploy to Koyeb button below.
You can consult the repository on GitHub (opens in a new tab) to find out more about the example application that this guide uses.
Get started by creating a minimalistic Python Flask application that we will deploy on Koyeb. You will need Python (opens in a new tab) installed on your machine.
In your terminal, run the following commands to create the directory that will hold the Flask application code:
In the folder you created, create a new virtual environment folder using
python -m venv venv. Virtual environments provide isolation from the system environment allowing each virtual environment you create to have its own installation directories, dependencies, etc.
Activate and load your virtual environment, type:
With your virtual environment active, install Flask, a minimal web framework, and create the
requirements.txt file to store the dependencies and versions of each package required to run the application.
pip install flask gunicorn
pip freeze > requirements.txt
Create a new file called
app.py to store our minimalist Flask application code. In the snippet below, we define a route to handle requests for
/ and return "Hello from Koyeb" as a response:
from flask import Flask
app = Flask(__name__)
return 'Hello from Koyeb'
if __name__ == "__main__":
This example application should run on any modern version of Python. If your application requires a specific
Python version, create a
runtime.txt file in your repository with the version number. Consult the build
with Python page to learn more.
Launch the application locally to make sure everything is running as expected.
You can now access the application at
In the project directory, initialize a new git repository by running the following command:
To tell Koyeb how to run the application, we'll add a
Procfile containing the command we want to run, prefixed by the
web: identifier. Add a file called
Procfile to the project root directory with the following contents:
web: gunicorn --bind :$PORT app:app
The command we use here includes the
--bind :$PORT option to allow the application to dynamically listen to the port specified by the
PORT environment variable.
We will use this repository to version the application code and push the changes to a GitHub repository. If you don't have an existing GitHub repository to push the code to, you can create a new one and run the following commands to commit and push changes to your GitHub repository:
git add requirements.txt app.py Procfile
git commit -m "Initial commit"
git remote add origin email@example.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git
git push -u origin main
Make sure to replace
<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.
To deploy the Python Flask app to Koyeb using the control panel (opens in a new tab), follow the steps below:
- Click Create App in the Koyeb control panel.
- Select GitHub as the deployment option.
- Choose the GitHub repository and branch containing your application code.
- Name your service, for example
- Name the App, for example
- Click the Deploy button.
A Koyeb App and Service will be created. Your application will be built and deployed to Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with
As an alternative to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.
To dockerize the Python Flask application, create a
Dockerfile in your project root directory and copy the content below:
FROM python:3-alpine AS builder
RUN python3 -m venv venv
COPY requirements.txt .
RUN pip install -r requirements.txt
# Stage 2
FROM python:3-alpine AS runner
COPY --from=builder /app/venv venv
COPY app.py app.py
CMD ["gunicorn", "--bind" , ":8080", "--workers", "2", "app:app"]
The Dockerfile above provides the minimum requirements to run the Python Flask application. You can easily extend it depending on your needs.
To build and push the Docker image to a registry and deploy it on Koyeb, refer to the page on deploying pre-built container images.