Deploy a Flask application
Flask suits APIs and small web applications that need to stay simple. On app hosting, the essentials are to run Flask on the provided port, on 0.0.0.0, with debug mode off. The rest comes down to route structure and post-deploy checks.
Minimal application
import os
from flask import Flask, jsonify
app = Flask(__name__)
@app.get("/")
def index():
return jsonify(service="example", status="ok")
@app.get("/health")
def health():
return jsonify(status="ok"), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", "8000")))requirements.txt:
Flask==3.0.3
gunicorn==22.0.0app.run() blocks and serves requests. On the platform the main file is executed directly, so the __main__ block is reached.
Production mode
Never leave debug=True on an accessible service: the interactive debugger allows code execution when access is not filtered. Flask also prints an explicit warning when the development server is used in production.
The built-in server is fine for low traffic and diagnostics: it remains useful to validate a route, but it should not become the target of sustained traffic. For real load, use a WSGI server such as Gunicorn or Waitress, if your plan allows changing the start command:
gunicorn --bind 0.0.0.0:$PORT --workers 2 main:appTwo workers are often enough on a one-vCPU plan; each worker has its own memory. Do not multiply workers without watching RAM.
Routes and errors
- A fast
/healthroute with no heavy dependency. - Consistent JSON responses, including for errors.
- Input validation before the database.
- A size limit (
MAX_CONTENT_LENGTH) if you accept uploads. - An error handler that logs the stack on the server without returning it to the client.
Large static files and uploads should be limited or delegated to suitable storage; the container is not a CDN.
Check
curl -i http://ADRESSE_FOURNIE:PORT/
curl -i http://ADRESSE_FOURNIE:PORT/healthExpect 200 and a JSON body. Request an unknown route: 404, not a Flask traceback. If the service is "online" but returns a connection error, check the host (0.0.0.0) and port before suspecting the code.
Advertisement
Was this article helpful?
Read next
- Configure a Python applicationSet up main.py, dependencies, port and variables for a Python application.
- Deploy a FastAPI APIStart Uvicorn correctly, expose a health route and control the documentation.
- Understanding requirements.txtPin versions, install with pip and reproduce an identical environment.