To create a Docker image for a FastAPI backend, define a Dockerfile that packages Python runtime, dependencies, and application code, and then build it using the docker build command. Here is the standard, step-by-step process based on the official FastAPI Docker Documentation. Package Requirements: When the project in managed with with uv, its direct dependencies are declared in pyproject.toml and the exact resolved versions are stored in uv.lock. Add the packages the application needs with: uv add "fastapi[standard]" pydantic ---------------------------------- 1. Organize Project Structure ---------------------------------- Ensure the project files are organized cleanly, with all dependencies listed in a requirements.txt file (or pyproject.toml). Create an app directory and enter (change working directory to app/) it. Create an empty file __init__.py inside this folder. backend-fastapi |-- app/ | |-- __init__.py | '-- main.py |-- requirements.txt |-- pyproject.toml |-- .docerignore '-- Dockerfile Create a main.py: from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "q": q} ---------------------------------- 2. Create a .dockerignore File ---------------------------------- Create a .dockerignore file in root directory to prevent local clutter and sensitive credentials from being copied into the image. Files to ingnore: __pycache__, *.pyc, .venv, venv/, .env, .git ---------------------------------- 3. Write the Dockerfile ---------------------------------- Create a file named Dockerfile (with no file extension) in the root of the project. refer https://fastapi.tiangolo.com/deployment/docker. #Dockerfile ---------------------------------- # Start with lightweight Python base image FROM python:3.10 # Set the working directory inside the container. This is where requirements.txt file and app directory stored. WORKDIR /code # Copy only the requirements file first to leverage Docker layer caching COPY ./requirements.txt /code/requirements.txt # Install dependencies without caching pip files RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt # Copy your FastAPI application code into the container COPY ./app /code/app # Expose the port the app runs on EXPOSE 80 # Run the FastAPI application using the built-in CLI (or Uvicorn) CMD ["fastapi", "run", "app/main.py", "--port", "80"] (Note: Copying requirements.txt and installing packages before copying ./app is a key optimization. Docker caches each step as a layer. If only Python code is changed, Docker will reuse the cached dependency layer instead of reinstalling packages every time.) ---------------------------------- 4. Build the Docker Image ---------------------------------- Open a terminal in the root directory of the project (where the Dockerfile is located) and run: docker build -t fastapi-app:revA . -t fastapi-app: name and tag of the image so it is easy to reference later . tells Docker to use the current directory as the build context: build expects to find the Dockerfile and the main.py file in the directory where the command is invoked. ---------------------------------- 5. Run and Test the Container ---------------------------------- Once the build finishes, launch a running container from new image using: docker run -d --name fastapi-container -p 8000:80 fastapi-app:revA -d: Runs the container in detached mode (in the background). -p 8000:80: Maps port 8000 on local machine to port 80 inside the container. After the image has been built, the application can be run as a container with docker run, specifying the image name: docker run -p 127.0.0.1:8000:8000 fastapi-app:revA The API will now be accessible at http://localhost:8000.