DEV Community

ADERA KARUME
ADERA KARUME

Posted on

GETTING STARTED WITH FAST API AND DOCKER

Fastapi is a modern high performance framework for building websites with python 3.6.Docker is a software platform for building applications based on containers. This articleexplores the use of both Fastapi and docker in building efficient web applications.

WHY FastAPIs

fastAPIS is fast,if not the fastest python framework available .It increases the speed to develop feateures by 200%. it also reduces human induced bugs,FastAPis is also designed to be easy to learn and use. FastAPIs is very intuitive, with great editor support and completion everywhere. Here is a quick in depth tutorial, click through to get deeper understanding as we skip to the installation https://fastapi.tiangolo.com/tutorial/

FastAPI INSTALLATION

Click on the shell and run the following command

$ pip install fastapi

an ASGI(asynchronous server gateway interface) for production is also required. in this case uvicorn

to instal uvicorn run the following command;

$ pip install uvicorn

lets create an example API,
In a main.py file, the simplest FastAPI could look like this

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
return {"message": "Hello World"}

run the server by ...

uvicorn main:app

in this command, main refers to the file main.py while app refers to the object created inside the file.

open your browser at http://127.0.0.1:8000/
A json response should be seen as follows

{"message": "Hello World"}

next,you can change the route to access the fastapis docs (simply by adding /docs to the url on the browser) you will be redirected to the interactive documentation. an alternative api documentation is provided by changing the route to redoc.

with these few steps you have installed fastapi and uvicorn ,imported fastapi and created your first fastapi instance.

DOCkER

docker helps developers build lightweight and portable software containers that simplify application development,testing and deployment. So what are containers? containers are “self-contained units of software you can deliver from a server over there to a server over there, from your laptop to EC2 to a bare-metal giant server, and it will run in the same way because it is isolated at the process level and has its own file system.”

WHY DOCKER

docker comes highly reccommended for simple and faster configurations. With docker you can achieve continous deployment and testing.

INSTALLATIONS

check out the following resource to download/install docker on your desktop ,either mac or windows.

for windows
for mac

Docker is broken down into component parts including dockerfile, container images,Docker run utility,Docker hub,Docker engine,Docker compose and Docker desktop.

Dockerfile. Each Docker container starts with a Dockerfile. This text file provides a set of instructions to build a Docker image, including the operating system, languages, environmental variables, file locations, network ports, and any other components it needs to run.

Docker image. Similar to a snapshot in the VM world, a Docker image is a portable, read-only, executable file containing the instructions for creating a container and the specifications for which software components the container will run and how.

Docker run utility. Docker’s run utility is the command that launches a container. Each container is an instance of an image, and multiple instances of the same image can be run simultaneously.

Docker Hub. Docker Hub is a repository where container images can be stored, shared, and managed. Think of it as Docker’s own version of GitHub, but specifically for containers

CONSTRAINTS OF USING DOCKER

Docker containers are not virtual machines, Unlike virtual machines, containers use controlled portions of the host operating system’s resources, which means elements aren’t as strictly isolated as they would be on a virtual machine

Docker images are immutable by nature,hence once created it cant be changed.

Top comments (0)