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).
There is normally another tool in charge of starting and running your container.
It could be Docker directly, Docker Compose, Kubernetes, a cloud service, etc.
In most (or all) cases, there's a simple option to enable running the container on startup and enabling restarts on failures. For example, in Docker, it's the command line option --restart
.
Without using containers, making applications run on startup and with restarts can be cumbersome and difficult. But when working with containers in most cases that functionality is included by default.
If you have a cluster of machines with Kubernetes, Docker Swarm Mode, Nomad, or another similar complex system to manage distributed containers on multiple machines, then you will probably want to handle replication at the cluster level instead of using a process manager (like Gunicorn with workers) in each container.
One of those distributed container management systems like Kubernetes normally has some integrated way of handling replication of containers while still supporting load balancing for the incoming requests. All at the cluster level.
In those cases, you would probably want to build a Docker image from scratch as explained above, installing your dependencies, and running a single Uvicorn process instead of running something like Gunicorn with Uvicorn workers.