Skip to content

Run Python Flask with SQLite

In this guide we create and deploy a Python Flask application using SQLite. To run this example, follow these steps:

  1. Install the kraft CLI tool and a container runtime engine, e.g. Docker.

  2. Clone the examples repository and cd into the examples/python3.12-flask3.0-sqlite/ directory:

Terminal window
git clone https://github.com/kraftcloud/examples
cd examples/python3.12-flask3.0-sqlite/

Make sure to log into Unikraft Cloud by setting your token and a metro close to you. We use fra0 (Frankfurt, πŸ‡©πŸ‡ͺ) in this guide:

Terminal window
# Set Unikraft Cloud access token
export UKC_TOKEN=token
# Set metro to Frankfurt, DE
export UKC_METRO=fra0

When done, invoke the following command to deploy this application on Unikraft Cloud:

Terminal window
kraft cloud deploy -p 443:8080 -M 512 .
Terminal window
[●] Deployed successfully!
β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ name: python312-flask30-sqlite-qodkd
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ uuid: e00e7aca-962d-409c-87c2-c245ca08b54b
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ state: running
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ url: https://lingering-orangutan-840mmdvd.fra0.kraft.host
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€ image: python312-flask30-sqlite@sha256:bdb0bf35a9675b9b3836cbb626606da0606334d91768c7ba31195c3062d6f517
β”œβ”€β”€β”€β”€β”€ boot time: 166.25 ms
β”œβ”€β”€β”€β”€β”€β”€β”€β”€ memory: 512 MiB
β”œβ”€β”€β”€β”€β”€β”€β”€ service: lingering-orangutan-840mmdvd
β”œβ”€β”€ private fqdn: python312-flask30-sqlite-qodkd.internal
β”œβ”€β”€β”€β”€ private ip: 172.16.3.3
└────────── args: /usr/bin/python3 /app/server.py

In this case, the instance name is python312-flask30-sqlite-qodkd and the URL is https://lingering-orangutan-840mmdvd.fra0.kraft.host. They are different for each run.

Use curl to query the Unikraft Cloud instance of the Python-based HTTP web server:

Terminal window
curl https://young-night-5fpf0jj8.fra0.kraft.host
<!doctype html>
<html lang="en">
[...]
<h1> Welcome to FlaskBlog </h1>
<a href="/1">
<h2>First Post</h2>
</a>
<span class="badge badge-primary">2024-02-15 22:01:07</span>
<a href="/1/edit">
<span class="badge badge-warning">Edit</span>
</a>
<hr>
<a href="/2">
<h2>Second Post</h2>
</a>
</html>

At any point in time, you can list information about the instance:

Terminal window
kraft cloud instance list
NAME FQDN STATE CREATED AT IMAGE MEMORY ARGS BOOT TIME
python312-flask30-sqlite-qodkd lingering-orangutan-840mmdvd.fra0.kraft.host running 1 minute ago python312-flask30-sqlite@sha256... 512 MiB /usr/bin/python3 /app/server.py 166250us

When done, you can remove the instance:

Terminal window
kraft cloud instance remove python312-flask30-sqlite-qodkd

Implementation Details

The application uses several files, listed below:

  • schema.sql: SQL schema for the database
  • init_db.py: script to initialized the database file from schema.sql in /app/database.db
  • server.py + templates/: the actual Flask-based implementation: Python source code file and HTML template files
  • requirements.txt: pip configuration file to install required packages: Flask and SQLite
  • Kraftfile: the Unikraft Cloud specification
  • Dockerfile: the Docker-specified application filesystem
DROP TABLE IF EXISTS posts;
CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
content TEXT NOT NULL
);

Lines in the Kraftfile have the following roles:

  • spec: v0.6: The current Kraftfile specification version is 0.6.

  • runtime: python:3.12: The Unikraft runtime kernel to use is Python 3.12.

  • rootfs: ./Dockerfile: Build the application root filesystem using the Dockerfile.

  • cmd: ["/usr/bin/python3", "/app/server.py"]: Use /usr/bin/python3 /app/server.py as the starting command of the instance.

Lines in the Dockerfile have the following roles:

  • FROM scratch: Build the filesystem from the scratch container image, to create a base image.

  • COPY rules result in the copying of required files. The requirements.txt file is copied before running pip3 install. Other files (including schema.sql, init_db.py) are copied for the initialization of the database.

  • RUN commands trigger actions such as installing Python packages and initializing the database.

  • The relevant contents required by the application are copied to the new scratch image:

    • the SQLite dynamic library: /usr/lib/x86_64-linux-gnu/libsqlite3.so.0
    • the Python package files: /usr/local/lib/python3.12
    • the /app directory

Customize your Application

To customize the application, update application files in the repository:

  • schema.sql: Update the database schema.
  • server.py, templates/: Update the Flask application..
  • requirements.txt, Dockerfile: Update the list of Python packages used by the application.
  • Kraftfile: Update the command line used to start the application.

The following options are available for customizing the application:

  • If only updating the implementation in the server.py source file or template/ directory, and the database schema in schema.sql, no other change is required.

  • If new files are added, these have to be copied in the application filesystem, using the COPY command in the Dockerfile.

  • If new dependencies are added in requirements.txt, the RUN pip3 install in the Dockerfile command should take care of everything. It may be the case that other files, such as the /usr/lib/x86_64-linux-gnu/libsqlite3.so.0 for SQLite, are required to be copied via COPY commands in the Dockerfile. More extensive changes may require expanding the Dockerfile with additional Dockerfile commands.

  • If a new Python source file is added that’s running the main() function, update the cmd line in the Kraftfile and replace server.py to run that file when creating the instance.

Learn More

Use the --help option for detailed information on using Unikraft Cloud:

Terminal window
kraft cloud --help

Or visit the CLI Reference.