> ## Documentation Index
> Fetch the complete documentation index at: https://javaai.pig4cloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Docker Compose

> Spring AI 开发的 Docker Compose 服务

# 开发时服务

Spring AI 为本地开发和测试提供 Docker Compose 配置。

## 概述

本节描述了可用于 Spring AI 开发的 Docker Compose 服务，包括：

* 向量数据库
* AI 模型服务器
* 测试工具

## 可用服务

### 向量数据库

* 带 pgvector 的 PostgreSQL
* Milvus
* Qdrant
* Weaviate
* Chroma

### AI 模型服务器

* Ollama
* LocalAI
* Hugging Face 推理服务器

### 测试工具

* Testcontainers 配置
* 模拟 AI 服务

## 使用方法

要使用这些服务，您可以运行：

```bash theme={"system"}
docker-compose up -d
```

这将启动本地开发所需的所有必要服务。

## 配置

每个服务都可以通过环境变量或 Docker Compose 覆盖进行配置。

## 实现

### 基本 Docker Compose 设置

```yaml theme={"system"}
version: '3.8'

services:
  # 向量存储
  pgvector:
    image: pgvector/pgvector:latest
    environment:
      POSTGRES_DB: vectordb
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"

  # AI 模型服务器
  ollama:
    image: ollama/ollama:latest
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama

  # 监控
  prometheus:
    image: prom/prometheus:latest
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml

volumes:
  ollama_data:
```

### 服务配置

```yaml theme={"system"}
# 向量存储配置
spring.ai.vectorstore.pgvector.url=jdbc:postgresql://localhost:5432/vectordb
spring.ai.vectorstore.pgvector.username=postgres
spring.ai.vectorstore.pgvector.password=postgres

# AI 模型配置
spring.ai.ollama.base-url=http://localhost:11434
spring.ai.ollama.model=llama2
```

## 服务类别

### 1. 向量存储

```yaml theme={"system"}
# 带 pgvector 的 PostgreSQL
pgvector:
  image: pgvector/pgvector:latest
  environment:
    POSTGRES_DB: vectordb
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
  ports:
    - "5432:5432"

# Milvus
milvus:
  image: milvusdb/milvus:latest
  ports:
    - "19530:19530"
    - "9091:9091"
```

### 2. AI 模型服务器

```yaml theme={"system"}
# Ollama
ollama:
  image: ollama/ollama:latest
  ports:
    - "11434:11434"
  volumes:
    - ollama_data:/root/.ollama

# LocalAI
localai:
  image: localai/localai:latest
  ports:
    - "8080:8080"
  volumes:
    - ./models:/models
```

### 3. 监控服务

```yaml theme={"system"}
# Prometheus
prometheus:
  image: prom/prometheus:latest
  ports:
    - "9090:9090"
  volumes:
    - ./prometheus.yml:/etc/prometheus/prometheus.yml

# Grafana
grafana:
  image: grafana/grafana:latest
  ports:
    - "3000:3000"
  volumes:
    - grafana_data:/var/lib/grafana
```

## 配置属性

```properties theme={"system"}
spring.ai.development.services.enabled=true
spring.ai.development.services.auto-start=true
spring.ai.development.services.port-range=8000-9000
```

## 最佳实践

<Note>
  使用开发时服务时，请考虑以下最佳实践：
</Note>

* **资源管理**：监控资源使用情况
* **数据持久化**：使用卷进行数据持久化
* **安全性**：保护开发环境
* **网络**：配置适当的网络隔离
* **文档**：记录服务配置

## 高级功能

### 自定义服务配置

```yaml theme={"system"}
# 自定义服务配置
services:
  custom-service:
    build:
      context: ./custom-service
      dockerfile: Dockerfile
    environment:
      CUSTOM_VAR: value
    ports:
      - "8080:8080"
```

### 服务健康检查

```yaml theme={"system"}
services:
  pgvector:
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
```

## 故障排除

常见问题和解决方案：

1. **服务启动问题**
   * 检查端口冲突
   * 验证资源可用性
   * 查看服务日志

2. **连接问题**
   * 检查网络配置
   * 验证服务健康状态
   * 测试连接性

3. **资源问题**
   * 监控资源使用情况
   * 调整资源限制
   * 清理未使用的资源

<Card title="文档有误？请协助编辑" icon="pencil" href="https://github.com/javaai-pig4cloud-com/javaai-docs/edit/main/spring-ai/api/docker-compose.mdx">
  发现文档问题？点击此处直接在 GitHub 上编辑并提交 PR，帮助我们改进文档！
</Card>
