FastAPI is a modern, high-performance Python web framework.
You can use uv to manage your FastAPI project, including installing dependencies, managing
environments, running FastAPI applications, and more.
Note
You can view the source code for this guide in the uv-fastapi-example repository.
From there, you can run the FastAPI application with:
$ uvrunfastapidev
uv run will automatically resolve and lock the project dependencies (i.e., create a uv.lock
alongside the pyproject.toml), create a virtual environment, and run the command in that
environment.
To deploy the FastAPI application with Docker, you can use the following Dockerfile:
Dockerfile
FROMpython:3.12-slim# Install uv.COPY--from=ghcr.io/astral-sh/uv:latest/uv/uvx/bin/
# Copy the application into the container.COPY./app
# Install the application dependencies.WORKDIR/appRUNuvsync--frozen--no-cache
# Run the application.CMD["/app/.venv/bin/fastapi","run","app/main.py","--port","80","--host","0.0.0.0"]