FastAPI from Empty Folder to Deployed: The Full Build-Along
FastAPI from Empty Folder to Deployed: The Full Build-Along
Most FastAPI tutorials stop where the interesting part begins. They end with a working endpoint on a local port and leave the rest — validation, configuration, containers, TLS, health checks, logs — to be discovered one production incident at a time. This build-along covers the whole path from an empty folder to a service you can leave running.
It assumes Python 3.11 or newer and a Linux server you can reach over SSH. Every step is a decision you would otherwise make under pressure later.
What you will have at the end
A small API with two endpoints, typed request and response models, configuration loaded from environment variables, an automated test, a container image that runs as a non-root user, a compose file that brings the stack up with one command, a reverse proxy terminating HTTPS, a health endpoint, and structured logs you can actually read.
That is the minimum shape of a service that survives contact with real traffic.
How to structure the project from the first file
Create the folder, a virtual environment, and a requirements file with three entries to start: fastapi, uvicorn, and pydantic-settings. Keep the application in a single package — app/ with main.py, models.py and settings.py — and resist splitting it into modules until there is a reason. Early structure is easier to add than to remove, and a two-file service is easier to test than a ten-file one.
Why validation and settings belong in the first hour
Two habits prevent the majority of later bugs. Typed models for every request and response: FastAPI will reject malformed input for you and document the shape automatically, which removes a whole class of defensive coding. Settings from the environment, never hard-coded: database URLs, API keys and feature flags belong in environment variables loaded through one settings object, so the same image runs in development and production without edits.
The payoff is testability. If the app reads configuration from one place, a test can replace it, and you can verify behaviour without a running database.
How to containerise it without surprises
Four rules. Use a slim base image and pin its version. Run as a non-root user, because a container running as root turns a small vulnerability into a large one. Copy requirements.txt and install before copying the application code, so rebuilds reuse the dependency layer. And start the app with a command that binds to 0.0.0.0 — the single most common reason a container runs but cannot be reached.
| Step | Command shape | Why |
|---|---|---|
| Build | docker compose build | Reproducible image from the same source |
| Up | docker compose up -d | App plus proxy in one step |
| Check | curl the health endpoint | Running is not the same as serving |
| Logs | docker compose logs -f | First twenty lines tell you what broke |
What to put in front of it on the server
A reverse proxy handles TLS, redirects and compression so the application does not have to. Terminate HTTPS at the proxy, forward to the app on its internal port, and pass the original host header — otherwise generated URLs come back with the wrong scheme and produce redirect loops. Keep secrets in the environment or a file readable only by the service, never in the compose file that lives in git.
How to know it is healthy in production
Add a health endpoint that checks what actually matters — that the process responds and that any required dependency is reachable — and point both the container health check and your monitoring at it. Log requests with a request identifier so a user report can be traced to a specific call. And set the process to restart on failure, because the alternative is discovering at breakfast that the service stopped at two in the morning.
For the groundwork, building your first REST API with FastAPI covers the endpoint layer in more detail, the Docker deployment checklist covers the five failures that break a first deploy, and the beginner deployment guide is the shorter path if you only need something live today.
Build it once, deploy it many times
The developer section of the library collects the FastAPI, Docker and API guides in one place.