Python Flask Application Deployment on Koyeb

May 03, 2021

Édouard Bonlieu

Édouard Bonlieu


Flask is a popular and lightweight web framework written in Python offering developers all the tools and features to create web applications with flexibility and ease.

In this guide, you will create a simple "Hello World" application written in Flask, create a Dockerfile using multi-stage builds to minimize the Docker image size, run the Dockerized application locally and deploy it to the Koyeb platform.

By deploying your Python Flask application on Koyeb you benefit from native autoscaling, Automatic HTTPS (SSL) on all apps you deployed, high availability, and global load-balancing & CDN to make your applications global.


To successfully follow and complete this guide, you need:


To successfully dockerize your Flask application and deploy it to Koyeb, you have to follow the following steps:

  1. Create a "Hello World" Flask application
  2. Create the Docker image to run the Flask application
  3. Push the Docker image on the Docker Hub
  4. Deploy the Flask application on Koyeb

Create a "Hello World" Flask application

To get started, we need to create a new folder to store the Flask application. In your terminal create a new folder by executing the command below:

mkdir -p ~/demo/flask

In my case, I create a demo/flask folder in my home directory.

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.

Then to activate and load your virtual environment, type:

. venv/bin/activate

Install dependencies

Now that our environment is created and activated, let's install Flask by running:

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

Create the Hello World Flask application

In the demo/flask/app folder, create a new file We will use this file to store our minimalist Flask application code. In the snipper below, we define a route to handle / request and return "Hello, World!" as a response.

from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == "__main__":

Run the application locally.

Back to your terminal and run the command below to launch the Flask application.


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

Create the Docker image to run the Flask application

To create our Docker image, we will Docker multi-stage to keep the Docker image size down by building an image that only contained what our application needs to run.

The Docker image is composed of two stages:

  1. Create the virtual environment and install dependencies
  2. Copy the virtual environment and launch the app

In the project root, create a Dockerfile containing the following:

# Stage 1 FROM python:3-slim-buster AS builder WORKDIR /flask-app RUN python3 -m venv venv ENV VIRTUAL_ENV=/flask-app/venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" COPY requirements.txt . RUN pip install -r requirements.txt # Stage 2 FROM python:3-slim-buster AS runner WORKDIR /flask-app COPY --from=builder /flask-app/venv venv COPY ENV VIRTUAL_ENV=/flask-app/venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" ENV FLASK_APP=app/ EXPOSE 5000 CMD ["python", "-m" , "flask", "run", "--host="]

Build and run the Docker image locally

To build the Docker image execute the following command in the ~/demo/flask folder:

docker build . -t <YOUR_DOCKER_HUB_USERNAME>/koyeb-flask-demo

In this guide, we will push the Docker image on the Docker Hub registry. Replace <YOUR_DOCKER_HUB_USERNAME> with your Docker Hub username.

Once the Docker build achieved, let's run the Docker image locally to ensure everything is running as expected:

docker run -d -p 5000:5000 <YOUR_DOCKER_HUB_USERNAME>/koyeb-flask-demo

If you open your browser and navigate to http://localhost:5000 you will see Hello, World! served by the Docker container.

Push the Docker image on the Docker Hub

With our Docker image built and working as expected, we can now push it to the Docker Hub. In your terminal run the command below to push the image.

docker push <YOUR_DOCKER_HUB_USERNAME>/koyeb-flask-demo

Within a few minutes, you will see your Docker image available on your Docker Hub account repository.

Deploy the Flask application on Koyeb

The most exciting part is happening now as we will deploy our Flask application on Koyeb!

On the Koyeb Control Panel, click the Create App button.

In the form, fill the Docker image field with the name of the image we previously created which should look like <YOUR_DOCKER_HUB_USERNAME>/koyeb-flask-demo.

Check the box Use a private registry and, in the select field, click Create Registry Secret.

A modal opens asking for:

  • a name for the Secret which will be created, we can use for instance docker-hub-secret
  • the registry provider to generate the secret containing your private registry credentials, in our case Docker Hub
  • your Docker Hub username and password. We recommend you to generate an access token from the Docker Hub to use instead of your password. Once you've filled all the fields, click the Create button.

In the Ports section, change the export port from 80 to 5000, which is the port our Flask app is listening on. This setting is required to let Koyeb know which port your application is listening to and properly route incoming HTTP requests. We don't need to change the Path, our app will be available at the root of our domain: /.

Give your App a name, i.e koyeb-flask-demo, and click Create App

You can add more regions to deploy your applications, set environment variables, and define the horizontal scaling according to your needs.

You will automatically be redirected to the Koyeb App page where you can follow the progress of your Flask application deployment. In a few seconds, once your app is deployed, click on the Public URL ending with

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.

Welcome to Koyeb

Koyeb is a developer-friendly serverless platform to deploy any apps globally.

Start for free
Start for free, pay as you grow

Deploy 2 services for free and enjoy our predictable pricing as you grow

Deploy your first app in no time

Get up and running in 5 minutes