Going Live: Deployment
Going Live: Deployment
Section titled “Going Live: Deployment”You have built a robust, tested application. Now it is time to share it with the world. Deployment is the process of moving your application from your local development machine to a production server.
Concepts: Process Managers
Section titled “Concepts: Process Managers”When developing locally, we use uvicorn main:app --reload. The --reload flag is great for development but consumes resources checking for file changes. In production, we need stability and performance.
A Process Manager is a tool that ensures your application stays alive. If your application crashes, the process manager restarts it. It can also manage multiple copies (workers) of your application to handle more traffic.
Containerization with Docker
Section titled “Containerization with Docker”The most common and recommended way to deploy FastAPI applications today is using Docker. A Docker container packages your code, the Python runtime, and all libraries into a single, portable unit.
Here is a standard Dockerfile for a FastAPI application:
# Start from the official Python base imageFROM python:3.9
# Set the current working directory inside the containerWORKDIR /code
# Copy the requirements file first to leverage Docker cacheCOPY ./requirements.txt /code/requirements.txt
# Install dependenciesRUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Copy the application codeCOPY ./app /code/app
# Command to run the application using UvicornCMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]By building this image, you ensure that your application runs exactly the same way on a cloud server as it does on your machine.
Production Server: Uvicorn and Gunicorn
Section titled “Production Server: Uvicorn and Gunicorn”While Uvicorn is a lightning-fast ASGI server, it is not primarily a process manager. Gunicorn is a mature, production-grade process manager.
For heavy workloads, the recommended setup is to use Gunicorn as the manager, with Uvicorn workers handling the actual data. This gives you the best of both worlds: Gunicorn’s reliability and Uvicorn’s speed.
You can achieve this by installing gunicorn and running a command like:
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:80Cloud Providers and HTTPS
Section titled “Cloud Providers and HTTPS”Because FastAPI is based on standard Python and ASGI, you can deploy it anywhere:
- PaaS (Platform as a Service): Services like Heroku, Google App Engine, or Azure App Service.
- CaaS (Containers as a Service): Kubernetes, AWS ECS, or Google Cloud Run (highly recommended for Docker images).
- Serverless: AWS Lambda (using an adapter like Mangum).
Finally, remember that modern web APIs must use HTTPS. Typically, you will place a “Termination Proxy” like Nginx or Traefik in front of your Uvicorn/Gunicorn application. This proxy handles the encryption certificates (e.g., Let’s Encrypt) and passes plain HTTP requests to your app container, simplifying your application logic.