Getting your hands dirty with FastAPI

Shruti
featurepreneur
Published in
3 min readJan 20, 2021

Building a FastAPI application and dockerizing it.

FAST API

Prerequisites:

  1. Install & setup Docker

2. For FastAPI and Uvicorn installation follow this article

You can start your application in 3 ways if you follow this article:

1. Using Uvicorn

1. Create a FastAPI application

Create a file named main.py inside a folder called app and add the following code to it.

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

db = []

class Book(BaseModel):
title: str
author: str

@app.get('/')
def index():
return {'key' : 'value'}

@app.get('/books')
def get_books():
return db

@app.get('/books/{book_id}')
def get_book(book_id: int):
return db[book_id-1]

@app.post('/books')
def create_book(book: Book):
db.append(book.dict())
return db[-1]

@app.delete('/books/{book_id}')
def delete_book(book_id: int):
db.pop(book_id-1)
return {}

2. Running the application

Set up a virtual environment and activate it.

virtualenv venv
source venv/bin/activate

Now, cd into the project folder and install FastAPI and Uvicorn using pip or pip3.

pip3 install fastapi
pip3 install uvicorn

Now to run your application:

uvicorn app.main:app --reload

At this point, Uvicorn is running on http://127.0.0.1:8000 and you should see this output in there:

Now, check http://127.0.0.1:8000/docs. This will open SwaggerUI.

The same can be viewed using ReDoc as well at http://127.0.0.1:8000/redoc.

You can now use the GET(with and without id), POST, and DELETE services. You have successfully created a FastAPI application if all the services are working.

You can exit the virtual environment using:

deactivate

2. Using Docker

You can dockerize your application and run it after creating a docker image without manually installing your project requirements/dependencies.

At the root of the project, create a file and name it Dockerfile. Add the following code to it.

FROM python:3.7

RUN pip install fastapi uvicorn

EXPOSE 80

COPY ./app /app

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]

Now, in your terminal, cd into your project and type:

docker build -t myimage .

This will create an image that can be checked with this command:

docker images

To run the application using docker,

docker run -p 80:80 -it myimage

Now your application is running at http://0.0.0.0:80. Similarly, SwaggerUI and Redoc can be viewed at http://0.0.0.0:80/docs and http://0.0.0.0:80/redoc respectively.

3. Using docker-compose

Adding docker-compose will allow you to get your application up and running using a single command which will install all requirements for you.

Now create another file at the root of the project and name it docker-compose.yaml. Add the following code.

version: "3"
services:
fastapi:
build: .
ports:
- "80:80"

To start your application:

docker-compose up

Now your application is running at http://0.0.0.0:80. Similarly, SwaggerUI and Redoc can be viewed at http://0.0.0.0:80/docs and http://0.0.0.0:80/redoc respectively.

You can refer to this project on Github.

--

--

Shruti
featurepreneur

Tech builds shouldn't be intimidating – I break down my projects in detail, turning my learning experiences into your shortcuts.