Flask with MongoDB
Flask is a lightweight WSGI web application framework in Python, and MongoDB is a NoSQL database that stores data in JSON-like documents. This example deploys three services on Unikraft Cloud: Nginx (reverse proxy), Flask (backend), and MongoDB (database).
Credits: This example is based on this Awesome Compose example.
Deployment
To run this example, follow these steps:
-
Install the CLI. Use the unikraft CLI or the legacy kraft CLI. You need a BuildKit builder. The easiest way to get one is via Docker. Alternatively, you can also directly set up and use BuildKit, see the quick start.
The unikraft CLI is the current standard, while kraft is the legacy version. Choose one of the CLIs below and only run the commands associated with it for the rest of this guide.
-
Clone the
examplesrepository andcdinto theexamples/nginx-flask-mongodirectory:
Code
Make sure to log into Unikraft Cloud and pick a metro close to you.
This guide uses fra (Frankfurt, 🇩🇪):
Create the volume
Create a volume for MongoDB data persistence:
The output shows the volume details:
Deploy MongoDB
First, deploy the MongoDB instance.
MongoDB is an internal service (not publicly accessible), reached via the mongo.internal domain:
The output shows the MongoDB instance details:
Deploy Flask
Next, deploy the Flask backend.
It connects to MongoDB using the MONGO_SERVER_URL environment variable and is reached internally via backend.internal:
The output shows the Flask instance details:
Deploy Nginx
Finally, deploy Nginx as the public-facing reverse proxy.
It forwards requests to the Flask backend at backend.internal:9091:
The output shows the Nginx instance details including its public FQDN:
You can list all deployed instances with:
Test the deployment
The FQDN of the Nginx instance can be found in the FQDN column of the unikraft instances list output above.
Use curl to query it (replace with your actual FQDN):
Code
Code
Clean up
When done, remove the instances and volume:
Learn more
Use the --help option for detailed information on using Unikraft Cloud:
Or visit the CLI Reference or the legacy CLI Reference.
- Flask Documentation
- MongoDB Documentation
- Nginx Documentation
- Awesome Compose
- Unikraft Cloud's Documentation
- Building
DockerfileImages withBuildkit