17
Getting Started With FastAPI and Docker
For those of us in the tech industry, we may have come across the phrase 'API' several times. It is important to clarify what an API means. An API is the gateway to your application, the interface that users and other services can use to interact with it. Traditionally, building APIs has been a challenging task until recently when web frameworks like FastAPI were introduced. FastAPI is a modern, fast and high-perfomance web framework for building APIs with Python 3.6+ based on standard Python type hints. Today, we are going to take a deep dive into how FastAPI works and also learn what Docker is.
Since I have already outlined what FastAPI is, I shall discuss a few thing about Docker. Docker is an open source containerization platform which enables developers to package applications into containers, standardized executable components combining application source code with the operating system libraries and dependencies needed to run that code in any environment.
A Docker container is a virtualized runtime environment
that provides isolation capabilities for separating the execution of applications from the underpinning system. Containers simplify delivery of distributed applications, and have become increasingly popular as organizations shift to cloud-native development and hybrid multi-cloud environments. It is worth noting that developers can create containers without Docker, but Docker makes it easier, simpler, and safer to build, deploy, and manage containers. A docker container can be viewed as an instance of a Docker image.
A docker image is a read-only, inert template that comes with guidelines for deploying containers. In Docker, everything basically revolves around images. An image entails a collection of files (also called layers) that pack together all the necessities such as dependencies, source code, and libraries needed to set-up a completely functional container environment.
Going back to FastAPI, the web framework is extremely fast thanks to the out of the box support of the async feature of Python 3.6+. As such, FastAPI is the recommended tool to use for the latest versions of Python. Examples of tech giants using FastAPI to build their applications are Microsoft, Uber and Netflix.
When developing Python apps, it is advisable to use a virtual environment to speed up and clean your overall project workflow. Below, I will show you how to dockerize a simple FastAPI application.
Installing Virtual Environment
Step 1 - Installing a virtual environment
pip3 install virtualenv
Step 2 - Creating virtual environment
python3 -m venv luxenv
Step 3 - Activating the virtual environment
source luxenv/bin/activate
Step 4 - Installing FastAPI
pip3 install fastapi
pip3 install uvicorn
Step 5 - creating requirements.txt file
pip3 freeze > requirements.txt
Ensure you have the app.py application.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return { "Lux app": "Welcome to Lux app" }
Step 6 - create Dockerfile with the following code:
FROM python:3.6
COPY . /src
COPY ./requirements.txt /src/requirements.txt
WORKDIR src
EXPOSE 8000:8000
RUN pip install -r requirements.txt
CMD [ "python", "app.py" ]
Step 7 - Build Docker image called demo
docker build -t luxapp .
Step 8 - Run Docker image called demo
docker run -p 8000:8000 -t -i luxapp
Once you are done with that, you can visit [http://127.0.0.1:8000/] where you will see the app details displayed on your browser.
Here are a few additional tips for you:
docker system prune --> cleans up any resources (images, containers, volumes, networks) that are dangling. Dangling means not associated with a container
docker system prune -a --> removes any stopped containers and all unused images (not just dangling mages)
docker images --> lists all images
docker rmi image_id image_id --> deletes docker images with the given image_id
docker run --rm --> automatically deletes a container that isn't going to be used again
docker ps -a -f status=exited --> lists all exited containers
docker rm $(docker ps -a -f status=exited -q) --> removes/deletes all exited containers
docker push --> push to docker hub
Incase of an error when pushing to the docker hub, try the following commands:
$ docker login
$ docker tag first-image {docker-hub-username}/{default-repo-folder-name}:first-image
$ docker push {docker-hub-username}/{default-repo-folder-name}:first-image
e.g. I have a public repository like this; magoto/luxapp so commands would be:
$ docker tag first-push magoto/uxapp:first-push
$ docker push magoto/luxapp:first-push
Good Luck!
My GitHub
17