27
Getting Started With Fast API and Docker
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints to validate, serialize, and deserialize data, and automatically auto-generate OpenAPI documents.
FastAPI has is one of the latest python api's for web development. It has some of this features:
It is fast when we compare it to other major Python frameworks like Flask and Django.
This is one of exciting feature of FastAPI that it supports asynchronous code out of the box using the async/await Python keywords.
It takes like 8 lines of code to comeup with a hello, word program
It has a feature of test driven development with the help using the TestClient provided by fastAPI.
It has one of the easiest documentation that one can understand.
You can easily deploy your FastAPI app via Docker using FastAPI provided docker image
Python 3.6+
pip install fastapi
pip3 install fastapi uvicorn
First create a folder at your own location of choice
mkdir foldername && cd foldername
create a virtual environment inside the folder you created
python3 -m venv env
activate the virtual environment created called env
source env/bin/activate
Create a python file with the following code and save as myapp.py
from fastapi import FastAPI, Depends
app = FastAPI()
@app.get('/')
async def root():
return {'message': 'Hello World!'}
Then to run the server, we use the following command:
uvicorn myapp:app --reload
To know it it works with no error, you will see this in your commandline
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [84109] using statreload
INFO: Started server process [84111]
INFO: Waiting for application startup.
INFO: Application startup complete.
Now to open in browser, use the following link
http://127.0.0.1:8000/docs
you will see the following output
before going to docker, we can create a requirement.txt file that has all the tools that you have installed in one place by running;
pip3 freeze > requirement.txt
- speed - docker has both execution speed,startuo speed and operational speed.
- consistency - Build an image once and use it any where. The same image that is used to run the tests is used in production. This avoids the works in my machine problems.
- Flexibility - Containers are portable. Well to an extent (as long as the host is running some form of linux or linux vm).
Create a docker file and save with this commands
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt ./requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
Then build your image
docker build -t myimage ./
27