Skip to content

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.

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.

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 image
FROM python:3.9
# Set the current working directory inside the container
WORKDIR /code
# Copy the requirements file first to leverage Docker cache
COPY ./requirements.txt /code/requirements.txt
# Install dependencies
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Copy the application code
COPY ./app /code/app
# Command to run the application using Uvicorn
CMD ["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.

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:

Terminal window
gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:80

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.