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:
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:
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.
demo/flask/app folder, create a new file
app.py. 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__": app.run()
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
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:
In the project root, create a Dockerfile containing the following:
# Stage 1 FROM python:3.9-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.9-slim-buster AS runner WORKDIR /flask-app COPY --from=builder /flask-app/venv venv COPY app app ENV VIRTUAL_ENV=/flask-app/venv ENV PATH="$VIRTUAL_ENV/bin:$PATH" CMD python app/app.py
To build the Docker image execute the following command in the
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.
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.
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
Check the box
Use a private registry and, in the select field, click Create Registry Secret.
A modal opens asking for:
In the Ports section, change the export port from
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.
Koyeb is a developer-friendly serverless platform to deploy any apps globally.Start for free
Deploy 2 services for free and enjoy our predictable pricing as you grow
Get up and running in 5 minutes