FAST API and step to deploy to Docker 101 #1

YOU NOOB TV
4 min readSep 27, 2023

--

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.

ref : https://fastapi.tiangolo.com/

The key features are:

  • Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
  • Fast to code: Increase the speed to develop features by about 200% to 300%. *
  • Fewer bugs: Reduce about 40% of human (developer) induced errors. *
  • Intuitive: Great editor support. Completion everywhere. Less time debugging.
  • Easy: Designed to be easy to use and learn. Less time reading docs.
  • Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
  • Robust: Get production-ready code. With automatic interactive documentation.
  • Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.

Today I want to show you how to deploy a FAST API on Docker with these steps as a basis for further implementation.

Of course,I understand that you’ll need to have some backgrounf in Python and that you’ll need Docker desktop to be able to follow me. Or if you’re still missing any of the parts I mentioned, I recommend the two source links I mentioned here.

Python : https://www.python.org/downloads/

Docker : https://docs.docker.com/engine/install/

Well,let’s get started. I’ll start by createing a Folder. When it comes up in the location I want to store it,you can choose how you want to store the Folder.

Install FastAPI and Uvicorn:

FastAPI is a web framework, and Uvicorn is an ASGI server that is commonly used with FastAPI. Install them using pip:

pip install fastapi uvicorn

This Project Structure :

As your project grows,it’s a good idea to organize your code separate modules and folders. Here’s a common project structure:

my_fastapi_project/
├── app/
│ ├── __init__.py
│ ├── main.py # FastAPI application definition
├── requirements.txt # List of project dependencies
├──Dockerfile

Create a Project Directory:

Create a directory for your FastAPI project and navigate to it:

mkdir my_fastapi_project
cd my_fastapi_project

Create Your FastAPI Application:

Inside your project directory, create a Python file (e.g., main.py) where you will define your FastAPI application. Here's a simple example:

# main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}

Run Your FastAPI Application:

You can run your FastAPI application using Uvicorn. Run the following command in your project directory

uvicorn main:app - reload

Create a Dockerfile:

Create a Dockerfile in your project directory to define how your FastAPI application should be built within a Docker container:

# version python
FROM python:3.10
# file Dir
WORKDIR /code
# copy to docker pip install on document
COPY ./requirements.txt /code/requirements.txt
# cmd install
RUN pip install - no-cache-dir - upgrade -r /code/requirements.txt
#
COPY ./app /code/app

# Expose the desired port
EXPOSE 3000
#
CMD ["uvicorn", "app.main:app", " - host", "0.0.0.0"," - port", "3000"]

Create a requirements.txt File:

Create a requirements.txt file in your project directory listing all the dependencies needed for your FastAPI application. Include FastAPI, Uvicorn, and any other required packages.

In this example I put the following information in it.

fastapi>=0.68.0,<0.69.0
pydantic>=1.8.0,<2.0.3
uvicorn>=0.15.0,<0.16.0

Step Deploy to Docker :
Now that we have prepared the code and arranged the files, the next step is to deploy it to Docker with the following steps and commands.
Note: Docker must be open to run commands.

1. Steps for creating images in Docker to create prototypes for later building containers.

docker build -t <project> .

In this example, we name the project my_fastapi_project So the command will be as follows.

docker build -t my_fastapi_project .

After running the command within docker desktop, my_fastapi_project will appear in the images menu tab.

2. After that use the command to create a container using port 3000.

docker run -d --name <name container> -p 80:3000 <nam images project>

In this example, the following command will be used.

docker run -d --name my_fastapi_project -p 80:3000 my_fastapi_project

If you look at the container menu, you will see that a container has been created using the prototype image we created in step 1.

When testing according to URL: http://localhost/docs This will be seen in this example.

You will notice that FASTAPI has prepared a document for the router to make it easy to run and test without having to go through Postman.

Summarize :
We’ll show you how to create a project and set it up for deployment by creating prototype images to create a prototype container. In the next article, we’ll show you how to use FASTAPI CRUD to prototype it. How to deploy it for real use? Please wait and follow.

--

--