Getting Started with Docker & Fast API 🚀🚀
Tags: machinelearning, python, datascience, devops
Published: 2021-09-14 10:40:46

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4jt274288k241g94r66.png)

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](https://github.com/HarunMbaabu/FastAPI-Projects-Demos) 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](https://dev.to/grayhat/getting-started-with-python-web-development-1jl9) 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](https://docs.python.org/3/library/venv.html#:~:text=A%20virtual%20environment%20is%20a,part%20of%20your%20operating%20system.) 

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:**

```python
pip3 install virtualenv
```

**Create a virtual environment:**

```python 
python3 -m venv luxenv
```
**Activate your virtual environment:**

```python 
source luxenv/bin/activate
``` 
**Note:** the following commands will only work on linux and MacOS, use [this](https://www.liquidweb.com/kb/how-to-setup-a-python-virtual-environment-on-windows-10/) guide to learn how to install, create, and activate virtual environment on windows. 

**Installing Fast API**

```python
pip3 install fastapi 
pip3 install uvicorn
```


`app.py`

```python
from fastapi import FastAPI

app = FastAPI()

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

**Creating a requirements.txt file:**

```python
pip3 freeze >  requirements.txt 
```

`Dockerfile`

```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:**

```bash
>>> docker build -t luxapp .  
```
#### **Run docker image called demo:** 

```bash
>>>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👇🏻

![Screenshot 2021-09-14 at 12.27.37](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eigiqa6an38bvpqtv308.png)


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](https://twitter.com/HarunMbaabu)