Docker Compose快速搭建TensorFlow服务环境

Quincy413 +0/-0 0 0 正常 2025-12-24T07:01:19 TensorFlow · Docker · Serving

Docker Compose快速搭建TensorFlow服务环境

在微服务架构中,TensorFlow Serving的容器化部署是实现模型快速迭代和弹性伸缩的关键。本文将通过Docker Compose快速构建一个完整的TensorFlow服务环境。

核心配置文件

首先创建docker-compose.yml文件:

version: '3.8'

services:
  tensorflow-serving:
    image: tensorflow/serving:latest-gpu
    container_name: tf-serving
    ports:
      - "8500:8500"   # gRPC端口
      - "8501:8501"   # HTTP端口
    volumes:
      - ./models:/models
      - ./config:/config
    environment:
      - MODEL_NAME=mnist_model
      - MODEL_BASE_PATH=/models
    deploy:
      resources:
        reservations:
          memory: 2G
        limits:
          memory: 4G
    restart: unless-stopped

  nginx:
    image: nginx:alpine
    container_name: tf-nginx
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - tensorflow-serving
    restart: unless-stopped

负载均衡配置

nginx.conf中配置负载均衡:

upstream tensorflow_servers {
    server tf-serving:8500 weight=1;
    server tf-serving:8500 weight=1;
}

server {
    listen 80;
    location / {
        proxy_pass http://tensorflow_servers;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

部署步骤

  1. 创建模型目录:mkdir -p models/mnist_model/1
  2. 将模型文件放入版本目录
  3. 启动服务:docker-compose up -d
  4. 验证服务:curl http://localhost:8501/v1/models/mnist_model

此方案实现了容器化部署与负载均衡的快速配置,适用于生产环境的TensorFlow服务化。

推广
广告位招租

讨论

0/2000
Nina57
Nina57 · 2026-01-08T10:24:58
Docker Compose 部署 TF Serving 时,建议明确指定 GPU 版本标签如 `latest-gpu`,并确保宿主机已安装 NVIDIA Container Toolkit,避免镜像拉取失败或运行时找不到 CUDA。实际项目中可加入 healthcheck 检测模型加载状态。
NiceSky
NiceSky · 2026-01-08T10:24:58
nginx 负载均衡配置虽简单,但生产环境需考虑会话保持、超时设置及健康检查机制,建议增加 `proxy_connect_timeout` 和 `proxy_send_timeout` 参数,并结合 Prometheus 监控服务可用性。