This is a simple FastAPI project created for learning purposes.
It includes basic API routes and demonstrates how to build and run a FastAPI application.
- FastAPI setup
- Simple in-memory data (BOOKS list)
- Automatic Swagger documentation
- Python 3.9+
- pip
- Clone the repository:
git https://github.com/RalitsaTerzieva/fastapi_books
cd fastapi_books- Create virtual environment:
python -m venv venv
- Activate virtual environment:
source venv/bin/activate
- Install dependencies:
pip install "fastapi[standard]"
fastapi dev books.py
Or using uvicorn:
uvicorn books:app --reload
This project can also be run using Docker, which allows the application to run inside a containerized environment.
Containerization ensures that the application runs consistently across different machines without dependency or Python version conflicts.
Docker builds an image from a Dockerfile, and that image is used to start a container that runs the application.
Create a file named:
Dockerfile
Add the following content:
FROM python:3.11
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Explanation of the Dockerfile steps:
| Instruction | Description |
|---|---|
FROM python:3.11 |
Uses an official Python base image |
WORKDIR /app |
Sets the working directory inside the container |
COPY requirements.txt . |
Copies dependency file into the container |
RUN pip install --no-cache-dir -r requirements.txt |
Installs project dependencies without caching to reduce image size |
COPY . . |
Copies the project source code |
CMD |
Starts the FastAPI application using Uvicorn |
Run the following command in the project root directory:
docker build -t fastapi-books .This command creates a Docker image named:
fastapi-books
Start the application inside a Docker container:
docker run -p 8000:8000 fastapi-booksExplanation of the port mapping:
-p 8000:8000
- The first
8000is the port on your local machine. - The second
8000is the port inside the container where the application is running.
Open your browser and go to:
http://localhost:8000/docs
This will display the automatically generated interactive API documentation from FastAPI.
Using Docker provides several advantages:
- Consistent development environment
- No dependency conflicts
- Easy deployment to cloud environments
- Simplified application distribution
- Faster onboarding for new developers
If you want to share your image, you can push it to your own Docker Hub account.
First, tag the image with your Docker Hub repository name:
docker tag fastapi-books <your-dockerhub-username>/fastapi-docker:latestExample (replace with your username):
docker tag fastapi-books johnsmith/fastapi-docker:latestThen log in to Docker Hub:
docker loginFinally, push the image:
docker push <your-dockerhub-username>/fastapi-docker:latestAnyone can pull and run it:
docker pull <your-dockerhub-username>/fastapi-docker:latest
docker run -p 8000:8000 <your-dockerhub-username>/fastapi-dockerThis uses your personal repository on Docker Hub so images are tied to your account.