pymilvus times out connecting to Milvus on localhost:19530 from a container
MilvusException: (code=2, message=Fail connecting to server on localhost:19530. Timeout)Short answer
Inside a container, localhost is the container itself, not the machine or the Milvus container, so the app gets a timeout on port 19530. Put the app and Milvus on the same Docker network, connect with the Milvus container or service name, and wait until Milvus reports healthy before connecting.
Fix it fast
1Find out which cause you have
Check that Milvus is up and healthy, and see which networks it and your app are attached to.
docker ps docker inspect --format '{{.State.Health.Status}}' milvus-standalone2Match what you see, then jump to its fix
What does the error look like?
MilvusException: (code=2, message=Fail connecting to server on localhost:19530. Timeout)
Cause 1 of 2
The app connects to localhost from inside its own container
1Check
A container has its own loopback address, so localhost inside the app container does not reach the Milvus container.
grep -rn "localhost:19530" .2Fix
Use the service or container name on the shared network. Compose registers each service in Docker's internal DNS, and container-to-container traffic uses the container port.
from pymilvus import MilvusClient
client = MilvusClient(uri="http://milvus-standalone:19530")3Verify
The client connects and calls succeed.
python -c "from pymilvus import MilvusClient; print(MilvusClient(uri='http://milvus-standalone:19530').list_collections())"Confirmed by docs.docker.com
Cause 2 of 2
Milvus has not finished starting
1Check
The Milvus docs say to wait until the health status reads healthy before you continue.
docker inspect --format '{{.State.Health.Status}}' milvus-standalone2Fix
Wait for healthy before starting the app, or retry the connection in your code.
until [ "$(docker inspect --format '{{.State.Health.Status}}' milvus-standalone)" = healthy ]; do sleep 5; done3Verify
The status reads healthy and the client connects.
docker inspect --format '{{.State.Health.Status}}' milvus-standaloneConfirmed by milvus.io
How do I stop it coming back?
- Put the Milvus address in an environment variable so one image works on a laptop and in Compose.
- Run Milvus and the app in one Compose project so they share a network and can use each other's names.
- Retry the first connection a few times instead of failing on the first timeout.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
AI
Chroma data is empty after a restart in LangChain
The collection is empty after the app restarts
Chroma only keeps data when you ask it to.
3 causes, each with a fixAI
The shell says ollama: command not found
command not found: ollama
The shell can only run ollama if the binary is installed in the same place the shell runs.
2 causes, each with a fixAI
Ollama is slow and the GPU is not being used
Replies arrive a few tokens per second on a machine with a GPU
Run ollama ps and read the PROCESSOR column: 100% GPU means the model is on the card, anything with CPU means it is not fully there.
4 causes, each with a fixAI
Open WebUI or another Docker app cannot connect to Ollama
Connect call failed ('127.0.0.1', 11434)
Inside a container, localhost is the container itself, not your machine, so a call to 127.0.0.1:11434 never reaches Ollama.
3 causes, each with a fixAI
Open WebUI web search with SearXNG fails with 403
Web search returns no results with SearXNG selected
Open WebUI asks SearXNG for JSON results, and SearXNG refuses that unless json is listed in the formats in settings.yml.
2 causes, each with a fixAI
Azure OpenAI says the API deployment for this resource does not exist
The API deployment for this resource does not exist
Azure OpenAI does not take a model name in the model field.
2 causes, each with a fix