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 | ||||
---|---|---|---|---|
| ||||
# 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"] |
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:
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 |
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"] |
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"] |
Now that all the files are in place, let's build the container image.
Dockerfile
is, containing your app
directory).Code Block |
---|
$ docker build -t myimage . |
Tip |
---|
Notice the In this case, it's the same current directory ( |
Code Block |
---|
$ docker run -d --name mycontainer -p 8085:8085 myimage |
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 |
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):
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):
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).