Skip to content

实战:Docker + Nginx 部署

使用 Docker 容器化部署 LangChain Agent 服务,并通过 Nginx 反向代理。

Dockerfile

dockerfile
FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 5000

CMD ["python", "agent_service.py"]

docker-compose.yml

yaml
version: "3.8"

services:
  agent:
    build: .
    ports:
      - "5000:5000"
    environment:
      - OPENAI_API_KEY=${OPENAI_API_KEY}
    restart: unless-stopped

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      - ./ssl:/etc/nginx/ssl
    depends_on:
      - agent
    restart: unless-stopped

Nginx 配置

nginx
server {
    listen 80;
    server_name agent.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name agent.example.com;

    ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;

    location /api/ {
        proxy_pass http://agent:5000/api/;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;

        # SSE 支持
        proxy_buffering off;
        proxy_cache off;
    }

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

构建与运行

bash
# 构建并启动
docker-compose up -d --build

# 查看日志
docker-compose logs -f agent

# 停止
docker-compose down

健康检查

yaml
services:
  agent:
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:5000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

下一步

本站为非官方中文学习站点,不代表 LangChain 官方。部分内容参考官方文档并重新整理为中文学习笔记。