Joachim Watkidog
Posted on November 14, 2021
In this beginner’s guide, I will take you through the basic steps to get your first application running using FastAPI and Docker.
Table of contents
- FastAPI
- Docker
- Building a FastAPI application with Docker
FastAPI
FastAPI is a fast, modern, high performance, open source Python web framework used to build APIs with Python 3.6+. It is a minimalistic framework and quite new with a smaller community compared to Django and Flask but when it comes to performance, it is the fastest by far. It supports asynchronous programming, which is a plus. To learn more about fastAPI, visit their official docs here
Docker
Docker is a containerization platform. It enables you to build, run and ship your application, packages your code and all its dependencies enabling your application to run consistently across different computing environments.
A container is a virtualized environment for running an application. It includes everything needed for an application to run.
Docker uses the client-server architecture. The client component communicates with the server (the Docker Engine) using a RESTful API. The Docker Engine builds and runs the Docker containers.
Building your first FastAPI and Docker application
In this section, we are going to dockerize a simple python application.
Requirements
- Linux or Mac computer. You can also use Windows but some commands are different.
- Python 3.6+
We shall start by installing a virtual environment in which we will create our application.
Install a virtual environment - virtualenv
pip3 install virtualenv
Navigate to a directory of choice and create the virtual environment.
python3 -m virtualenv hello-app-env
Activate the virtual environment
source hello-app-env/bin/activate
Change directory into the virtual environment created and install fasAPI and uvicorn. We'll use uvicorn to run our application.
pip3 install fastapi uvicorn
Create a requirements.txt file. This lists all the packages required by our application.
pip3 freeze > requirements.txt
Now that we are all set up. Let's create a python file main.py
in your src
directory and add the following code:
/src/main.py
from fastapi import FastAPI
app = fastAPI()
@app.get("/")
def index():
return {"Greeting": "Hello World!"}
Note: use @app.post() if you're accepting sensitive data from the user like usernames and passwords.
Create a Docker file called Dockerfile
FROM python:3.9
COPY ./src /app/src
COPY requirements.txt /app
WORKDIR /app
RUN pip3 install -r requirements.txt
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host=0.0.0.0", "--reload"]
Note: --reload
will automatically update changes when made so you won't have to re-run the container.
To build your Docker image, open your terminal and enter:
docker build -t hello-world-env .
Run the docker image
docker run -p 8000:8000 --name hello-app hello-world-env
Note: In this case, hello-app is the name I'm giving to the container, hello-world-env is the image
To verify that the image and container exist, run these commands in your terminal. The directory you are in does not matter:
docker image ls
docker container ls
The docker run
command will spin up your local server and you can view your results through this URL http://localhost:8000 or http://127.0.0.1:8000 ``
Conclusion
As you can see, it's quite easy to create an application with FastAPI and also dockerize it. And as you know by now, you learn code best by trying it out yourself through building stuff. So try and create another application and see if you can have something visible at the end.
Posted on November 14, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.