Getting Started with Docker & Fast API ππ
Mwenda Harun Mbaabu
Posted on September 14, 2021
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 8000:8000 -t -i luxapp
Now in your local browser visit http://127.0.0.1:8000 you should see the following resultππ»
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
Posted on September 14, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.