Skip to content

Configure Fast API with Uvicorn in DockerfileΒΆ

Create required app moduleΒΆ

To create the required app module for the uvicorn command in the Dockerfile, you will need to create a new file in your project directory. The file should be named after the app module specified in the Dockerfile command. For example, if the command in the Dockerfile is uvicorn my_app:app, the app module should be called my_app.py.

In the app module file, you will need to create an instance of the FastAPI class, which is a web framework for building APIs with Python. Here is an example of what the app module file might look like:

Python
from fastapi import FastAPI

app = FastAPI()

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

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

In this example, the FastAPI class is imported and an instance of the class is created. Then two routes are defined, / and /items/{item_id} with GET request handling function.

You can add any other routes and functionality as per your requirement.

Once you have created the app module file, you can then proceed to build the Docker image using the Dockerfile and commands I mentioned in my previous answer.

Module in different folder/subfolderΒΆ

In the uvicorn command in the Dockerfile, you will need to provide the path to the app module file that you created. The path should be relative to the root directory of your project.

For example, if your project directory is structured like this:

Python
my_project/
    app_module.py
    Dockerfile

The path to the app module in the uvicorn command should be app_module:app.

If your app module is in the subdirectory, you will need to provide the path to the subdirectory as well. For example, if your project directory is structured like this:

Python
my_project/
    my_app/
        __init__.py
        app.py
    Dockerfile

The path to the app module in the uvicorn command should be my_app.app:app.

If your app module is in the subdirectory and subfolder, you will need to provide the path to the subdirectory and subfolder as well. For example, if your project directory is structured like this:

Python
my_project/
    my_app/
        api/
            __init__.py
            app.py
    Dockerfile

The path to the app module in the uvicorn command should be my_app.api.app:app.

You can also use environment variable to provide the path of the app module, which is helpful when you're running your application in different environments.

You can use ENV command in the Dockerfile to set environment variable and use it in uvicorn command.

Python
ENV APP_MODULE=app_module.app:app
CMD uvicorn $APP_MODULE --host=0.0.0.0 --port=8000

You can also use .env file to set environment variable and use it in uvicorn command.

Python
CMD sh -c "source .env && uvicorn $APP_MODULE --host=0.0.0.0 --port=8000"

This way you can change the path of the app module easily without changing the Dockerfile.