MongoServerError: Error connecting to Search Index Management service when using docker

Good morning,
I need to create a search index in my local deployment of atlas, using a docker compose. I have my client in node with the following code:

import { MongoClient } from "mongodb";

const client = new MongoClient(
	"mongodb://user:pass@mongodb:27017/?directConnection=true"
);

async function run() {
	try {
		await client.connect();
		const db = client.db("testDb");
		const collection = db.collection("testCollection");

		console.log("Connected successfully to server");
    const index = {
      name: `vi-1`,
      type: "vectorSearch",
      definition: {
        "fields": [
          {
            "type": "vector",
            "numDimensions": 768,
            "path": "textEmbedding",
            "similarity": "cosine"
          },
          {
            "type": "filter",
            "path": "company"
          },
          {
            "type": "filter",
            "path": "phone"
          },
        ]
      }
    }
		await collection.createSearchIndex(index);

	} finally {
		await client.close();
	}
}

run();

And running the MongoDB server with the compose file written in the docs, this is the whole compose file with both services:

version: '3.8'

services:
  mongodb:
    image: mongodb/mongodb-atlas-local
    environment:
      - MONGODB_INITDB_ROOT_USERNAME=user
      - MONGODB_INITDB_ROOT_PASSWORD=pass
    ports:
      - 27017:27017
    volumes:
      - ./data:/data/db
    user: root:root

  client:
    build: 
      context: ./db
    depends_on:
      - mongodb
        
volumes:
  data:

This is the clitent’s Dockerfile:

FROM node:20.16.0

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

CMD [ "node", "db.js" ]

Note that this issue does not happen if I run the client code manually in my computer, using a localhost URL.

1 Like

I am having the exact same issue trying to connect to a sample free cluster. Is the Atlas vector creation expected to work on this case? Not sure how to troubleshoot the case, I just am havving the same error

  errorResponse: {
    ok: 0,
    errmsg: 'Error connecting to Search Index Management service.',
    code: 125,
    codeName: 'CommandFailed',
    '$clusterTime': { clusterTime: [Timestamp], signature: [Object] },
    operationTime: Timestamp { low: 8, high: 1724062916, unsigned: true }
  },

Manged to get around it by adding a 5 second delay before creating the index

I’m having the same issue. Any luck?