Host a Python app (Flask or Django) with a database
Run a Flask or Django app on DASTER as a public Docker image, with PostgreSQL in the same project and DATABASE_URL given to the app at every deployment.
Published · DASTER
Last updated
To host a Flask or Django app on DASTER today, build it into a Docker image, push the image to a public registry and deploy it as a web service. Set up PostgreSQL in the same project, and the app receives DATABASE_URL at every deployment. DASTER does not build Python from source yet, so the image is the way in.
This post is for the searches "host flask app", "host django app" and "host python app online". It shows a small Flask app that reads PostgreSQL, the files it needs, and the exact steps. A short Django section follows.
Can DASTER run Python from my repository?
Not from source yet. Hosting builds and runs Node and npm servers, static output served by Node, and restricted Dockerfiles that only copy files. A Python repository may be detected as Flask or Django, but detection does not mean DASTER can build it.
What does work is Docker image: you build the image yourself, push it to a public registry, and DASTER runs it. The image carries Python, your packages and its own start command, so DASTER does not need to know about Python at all.
| Source | Python today |
|---|---|
| GitHub repository | No, Python is not built yet |
| .zip upload | No, same build as a repository |
| Public Docker image | Yes, as a web service or worker |
What does the app need to run on DASTER?
- A public image on one of the registries listed in Deploy a Docker image, up to 1 GiB uncompressed. Private registries and registry passwords are not supported.
- An ARM build. DASTER's hosting servers use 64-bit ARM processors, so build for
linux/arm64. - A server that listens on a port between 1024 and 65535, and a fast health check path.
- No writes outside
/tmp. The container runs as a normal user with a read-only file system and a small/tmp. Keep data in PostgreSQL or Storage.
Write the Flask app
Three files in one folder.
app.py:import os import psycopg from flask import Flask, jsonify, request app = Flask(__name__) # DATABASE_URL uses sslmode=verify-full. Point the driver at the # system certificate bundle so it can check the server's certificate. CA_FILE = "/etc/ssl/certs/ca-certificates.crt" def connect(): return psycopg.connect(os.environ["DATABASE_URL"], sslrootcert=CA_FILE) @app.get("/health") def health(): return "ok" @app.get("/todos") def list_todos(): with connect() as conn: rows = conn.execute( "select id, title, done from app.todos order by id limit 20" ).fetchall() return jsonify([{"id": r[0], "title": r[1], "done": r[2]} for r in rows]) @app.post("/todos") def add_todo(): title = (request.get_json(silent=True) or {}).get("title", "").strip() if not title: return jsonify(error="title is required"), 400 with connect() as conn: row = conn.execute( "insert into app.todos (title) values (%s) returning id", (title,), ).fetchone() return jsonify(id=row[0], title=title, done=False), 201requirements.txt:flask==3.1.0 gunicorn==23.0.0 psycopg[binary]==3.2.3Dockerfile:FROM python:3.12-slim ENV PYTHONUNBUFFERED=1 WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY app.py . EXPOSE 8000 CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "2", "app:app"]The start command is the image's
CMD: Gunicorn serves the app on port 8000 with two workers. TheRUN pip installline runs on your machine when you build, not on DASTER.Build and push the image
On your machine, build for ARM and push to a public repository on a supported registry:
docker buildx build --platform linux/arm64 \ -t <registry>/<you>/flask-todos:1 --push .Make the repository public in the registry's settings. DASTER pulls images without a password.
Set up PostgreSQL
Create an App project, open Backend Cloud and choose Set up Backend Cloud. Your organization's first PostgreSQL database is the smallest size (512 MB, 10 connections). The setup page follows the database until it is ready.
Then open PostgreSQL › SQL Editor and create the table:
create table app.todos ( id bigint generated always as identity primary key, title text not null, done boolean not null default false );Your tables live in the
appschema, which is why the code saysapp.todos. Read more in PostgreSQL for startups.Deploy the image
In Hosting, choose Deploy an app and pick Web service and a size. Under the source, choose Docker image and enter
<registry>/<you>/flask-todos:1. Set the port to8000and the health check path to/health. Choose Review, then Deploy to Development.DASTER pins the image to its digest when the deployment starts, so a later push of the same tag changes nothing until you deploy again. When the deployment is LIVE, open
https://<app id>.apps.daster.io/todos.Ship a new version
Build and push a new tag, such as
flask-todos:2, then deploy the service again with the new reference. When Development works, owners and admins can use Promote to Production to run the same image there.
How do I run Django the same way?
Django needs the same three things: an image, a port and DATABASE_URL. Read the URL in settings.py:
import os
from urllib.parse import unquote, urlparse
db = urlparse(os.environ.get("DATABASE_URL", "postgresql://localhost/dev"))
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": db.path.lstrip("/"),
"USER": unquote(db.username or ""),
"PASSWORD": unquote(db.password or ""),
"HOST": db.hostname,
"PORT": db.port or 5432,
"OPTIONS": {
"sslmode": "verify-full",
"sslrootcert": "/etc/ssl/certs/ca-certificates.crt",
},
}
}
ALLOWED_HOSTS = [".apps.daster.io"]The pooler does not accept session options in the connection string, so set the schema after each connection opens. In one of your apps, todos/apps.py:
from django.apps import AppConfig
from django.db.backends.signals import connection_created
def use_app_schema(sender, connection, **kwargs):
with connection.cursor() as cursor:
cursor.execute("set search_path to app")
class TodosConfig(AppConfig):
name = "todos"
def ready(self):
connection_created.connect(use_app_schema)Now Django's own tables and yours live in the app schema. Add django, gunicorn and psycopg[binary] to requirements.txt, and run migrations when the container starts, since only apps hosted on DASTER can reach the database:
CMD ["sh", "-c", "python manage.py migrate && gunicorn mysite.wsgi --bind 0.0.0.0:8000"]Replace mysite with your project's name, and add your own domain to ALLOWED_HOSTS when you connect one.
What should I know before I start?
- The database is reachable only from apps hosted on DASTER. You cannot connect with
psqlor run migrations from your laptop yet; use the SQL Editor or run them when the container starts. - Variables are given at runtime. Read
DATABASE_URLwhen a request arrives or the app starts, not while the image builds. - Preview deployments never receive the connection, so a branch preview cannot touch your Development data.
- Sizes: Starter (0.5 vCPU, 256 MB) or Standard (1 vCPU, 1 GB), up to 2 instances. Keep Gunicorn's workers low on Starter.
- Apps run in Mumbai (
ap-south-1) today. Prices are not final.
To compare this with a Node app built from GitHub, read How to host a full-stack app with a database.
DASTER is in testing and sign-up is waitlist-only. Join the waitlist and you get access 20 days after you join.
Questions about hosting a Python app on DASTER
Can I push my Flask repository and let DASTER build it?
Not yet. Python is not built from a repository or a .zip today. Build a Docker image yourself, push it to a public registry and deploy it with Docker image.
Does the app get DATABASE_URL by itself?
Yes. Once Backend Cloud is ready, every deployment gives the app DATABASE_URL, DASTER_URL and DASTER_SERVER_KEY. A variable you set yourself under one of these names is kept.
Why does the code pass sslrootcert?
DATABASE_URL asks for sslmode=verify-full, so the driver must check the server's certificate. Python's PostgreSQL driver needs to be told where the trusted certificates are; the slim Python image keeps them in /etc/ssl/certs/ca-certificates.crt.
Can I use a private registry?
No. The image must be public, on one of the registries the docs list. Private registries and registry passwords are not supported.
Can I run a Python background worker?
Yes. A Docker image can run as a web service or as a worker. Cron jobs and functions are not offered for images.