A repository is built as a Python project if it contains a dependencies file (requirements.txt, pyproject.toml, Pipfile, setup.py) and a recognized application: Django, FastAPI, Flask or one of the frameworks listed below. A simple main.py, app.py, server.py or run.py script is also enough.
Python version
The image is python:<version>-alpine. The version is read, in order, from .python-version, runtime.txt, Pipfile then requires-python in pyproject.toml. With no indication, it is Python 3.12.
# .python-version
3.13
A lower bound like requires-python = ">=3.9" gives 3.12, not 3.9. An upper bound like ">=3.9,<3.12" gives 3.9.
Installing dependencies
The build chooses the tool based on the files present:
| File present | Command |
|---|---|
uv.lock | uv export --frozen --no-dev, then pip install of the result |
poetry.lock or [tool.poetry] section | poetry install --only main --no-root |
Pipfile with Pipfile.lock | pipenv install --system --deploy |
Pipfile alone | pipenv install --system --skip-lock |
requirements.txt (or requirements/production.txt, prod.txt, base.txt) | pip install -r <file> |
pyproject.toml or setup.py with no other file | pip install . |
Development dependencies are not installed with uv and Poetry. Gunicorn or Uvicorn are added if they are missing from your dependencies.
Unlike Node.js, a lock file is not mandatory. Without one, two builds may install different versions: pin the versions in requirements.txt (fastapi==0.115.6) or commit uv.lock or poetry.lock.
System libraries
The image is based on Alpine. The following packages are compiled thanks to libraries added automatically when the build sees them in your dependencies:
| Python package | Added for compilation |
|---|---|
psycopg2, psycopg | PostgreSQL client |
mysqlclient | MariaDB client |
pillow | libjpeg, zlib, freetype |
lxml | libxml2, libxslt |
weasyprint, pycairo, shapely, gdal, pyodbc, python-ldap, pycurl | corresponding libraries |
The base (build-base, libffi-dev, openssl-dev, git) is always present. A package with no precompiled version for Alpine is compiled from source: the first build is longer, later ones use the cache.
What does not work on Alpine: TensorFlow, PyTorch and opencv-python. The build log says so. Use opencv-python-headless, or a Dockerfile based on python:3.12-slim.
Variables set in the image
| Variable | Value | Effect |
|---|---|---|
PYTHONUNBUFFERED | 1 | print output appears right away in the Logs tab |
PYTHONDONTWRITEBYTECODE | 1 | No .pyc files written to disk |
PYTHONPATH | /app:/app/src | Only if the repository has Python code in src/ |
FastAPI
# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"message": "Hello"}
@app.get("/healthz")
def healthz():
return "ok"
# requirements.txt
fastapi==0.115.6
uvicorn[standard]==0.34.0
Generated command: uvicorn main:app --host 0.0.0.0 --port $PORT. The build itself looks for the file that creates the FastAPI() object: api/main.py gives api.main:app. Litestar, Starlette and Quart are started the same way.
Behind Webgune, the visitor's address arrives in X-Forwarded-For. For Uvicorn to take it into account, replace the start command with:
uvicorn main:app --host 0.0.0.0 --port $PORT --proxy-headers --forwarded-allow-ips="*"
Django
Generated command: gunicorn <project>.wsgi:application --bind 0.0.0.0:$PORT, based on the location of wsgi.py. Without wsgi.py, it is python manage.py runserver, which should be avoided in production.
If your settings define STATIC_ROOT, the build runs python manage.py collectstatic --noinput. If this command fails (often because SECRET_KEY is missing at build time), the build continues and the log says so.
Variables to plan for:
DJANGO_SECRET_KEY=a-long-random-value
DJANGO_ALLOWED_HOSTS=my-site.webgune.dev,www.example.com
DATABASE_URL=postgresql://user:password@host:5432/database
# settings.py
import os
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "").split(",")
CSRF_TRUSTED_ORIGINS = [f"https://{h}" for h in ALLOWED_HOSTS if h]
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
STATIC_ROOT = BASE_DIR / "staticfiles"
SECURE_PROXY_SSL_HEADER tells Django the request arrived over HTTPS: without it, forms may fail with a CSRF error. To serve static files, add WhiteNoise (whitenoise.middleware.WhiteNoiseMiddleware).
Migrations cannot run at build time, which has no access to the database. Run them at startup with a custom command:
python manage.py migrate --noinput && gunicorn myproject.wsgi:application --bind 0.0.0.0:$PORT --workers 2
Read Database migrations first: a migration run at startup causes problems when rolling back to the previous version.
Flask
Generated command: gunicorn '<module>:<app>' --bind 0.0.0.0:$PORT. Application factories are recognized: a def create_app(): in app/__init__.py gives gunicorn 'app:create_app()'. Bottle and Falcon are started the same way.
For several processes per instance:
gunicorn app:app --bind 0.0.0.0:$PORT --workers 2 --timeout 60
Each Gunicorn process loads the application into memory. Count one process's memory multiplied by the number of --workers (Limits and resources).
Data applications
| Tool | Generated command |
|---|---|
| Streamlit | streamlit run <file> --server.port=$PORT --server.address=0.0.0.0 |
| Gradio | python <file>, with GRADIO_SERVER_NAME=0.0.0.0 and GRADIO_SERVER_PORT=$PORT |
| Dash | gunicorn '<module>:server' if your code contains server = app.server, otherwise python <file> |
| Chainlit | chainlit run <file> --host 0.0.0.0 --port $PORT |
| Panel | panel serve <file> --address 0.0.0.0 --port $PORT |
Tornado, aiohttp, CherryPy and simple scripts are launched with python <file>: your code must read PORT and listen on 0.0.0.0.
import os
from aiohttp import web
app = web.Application()
web.run_app(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
Documentation sites
MkDocs (mkdocs.yml), Pelican (pelicanconf.py) and Sphinx (conf.py or docs/conf.py) are built as a static site and served by Nginx. A repository that contains both mkdocs.yml and a Flask application stays an application.
Replacing the command
A Procfile at the root of the repository replaces the generated command:
web: gunicorn myproject.wsgi:application --bind 0.0.0.0:$PORT --workers 3
The service's start command, in its settings, has the same effect without touching the repository (Custom start command).
Common errors
| Symptom | Cause | What to do |
|---|---|---|
| "Framework not detected" | No dependencies file, or application not found | Add requirements.txt, or a Procfile |
Compilation fails on gcc or a .h header | Package with no Alpine version and no known library | Dockerfile based on python:3.12-slim |
DisallowedHost | Domain missing from ALLOWED_HOSTS | Add the service's domain |
| CSRF error when submitting a form | Django does not know the request is over HTTPS | SECURE_PROXY_SSL_HEADER and CSRF_TRUSTED_ORIGINS |
ModuleNotFoundError on your own code | Code in a subfolder that cannot be imported | Put the code in src/ or at the root |
| Stop with code 137 | Too many --workers for the service's memory | Reduce --workers |
Updated on