Skip to main content

Flask

Flask Flask is a lightweight and flexible Python Web framework, designed with simplicity and extensibility at its core. It does not enforce dependencies on specific libraries or architectures, providing only essential features (such as routing and template rendering). Developers can freely select components like databases and form validation (e.g., SQLAlchemy, WTForms). This "microframework" approach results in an extremely low learning curve while enabling the construction of complex applications through extensions. It is particularly well-suited for rapid development of small projects or serving as a foundation for microservices.

This guide describes how to deploy the Flask application on Tencent CloudBase through multiple approaches:

Create a Flask application

Note: If you already have a Flask application, you can skip this step.

To create a new Flask application, ensure that you have installed Python and Flask on your machine.

Set up the project in the directory by following the steps below.

Create a cloudrun-flask directory and cd into it.

Create a virtual environment

python -m venv env

Activate the virtual environment

source env/bin/activate

Install Flask

python -m pip install flask

In the cloudrun-flask directory, create a new file manage.py with the following content:

import os
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello():
return 'Hello world!'

1,from flask import Flask

  • This line imports the Flask class from the Flask framework, which is used to create and manage Web applications.

2,app = Flask(__name__)

  • This line creates an instance of the Flask class and assigns it to the app variable.
  • The name parameter helps Flask locate the application. It is essential for determining resource paths and error reporting.

3,@app.route('/')

  • The @app.route('/') decorator sets up the URL route for the application. When the root URL (/) is accessed, Flask will execute the function immediately following the decorator.

4,def hello()

  • The hello function returns a plain text message "Hello world!", which will be displayed in the browser when the application's root URL is accessed.

In the cloudrun-flask directory, execute python3 manage.py runserver 0.0.0.0:8080 to start the service. Open your browser and visit http://127.0.0.1:8080 to view the returned result.

Configuring Dependencies

Create the requirements.txt file: To track all dependencies for deployment, create a requirements.txt file:

pip freeze > requirements.txt

Note: It is safe to run the above command only in a virtual environment; otherwise, it will generate all installed python packages on the system. This may result in the application failing to start on cloud hosting.

With the above changes, your Flask application will be ready to deploy on Tencent CloudBase!

Configuring Dockerfile

Create a Dockerfile file in the cloudrun-flask directory with the following content:

FROM alpine

# The container's default time zone is UTC. To use Shanghai time, enable the following time zone setting command.
RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone

# Use a domestic mirror source to improve download speed.
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tencent.com/g' /etc/apk/repositories \
&& apk add --update --no-cache python3 py3-pip gcc python3-dev linux-headers musl-dev \
&& rm -rf /var/cache/apk/*

# Using the HTTPS Protocol to Access Container Cloud for Certificate Installation Invocation
RUN apk add ca-certificates

# Copy the current project to the /app directory (excluding files specified in .dockerignore)
COPY . /app

# Set Current Working Directory
WORKDIR /app

# Install dependencies to the specified /install folder
# Use a domestic mirror source to improve download speed.
RUN pip config set global.index-url http://mirrors.cloud.tencent.com/pypi/simple \
&& pip config set global.trusted-host mirrors.cloud.tencent.com \
&& pip install --upgrade pip --break-system-packages \
# If pip install scipy and other math packages fails, you can use apk add py3-scipy instead. Refer to https://pkgs.alpinelinux.org/packages?name=py3-scipy&branch=v3.13 for installation.
&& pip install --user -r requirements.txt --break-system-packages

# Execute the startup command.
# Writing multiple independent CMD commands is incorrect! Only the last CMD command will be executed, and all previous ones will be ignored, causing application errors.
# Refer to [Docker official documentation on CMD command](https://docs.docker.com/engine/reference/builder/#cmd)
CMD ["python3", "manage.py", "runserver","0.0.0.0:8080"]

With the above changes, your flask application will be ready to deploy on Tencent CloudBase!

Deployment to Cloud Hosting

Cloud Hosting provides multiple deployment methods to deploy your application:

Deploy via Console

Open Tencent CloudBase, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> select Folder for the code package type -> choose the cloudrun-flask directory for upload -> enter 8080 for the port -> click Create and wait for the creation to complete.

Deploying via cli

If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:

tcb cloudrun deploy -p 8080

After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.

Besides manual deployment, you can also install the aforementioned applications with one click:

One-Click Deployment from Template

One-Click Deployment from github