Docker Compose gives you the same setup on your laptop and your server: one command builds and starts the API and the frontend. This guide covers both Dockerfiles, the one setting people get wrong (build-time variables for the frontend), and going to production.
The backend image
With uv , the image is small and the install is reproducible from the lockfile:
FROM python:3.12-slim COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
WORKDIR /app COPY pyproject.toml uv.lock ./ RUN uv sync --frozen --no-dev COPY app ./app
EXPOSE 8000 CMD ["/app/.venv/bin/fastapi", "run", "app/main.py", "--port", "8000"]
Enter fullscreen mode
Exit fullscreen mode
Copying pyproject.toml and uv.lock before the code means dependency layers are cached until the dependencies change.…