How to build a FastAPI environment in two ways: core modules and Docker

overview

We will explain various environment construction methods of FastAPI, a Python-based web framework.

In this article, we will introduce how to build the environment in the following two ways.


  1. How to build an environment with FastAPI core module
  2. How to build an environment using Docker

Here, we will explain how to build a FastAPI environment using the following versions.

FastAPI v0.100.0
Python v3.11.4
uvicorn v0.22.0

Also, all the code I’ve created so far is listed below. https://github.com/wiblok/FastAPI

*** FastAPI runs in Python. If Python is not installed, download it from the official website and install it. ** Python

How to build an environment using core modules

This method imports and uses the FastAPI core module. Please follow the steps below.

To build an environment with FastAPI alone, the following prerequisites must be met:

  • Python 3.6 or above installed
  • pip installed

Create project directory

First, create a new directory to create your FastAPI project. Run the below command in command line

mkdir fastapi-core-project

Change to project directory

Go to your project directory. Please run the following command:

cd fastapi-core-project

After moving, move to the root directory of the project.

Installing FastAPI and Uvicorn

Install FastAPI and Uvicorn (ASGI server). Please run the following command:

pip install fastapi uvicorn

This will add FastAPI and Uvicorn to your project.

Creating an application

Create a FastAPI application. Create a new file (e.g. main.py) inside your project directory and add the following code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "FastAPI"}

The example above shows the basic structure of a FastAPI application. Define a handler for the root path using the @app.get("/") decorator and return {"Hello": "FastAPI"} as a response.

Start the server

Finally, start the FastAPI server. Please run the following command:

uvicorn main:app --reload

If the server starts successfully, you should see “Uvicorn running on http://127.0.0.1:8000” (default host and port) in the console. Visit http://localhost:8000 in your browser to make sure your FastAPI application is working properly.

test

curl http://localhost:8000/

How to build an environment using Docker

First of all, install Docker. Download the Docker installer for your operating system from the official Docker website and install it.

Create project directory

First, create a new directory to create your FastAPI project. Run the following command on the command line:

mkdir fastapi-docker-project

Change to project directory

Go to your project directory. Run the command below

please:

cd fastapi-docker-project

After moving, move to the root directory of the project.

Creating an application

Create a FastAPI application. Create a new file (e.g. main.py) inside your project directory and add the following code:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "FastAPI"}

Creating a Dockerfile

To build a FastAPI application as a Docker image, create a file named Dockerfile in your project folder and add the following content.

FROM python:3.11.4

WORKDIR /app

COPY . /app

RUN pip install fastapi uvicorn

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Building a Docker image

Build the Docker image using the command below.

docker build -t fastapi-docker-project .

Running a Docker container

Run a Docker container from the built Docker image.

docker run -p 8000:8000 -d fastapi-docker-project

This will run your FastAPI application inside a Docker container and map it to port 8000 on your host. You can access the FastAPI application by accessing http://localhost:8000 from your browser, API test tool, etc.

test

curl http://localhost:8000/

The above is the procedure for building a FastAPI application environment using Docker. By using Docker, you can reduce the work of building the environment and make the development process smoother.

Note: Access to port 8000 may be restricted by the host’s firewall or network settings. Adjust your firewall and network settings as needed.