docker中的chroma无法从另一个docker服务连接

问题描述 投票:0回答:1

我正在尝试从 service-a (也在 docker 容器中)与色度服务(在 docker 容器中)进行通信。但是,当我尝试

ChromaConnector.get_instance()
时,出现以下错误:

Could not connect to a Chroma server. Are you sure it is running?

我尝试从服务容器中执行以下操作

curl http://chroma:8000
curl http://chroma:8000/api/v1/collections
curl http://chroma:8000/api/v1

curl http://chroma:8000
给出错误

{"detail":"Not Found"}

但是

curl http://chroma:8000/api/v1/collections
给出了

[]

curl http://chroma:8000/api/v1
返回

{"nanosecond heartbeat":1731106743803134838}

这让我感觉服务器已经启动了。

下面是代码

class ChromaConnector:
    _instance = None

    @classmethod
    def get_instance(cls):
        
        if cls._instance is None:
            cls._instance = chromadb.HttpClient(host='http://chroma', port=8000)
        return cls._instance

下面是我的 docker-compose.yaml 文件

version: '3.8'

services:
  service-a:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "18001:18001"
    environment:
      - ENVIRONMENT=development
    depends_on:
      - kafka
      - chroma

  kafka:
    image: bitnami/kafka:latest
    ports:
      - "9094:9094"
    environment:
      - KAFKA_ENABLE_KRAFT=yes
      - KAFKA_CFG_BROKER_ID=1
      - KAFKA_CFG_NODE_ID=1
      - KAFKA_CFG_PROCESS_ROLES=broker,controller
      - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093,EXTERNAL://:9094
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT,EXTERNAL:PLAINTEXT
      - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092,EXTERNAL://localhost:9094
      - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@:9093
      - ALLOW_PLAINTEXT_LISTENER=yes    

  chroma:
    image: chromadb/chroma:latest  
    ports:
      - "18000:8000"
    volumes:
      - chroma-data:/chroma/chroma 
    restart: unless-stopped
    command: "--workers 1 --host 0.0.0.0 --port 8000 --proxy-headers --log-config chromadb/log_config.yml --timeout-keep-alive 30"
    environment:
      - IS_PERSISTENT=TRUE   

  redis:
    image: redis:latest
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data

volumes:
  redis-data:
  chroma-data:

请指教。谢谢。

python docker docker-compose chromadb
1个回答
0
投票

尝试使用

service-a

中初始化客户端的方式
class ChromaConnector:
    _instance = None

    @classmethod
    def get_instance(cls):
        
        if cls._instance is None:
            cls._instance = chromadb.HttpClient(host='http://chroma:8000')
        return cls._instance

我已经按照与您的设置类似的方式对此进行了测试:

version: '3.8'

services:
  service-a:
    image: python:3.12-bookworm
    command: sleep 3600
    ports:
      - "18001:18001"
    environment:
      - ENVIRONMENT=development
    depends_on:
      - chroma

  chroma:
    image: chromadb/chroma:latest  
    ports:
      - "18000:8000"
    volumes:
      - chroma-data:/chroma/chroma 
    restart: unless-stopped
    command: "--workers 1 --host 0.0.0.0 --port 8000 --proxy-headers --log-config chromadb/log_config.yml --timeout-keep-alive 30"
    environment:
      - IS_PERSISTENT=TRUE   

volumes:
  chroma-data:

Shell 进入 python312 容器

docker exec -it <containerid> /bin/bash
,安装 chroma
pip install chromadb
,然后使用 python REPL:

>>> import chromadb
>>> instance = chromadb.HttpClient(host='http://chroma:8000')
>>> instance.list_collections()
[]
>>> 

我认为问题的核心在于url的处理方式。大约一年前,添加了对处理

host
参数中完整 url 的支持(例如
http://my-chroma-ip:port/my_lb_service/
)。这允许在自定义路径后面使用 Chroma。这样做的缺点是,现在它引入了您遇到的混乱 - 提供 url + 端口,这会导致一个奇怪的缺陷,端口参数被忽略。

© www.soinside.com 2019 - 2024. All rights reserved.