composebuilder

Docker Compose: Meilisearch v1 + Python FastAPI Search API

Docker Compose stack for Meilisearch v1 in production mode with a master key, a healthcheck and persistent data, plus a FastAPI search API on localhost.

When to use this stack

Meilisearch is an open source search engine focused on instant, typo-tolerant results with a small API and sensible defaults. It fits product search, documentation search and any dataset where users expect answers while they type. This stack runs Meilisearch v1 in production mode with a master key, a healthcheck that uses the built-in health route, and a persistent data volume. A minimal Python FastAPI service sits in front of it and exposes three routes: health, add products and search, so you can test the full path from HTTP request to indexed result.

Use this stack when a site or application needs fast search over a moderately sized dataset, and when you want a simple REST interface without operating a large cluster. Do not expose the master key to browsers or to public clients. Meilisearch stores its indexes on disk and memory-maps them, so plan disk space generously. The sample API has no authentication of its own, so add it before the API is reachable from outside the server. Both published ports are bound to 127.0.0.1, which keeps the stack private until you add a TLS proxy.

docker-compose.yml

services:
  meilisearch:
    image: getmeili/meilisearch:v1.56.2
    restart: unless-stopped
    environment:
      MEILI_ENV: production
      MEILI_MASTER_KEY: ${MEILI_MASTER_KEY}
      MEILI_NO_ANALYTICS: "true"
    volumes:
      - meili_data:/meili_data
    ports:
      - "127.0.0.1:7700:7700"
    healthcheck:
      test: ["CMD", "wget", "-qO-", "http://127.0.0.1:7700/health"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s

  api:
    build: ./api
    restart: unless-stopped
    environment:
      MEILI_URL: "http://meilisearch:7700"
      MEILI_API_KEY: ${MEILI_MASTER_KEY}
    ports:
      - "127.0.0.1:8000:8000"
    depends_on:
      meilisearch:
        condition: service_healthy

volumes:
  meili_data:

.env

.env
# Master key required in production mode. Use at least 16 random bytes.
MEILI_MASTER_KEY=CHANGE_ME_MEILI_MASTER_KEY_AT_LEAST_16_BYTES

Start and verify

The heredocs below create the API project next to docker-compose.yml: a Dockerfile, pinned Python dependencies and a minimal app. The Meilisearch client talks to the server through the service name meilisearch, so the API never needs the public port.

Shell
mkdir -p api
cat > api/Dockerfile <<'EOF'
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
EOF
cat > api/requirements.txt <<'EOF'
fastapi==0.115.5
uvicorn==0.32.1
meilisearch==0.31.5
EOF
cat > api/main.py <<'EOF'
import os

from fastapi import FastAPI
from meilisearch import Client

client = Client(os.environ["MEILI_URL"], os.environ["MEILI_API_KEY"])
INDEX = "products"
app = FastAPI()


@app.get("/health")
def health():
    return client.health()


@app.post("/products")
def add_products(docs: list[dict]):
    client.index(INDEX).add_documents(docs)
    return {"queued": len(docs)}


@app.get("/search")
def search(q: str, limit: int = 10):
    return client.index(INDEX).search(q, {"limit": limit})
EOF
set -a; . ./.env; set +a
docker compose config --quiet
docker compose up -d --build
docker compose ps
docker compose logs --tail=30 meilisearch
curl -s http://127.0.0.1:8000/health
curl -s -X POST http://127.0.0.1:8000/products -H "Content-Type: application/json" -d '[{"id":1,"name":"Blue running shoes"},{"id":2,"name":"Red rain jacket"}]'
sleep 3
curl -s "http://127.0.0.1:8000/search?q=shoes"

The health call should report the server as available, and the search should return the blue running shoes after the indexing task finishes.

Production checklist

  • Replace the CHANGE_ME master key with a long random value, and keep MEILI_ENV set to production.
  • Create a search-only key through the keys endpoint, restrict it to the products index, and give that key to the API.
  • Keep port 7700 bound to 127.0.0.1, and expose only the API through a TLS reverse proxy.
  • Add authentication to the API routes, because the sample has none.
  • Define searchable, filterable and sortable attributes explicitly through the settings endpoints.
  • Enable scheduled snapshots with the snapshot setting, or export dumps, and copy them off the server.
  • Keep free disk space at several times the size of the indexed data, because indexes are memory-mapped files.
  • Pin the image tag and read the release notes before upgrading, since some major versions need a dump and import.

Common problems

  • Symptom: Meilisearch exits and mentions a master key that is missing or too short. Cause: production mode is enabled without a valid MEILI_MASTER_KEY. Fix: set a random key of at least 16 bytes in .env and start the stack again.

  • Symptom: the API returns 403 with an invalid API key message. Cause: the key in MEILI_API_KEY is wrong, or it is a scoped key without rights for the requested action. Fix: check the key value with docker compose config, or create a key with the search action on the products index.

  • Symptom: a new document does not appear in the search results. Cause: indexing is asynchronous, so the task is still running. Fix: look up the task in the tasks endpoint, wait for the succeeded status, and search again.

  • Symptom: a filter fails with an error that the attribute is not filterable. Cause: the attribute was not added to the filterable attributes setting. Fix: update the index settings to include the attribute, then wait for the settings task to finish.

Variations

  • Typesense instead: the Typesense stack in this category offers a similar typo-tolerant search with a different API. Choose it when you prefer its schema model.
  • Sync from a database: add a worker that reads changed rows from PostgreSQL and sends them to Meilisearch in batches.
  • Multiple indexes: create one index per content type, and define each index’s settings separately so ranking rules stay focused.

Questions

Why does Meilisearch refuse to start in production mode?

In production mode Meilisearch requires a master key of at least 16 bytes. Set MEILI_MASTER_KEY in .env to a long random value, then start the stack again.

Why does a search return nothing right after I add documents?

Indexing runs as an asynchronous task. Check the task status through the tasks endpoint, and search again when the task has succeeded.

Should the API use the master key?

The sample passes the master key to keep the first run simple. In production, create a search-only key through the keys endpoint and give the API that key instead.

Why is the Meilisearch port bound to 127.0.0.1?

The Meilisearch API can change indexes and settings when given the right key, so it should not be reachable from the internet. The API container on the Compose network talks to it directly.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • FastAPI + MongoDB 7 + Mongo Express

    Docker Compose stack for a Python FastAPI API on MongoDB 7 with authentication, a healthcheck, persistent storage and a protected Mongo Express UI on localhost.

    • api
    • mongodb
    • mongo-express
    Difficulty: Intermediate
  • ClickHouse 25.8 Analytics Database

    Docker Compose stack for ClickHouse 25.8 with a dedicated user, a healthcheck, raised file limits and HTTP and native ports on localhost for analytical queries.

    • clickhouse
    Difficulty: Advanced
  • Elasticsearch 8 + Kibana Search Stack

    Docker Compose stack for Elasticsearch 8.19 and Kibana 8.19 with security enabled, a persistent volume, a healthcheck and localhost-only ports.

    • elasticsearch
    • kibana
    Difficulty: Advanced
  • MariaDB 11.4 LTS + Adminer 5

    Docker Compose stack with MariaDB 11.4 LTS, a healthcheck, a named volume and Adminer 5 on localhost. A lightweight admin UI for MySQL-compatible databases.

    • mariadb
    • adminer
    Difficulty: Beginner

Browse more Databases & Search stacks or the full library.