Problem with connection between Milvus and FastAPI docker containers
Published: 2023-10-26 19:20:14


Hey, everybody!

I am trying to configure docker-compose for Milvus and FastAPI.
All containers are running and individually accessible from outside.
I.e. if I try to connect to Milvus using an external script, the connection is established, but if I try to connect via FastAPI, which is also in the container, I get this error:

<MilvusException: (code=2, message=Fail connecting to server on localhost:19530. Timeout)>

Dockerfile(FastAPI):

```
# Dockerfile

# pull the official docker image
FROM python:3.10

RUN mkdir /app

# set work directory
WORKDIR /app

# set env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt

# copy project
COPY . .

ENV https_proxy ""

#CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "5000"]
```

Docker-compose

```
version: '3.5'

services:
    ubuntu:
        container_name: ubuntu-os
        image: ubuntu:latest
        tty: true
        networks:
            -  milvus # Connect to the same network as other services


    etcd:
        container_name: milvus-etcd
        image: quay.io/coreos/etcd:v3.5.5
        networks:
            -  milvus
        environment:
            -  ETCD_AUTO_COMPACTION_MODE=revision
            -  ETCD_AUTO_COMPACTION_RETENTION=1000
            -  ETCD_QUOTA_BACKEND_BYTES=4294967296
            -  ETCD_SNAPSHOT_COUNT=50000
        volumes:
            -  ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/etcd:/etcd
        command: etcd -advertise-client-urls=http://etcd:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd # Use service name as host name
        healthcheck:
            test: ["CMD", "etcdctl", "endpoint", "health"]
            interval: 30s
            timeout: 20s
            retries: 3

    minio:
        container_name: milvus-minio
        image: minio/minio:RELEASE.2023-03-20T20-16-18Z
        networks:
            -  milvus
        environment:
            MINIO_ACCESS_KEY: minioadmin
            MINIO_SECRET_KEY: minioadmin
        ports:
            -  "9001:9001"
            -  "9000:9000"
        volumes:
            -  ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/minio:/minio_data
        command: minio server /minio_data --console-address ":9001" --address "minio:9000" # Use service name as host name and address
        healthcheck:
            test: ["CMD", "curl", "-f", "http://minio:9000/minio/health/live"] # Use service name as host name
            interval: 30s
            timeout: 20s
            retries: 3

    standalone:
        container_name: milvus-standalone
        image: milvusdb/milvus:v2.3.2
        networks:
            -  milvus
        command: ["milvus", "run", "standalone"]
        environment:
            ETCD_ENDPOINTS: etcd:2379 # Use service name as host name
            MINIO_ADDRESS: minio:9000 # Use service name as host name
        volumes:
            -  ${DOCKER_VOLUME_DIRECTORY:-.}/volumes/milvus:/var/lib/milvus
        healthcheck:
            test: ["CMD", "curl", "-f", "http://standalone:9091/healthz"] # Use service name as host name
            interval: 30s
            start_period: 90s
            timeout: 20s
            retries: 3
        ports:
            -  "19530:19530"
            -  "9091:9091"
        depends_on:
            -  "etcd"
            -  "minio"


    fastapi:
        build: ./bots_api
        command: ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "5000"]
        networks:
            -  milvus # Connect to the same network as other services 
        environment:
            MILVUS_HOST: 'standalone' # Use service name as host name 
            MILVUS_PORT: '19530'
        depends_on:
            -  standalone 
        ports:
            -  "5000:5000"


networks:
    milvus:
        driver: bridge # Use a bridge network for communicatio
```