DEV Community

Mwenda Harun Mbaabu
Mwenda Harun Mbaabu

Posted on

Getting Started with Docker & Fast API ๐Ÿš€๐Ÿš€

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
Enter fullscreen mode Exit fullscreen mode

Create a virtual environment:

python3 -m venv luxenv
Enter fullscreen mode Exit fullscreen mode

Activate your virtual environment:

source luxenv/bin/activate
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

app.py

from fastapi import FastAPI

app = FastAPI()

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

Creating a requirements.txt file:

pip3 freeze >  requirements.txt 
Enter fullscreen mode Exit fullscreen mode

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" ]
Enter fullscreen mode Exit fullscreen mode

Build docker image called demo:

>>> docker build -t luxapp .  
Enter fullscreen mode Exit fullscreen mode

Run docker image called demo:

>>>docker run -p 8000:8000 -t -i luxapp
Enter fullscreen mode Exit fullscreen mode

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

Top comments (8)

Collapse
 
artyomovs profile image
artyomovs • Edited


CMD [ "python", "app.py" ]

Does it work on your computer? I think, you confused with Flask.
Cause I managed to run FastAPI with uvicorn only this way:

CMD [ "uvicorn", "app:app", "--host", "0.0.0.0", "--reload" ]

Collapse
 
grayhat profile image
Mwenda Harun Mbaabu

It is an alternative, and yes it is working on my computer

Collapse
 
detzam profile image
webstuff

wait... wait.. aren't images different servers, development systems already made by people from the comunity and they're reduced in size meant to be built apon using the build files that you write dipending on the type of server you need?
ex:
if i want a lamp server, i'll get a debian distro, alpine image and then build apon it a container that will have apache and php and if i'm really poor i even put mysql on it.

Collapse
 
tgkprog profile image
Tushar Kapila • Edited

They are not servers. But operating system binaries, stored at a certain time from an installed and configured version of the OS. They can be used to create a server or a desktop when run on hardware.

In docker world we usually use images to build servers mostly, but some use them to make desktops too, like for a company that wants to provide virtual desktops with higher security and pre-installed software on the cloud. Some banks and medical orgs do this.

From Google:
In computing, a system image is a serialized copy of the entire state of a computer system stored in some non-volatile form such as a file. A system is said to be capable of using system images if it can be shut down and later restored to exactly the same state.

Collapse
 
grayhat profile image
Mwenda Harun Mbaabu

Thank you Tushar for answering this, you handled it very well ๐Ÿฅณ๐Ÿฅณ

Thread Thread
 
tgkprog profile image
Tushar Kapila

Ha thank you

Collapse
 
manzato profile image
Guillermo Manzato

docker run -p 5000:5000 -t -i luxapp should probably be docker run -p 8000:8000 -t -i luxapp

Collapse
 
grayhat profile image
Mwenda Harun Mbaabu

Thank you for the correction Guillermo