Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

When deploying FastAPI applications a common approach is to build a Linux container image. It's normally done using Docker. You can then deploy that container image in one of a few possible ways.

Using Linux containers has several advantages including security, replicability, simplicity, and others.

Tip

In a hurry and already know this stuff? Jump to the Dockerfile below.

Code Block
titleDockerfile Preview
collapsetrue
# Base image (alpine linux)
FROM python:3.11.2-alpine3.17 AS SearchAPI
 
#
WORKDIR /search_api
 
 
# copy source files
COPY . .
 
# Install Python Dependencies
RUN pip3 install --no-cache-dir -e .
 
# Install Pyqpl local library
RUN pip3 install lib/pyqpl-1.0.5-py3-none-any.whl
 
# exposed port for docker image [CAN BE CHANGED, BUT WHEN RUNNING THE IMAGE, THIS WILL BE PORT THAT WILL BE OPEN INSIDE DOCKER]
EXPOSE 8085
 
#
CMD ["uvicorn", "app.webapp:app", "--host", "0.0.0.0", "--port", "8085"]

Build a Docker Image for SearchA PI

This section will show you how to build a Docker image for Search API from scratch, based on the official Python image.

This is what you would want to do in most cases, for example:

  • Using Kubernetes or similar tools
  • When running on a Raspberry Pi
  • Using a cloud service that would run a container image for you, etc.

Package Requirements

Install the Search API requirements the same way it is specified in Install Python Dependencies

But in essence you need to do this

Code Block
pip install -e .

And then 

Code Block
pip install lib/pyqpl-VERSION_IN_PROJECT-py3-none-any.whl

Dockerfile

Now in the same project directory create a file Dockerfile with:

Code Block
# Base image (alpine linux)
FROM python:3.11.2-alpine3.17 AS SearchAPI

# 
WORKDIR /search_api


# copy source files
COPY . .

# Install Python Dependencies
RUN pip3 install --no-cache-dir -e .

# Install Pyqpl local library
RUN pip3 install lib/pyqpl-1.0.5-py3-none-any.whl

# exposed port for docker image [CAN BE CHANGED, BUT WHEN RUNNING THE IMAGE, THIS WILL BE PORT THAT WILL BE OPEN INSIDE DOCKER]
EXPOSE 8085

# 
CMD ["uvicorn", "app.webapp:app", "--host", "0.0.0.0", "--port", "8085"]

Behind a TLS Termination Proxy

If you are running your container behind a TLS Termination Proxy (load balancer) like Nginx or Traefik, add the option --proxy-headers, this will tell Uvicorn to trust the headers sent by that proxy telling it that the application is running behind HTTPS, etc.

Code Block
CMD ["uvicorn", "app.webapp:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8085"]

Build the Docker Image

Now that all the files are in place, let's build the container image.

  • Go to the project directory (in where your Dockerfile is, containing your app directory).
  • Build your FastAPI image:
Code Block
$ docker build -t myimage .
Tip

Notice the . at the end, it's equivalent to ./, it tells Docker the directory to use to build the container image.

In this case, it's the same current directory (.).

Start the Docker Container

  • Run a container based on your image:
Code Block
$ docker run -d --name mycontainer -p 8085:8085 myimage

Check it

You should be able to check it in your Docker container's URL, for example: http://localhost:8085/es/api (or equivalent, using your Docker host).

You will see something like:

Code Block
Search API Running

Interactive API docs

Now you can go to http://localhost/es/docs (or equivalent, using your Docker host).

You will see the automatic interactive API documentation (provided by Swagger UI):

Image Added

Alternative API docs

Now you can go to http://localhost/es/redoc (or equivalent, using your Docker host).

You will see the automatic interactive API documentation (provided by ReDoc):

Image Added

HTTPS

If we focus just on the container image for a Search API application (and later the running container), HTTPS normally would be handled externally by another tool.

It could be another container, for example with Traefik, handling HTTPS and automatic acquisition of certificates.

Tip

Traefik has integrations with Docker, Kubernetes, and others, so it's very easy to set up and configure HTTPS for your containers with it.

Alternatively, HTTPS could be handled by a cloud provider as one of their services (while still running the application in a container).