Getting Started with Docker & Fast API ??

Am back again after a 4 weeks break with a new article, today we are going to learn what docker is, understand the differences between docker image and docker container and containerize a simple Python (FastAPI Application), all the code and resourc…

image

Am back again after a 4 weeks break with a new article, today we are going to learn what docker is, understand the differences between docker image and docker container and containerize a simple Python (FastAPI Application), all the code and resources used in this article can be downloaded from this GitHub repo Repository.

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 required to run that code in any environment.

Containers simplify delivery of distributed applications, and have become increasingly popular as organizations shift to cloud-native development and hybrid multicloud environments.

Developers can create containers without Docker, but docker makes it easier, simpler, and safer to build, deploy and manage containers.

Docker is essentially a toolkit that enables developers to build, deploy, run, update, and stop containers using simple commands and work-saving automation through a single API.



Docker Container vs Docker Image

A Docker image is a read-only, inert template that comes with instructions for deploying containers. In Docker, everything basically revolves around images.

An image consists of a collection of files (or layers) that pack together all the necessities, such as dependencies, source code, and libraries needed to set up a completely functional container environment.

A Docker container is a virtualized runtime environment that provides isolation capabilities for separating the execution of applications from the underpinning system. A docker conatiner can be viewed as an instance of a Docker image.

Containers are the ultimate utility of the Docker technology—they provide a portable and lightweight environment for deploying applications.

Each container is autonomous and runs in its own isolated environment, ensuring it does not disrupt other running applications or its underlying system. This greatly improves the security of applications.



Fast API.

From the official documentation, FastAPI is a modern [and] fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python type hints.

As evident from the name, FastAPI is extremely fast and it owes this to the to out of the box support of the async feature of Python 3.6+. This is why it is recommended to use the latest versions of Python.

A number of tech giants like Microsoft, Uber and Netflix are already using FastAPI to build their applications.

When developing python application as i discussed in my previous article, Getting Started with Python Web Development it is advisable to use a virtual environment to speed up and clean your overall project workflow, read more about creating a virtual environment ?? here

So enough of talking let get our hands dirty and dockerize a simple Fast API application.

First install and activate your virtual environment and install Fast API as shown below:

Installing virtual environment:

pip3 install virtualenv

Create a virtual environment:

python3 -m venv luxenv

Activate your virtual environment:

source luxenv/bin/activate

Note: the following commands will only work on linux and MacOS, use this guide to learn how to install, create, and activate virtual environment on windows.

Installing Fast API

pip3 install fastapi 
pip3 install uvicorn

app.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return { "Lux app": "Welcome to Lux app" }

Creating a requirements.txt file:

pip3 freeze >  requirements.txt 

Dockerfile

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" ]



Build docker image called demo:

>>> docker build -t luxapp .  



Run docker image called demo:

>>>docker run -p 5000:5000 -t -i luxapp

Now in your local browser visit http://127.0.0.1:8000 you should see the following result??

Screenshot 2021-09-14 at 12.27.37

Congratulations if you read to this point, i hope you learnt one or two new things, please feel free to leave your comments below. Also you can connect with me on twiter @HarunMbaabu


Print Share Comment Cite Upload Translate
APA
Mwenda Harun Mbaabu | Sciencx (2024-03-29T11:54:51+00:00) » Getting Started with Docker & Fast API ??. Retrieved from https://www.scien.cx/2021/09/14/getting-started-with-docker-fast-api-%f0%9f%9a%80%f0%9f%9a%80/.
MLA
" » Getting Started with Docker & Fast API ??." Mwenda Harun Mbaabu | Sciencx - Tuesday September 14, 2021, https://www.scien.cx/2021/09/14/getting-started-with-docker-fast-api-%f0%9f%9a%80%f0%9f%9a%80/
HARVARD
Mwenda Harun Mbaabu | Sciencx Tuesday September 14, 2021 » Getting Started with Docker & Fast API ??., viewed 2024-03-29T11:54:51+00:00,<https://www.scien.cx/2021/09/14/getting-started-with-docker-fast-api-%f0%9f%9a%80%f0%9f%9a%80/>
VANCOUVER
Mwenda Harun Mbaabu | Sciencx - » Getting Started with Docker & Fast API ??. [Internet]. [Accessed 2024-03-29T11:54:51+00:00]. Available from: https://www.scien.cx/2021/09/14/getting-started-with-docker-fast-api-%f0%9f%9a%80%f0%9f%9a%80/
CHICAGO
" » Getting Started with Docker & Fast API ??." Mwenda Harun Mbaabu | Sciencx - Accessed 2024-03-29T11:54:51+00:00. https://www.scien.cx/2021/09/14/getting-started-with-docker-fast-api-%f0%9f%9a%80%f0%9f%9a%80/
IEEE
" » Getting Started with Docker & Fast API ??." Mwenda Harun Mbaabu | Sciencx [Online]. Available: https://www.scien.cx/2021/09/14/getting-started-with-docker-fast-api-%f0%9f%9a%80%f0%9f%9a%80/. [Accessed: 2024-03-29T11:54:51+00:00]
rf:citation
» Getting Started with Docker & Fast API ?? | Mwenda Harun Mbaabu | Sciencx | https://www.scien.cx/2021/09/14/getting-started-with-docker-fast-api-%f0%9f%9a%80%f0%9f%9a%80/ | 2024-03-29T11:54:51+00:00
https://github.com/addpipe/simple-recorderjs-demo