Text Embeddings Inference in Docker with a reranker

Text Embeddings Inference (TEI) from Hugging Face serves embedding and reranking models over HTTP. This tutorial starts two containers and queries them from Python, which gives an internal document search a second ranking stage.

Niklas Schult4 min read

Illustration: Two small wooden boxes pour beads onto a brass sieve that keeps only a few, lined up neatly on a tray.

What you will have at the end and what you need

At the end two services run on one machine: an embedding model on port 8080 and a reranking model on a second port. A short Python script uses the first to pre-select documents and the second to order the best candidates. This is the usual pattern for improving search over internal documents such as policies, maintenance plans or travel rules.

You need Docker, an NVIDIA GPU and the NVIDIA Container Toolkit. According to the TEI README, the NVIDIA drivers must be compatible with CUDA 12.2 or higher. The README also lists CPU images and images for other GPU generations, so check the image table if your hardware differs. Python with the requests package completes the list.

Start the embedding model with Docker

The first command downloads the image and starts the model Qwen/Qwen3-Embedding-0.6B. The volume variable shares a local data folder with the container, so the weights are not downloaded again on every start. Port 80 inside the container is published as 8080 on the host.

Shell
model=Qwen/Qwen3-Embedding-0.6B
volume=$PWD/data

docker run --gpus all -p 8080:80 -v $volume:/data --pull always \
  ghcr.io/huggingface/text-embeddings-inference:cuda-1.9 --model-id $model

The image tag cuda-1.9 is the one used in the README example. The README lists other tags per GPU architecture, for example hopper-1.9 for H100 cards or cpu-1.9 for x86_64 machines without a GPU. The same README also describes a gRPC API as an alternative to HTTP, which you get by adding the -grpc suffix to an image tag.

Serve a reranking model on a second port

A reranker is a cross-encoder that scores how well a text answers a query. The README names BAAI/bge-reranker-large as a supported example. The command below starts it in a second container and maps it to a different host port, so it does not collide with the embedding service.

Shell
model=BAAI/bge-reranker-large
volume=$PWD/data

docker run --gpus all -p 8081:80 -v $volume:/data --pull always \
  ghcr.io/huggingface/text-embeddings-inference:cuda-1.9 --model-id $model

Both containers can share the same data folder for the model cache. The README does not state how much GPU memory two parallel containers need, so test this on your own hardware. Other re-ranking models in the README table include BAAI/bge-reranker-base and Alibaba-NLP/gte-multilingual-reranker-base, which you can try if the large model is too heavy.

Query both services from Python

The script below sends three example documents and a question to the /embed endpoint. It ranks the documents by a dot product, keeps the top two and sends them with the question to the /rerank endpoint on the second port. The final print shows the reranker response.

Python
import requests

EMBED = "http://127.0.0.1:8080/embed"
RERANK = "http://127.0.0.1:8081/rerank"

docs = ["Urlaubsregelung für Mitarbeitende", "Wartungsplan Presse 4", "Reisekostenrichtlinie"]
frage = "Wie beantrage ich Urlaub?"

def embed(texts):
    r = requests.post(EMBED, json={"inputs": texts}, timeout=30)
    r.raise_for_status()
    return r.json()

doc_vecs = embed(docs)
q_vec = embed([frage])[0]

# Dot product as simple similarity score
scores = [sum(a * b for a, b in zip(q_vec, v)) for v in doc_vecs]
top = [docs[i] for i in sorted(range(len(docs)), key=lambda i: -scores[i])[:2]]

r = requests.post(RERANK, json={"query": frage, "texts": top}, timeout=30)
r.raise_for_status()
print(r.json())

In a real setup the document vectors would be stored in a vector database instead of being computed on every call. The README leaves that part open. The two-stage design keeps the expensive cross-encoder away from the full document set, because it only sees the few candidates that the embedding step has already selected.

Check that it works and avoid common pitfalls

The first call below sends a test text to the embedding service and should return a vector. The second call reads the Prometheus metrics, which the HTTP server exposes at /metrics on the API port by default. The README also points to the /docs route for the OpenAPI description.

Shell
curl 127.0.0.1:8080/embed -X POST \
  -d '{"inputs":"What is Deep Learning?"}' \
  -H 'Content-Type: application/json'
curl 127.0.0.1:8080/metrics

Several points from the documentation cause trouble in practice. Without the NVIDIA Container Toolkit the --gpus all flag fails. Gated or private models need an HF_TOKEN. By default the server answers every request, so the --api-key option is worth setting. The default payload limit is 2 MB and a client may send at most 32 inputs per request unless you change --max-client-batch-size. For machines without internet access, the README describes an air-gapped setup in which the weights are downloaded beforehand and mounted as a volume.

Where this fits in a company

TEI is one building block of a search pipeline. Someone has to own the containers, pin the image version, update models and decide who may reach the ports. Because the server answers everyone by default, many teams put it behind an internal network boundary and set an API key. Documents sent to the service may contain personal data, so the data protection officer and, where one exists, the Betriebsrat (works council) are usually involved. More background is on our pages about local AI and GDPR and AI.

Teams that prefer not to run this stack themselves can look at knowledge management with AI. souveraen.ai connects sources such as SharePoint and file servers and cites the source for each answer. It runs hosted in Germany or on-premise, as described on the on-premise page.

Sources

More articles

All articles

Self-host Langfuse for LLM monitoring in your company

Langfuse records prompts, answers and costs of AI applications, and the README describes how to run it on your own infrastructure. This tutorial takes you from an empty machine to a first logged model call.

Tutorial4 min

AI applicant profiles: what employers may use from the web

AI tools can turn social media posts and press articles into an applicant profile within seconds. Employers in Germany need to know which public data the GDPR and the BDSG allow them to use in hiring.

Data protection4 min

Do lawyers need client consent to use AI?

A Legal Tribune Online article by Nico Kuhlmann concludes that using AI in a law firm is treated like engaging any other service provider. Consent is usually unnecessary, though borderline cases exist and the duty of confidentiality applies in full.

Data protection5 min

Test it with your own documents.

Free for 3 days, then from €24.90 per user per month. Cancellable monthly.

Free for 3 days, then from €24.90 per user per month.Free for 3 days
Try free for 3 days