Python Flask Application Deployment on Koyeb
Introduction
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.
Requirements
To successfully follow and complete this guide, you need:
- A Koyeb account to deploy and run your Python Flask application
Steps
To successfully dockerize your Flask application and deploy it to Koyeb, you have to follow the following steps:
- Create a "Hello World" Flask application
- Create the Docker image to run the Flask application
- Push the Docker image on the Docker Hub
- 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 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()
Run the application locally.
Back to your terminal and run the command below to launch the Flask application.
python app.py
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:
- Create the virtual environment and install dependencies
- 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 app.py app.py
ENV VIRTUAL_ENV=/flask-app/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV FLASK_APP=app/app.py
EXPOSE 5000
CMD ["python", "-m" , "flask", "run", "--host=0.0.0.0"]
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, on the Overview tab, click Create Web Service to begin:
- Choose Docker as the deployment method.
- Enter name of the image you created in the Docker image field. For example,
<YOUR_DOCKER_HUB_USERNAME>/koyeb-flask-demo
. - Click the Private image toggle and, in the select field, click Create secret. In the form that appears, choose a name for the Secret, for example
docker-hub-secret
, the registry provider, in our case Docker Hub, and the registry 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 Exposed ports section, set the existing port to 5000.
- Choose a name for your App and Service, for example
koyeb-flask-demo
, and click Deploy.
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 koyeb.app
.
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.