Files
life-echo/api/Dockerfile
iammm0 0b7bd37d5d feat: Docker 构建预置 ASR 模型,支持离线使用
- Dockerfile 构建时预下载 faster-whisper 模型到镜像
- docker-compose 增加 ASR_MODEL_CACHE_DIR 环境变量
- asr_service 支持从缓存目录加载本地模型,无需运行时联网下载

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-10 15:08:00 +08:00

57 lines
1.8 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Life Echo API Dockerfile
FROM python:3.11-slim
# 设置工作目录
WORKDIR /app
# 安装系统依赖
RUN apt-get update && apt-get install -y \
gcc \
g++ \
libc6-dev \
libffi-dev \
libxml2-dev \
libxslt1-dev \
libjpeg-dev \
zlib1g-dev \
libpng-dev \
libfreetype6-dev \
&& rm -rf /var/lib/apt/lists/*
# 复制依赖文件
COPY requirements.txt .
# 安装Python依赖
RUN pip install --no-cache-dir --upgrade pip && \
pip install --no-cache-dir -r requirements.txt
# 复制应用代码(先复制代码,避免覆盖环境变量文件)
COPY . .
# 构建时预下载 faster-whisper 模型到镜像,云端运行时直接使用本地模型无需再下载
ARG ASR_MODEL_SIZE=small
RUN mkdir -p /app/models/whisper && \
python -c "from faster_whisper import WhisperModel; WhisperModel('${ASR_MODEL_SIZE}', device='cpu', compute_type='int8', download_root='/app/models/whisper')"
# 复制生产环境配置文件到镜像中
# 将 .env.production 复制为 .env供应用运行时使用
# 注意:构建前请确保 .env.production 文件存在
# 这一步放在最后,确保覆盖任何开发环境的 .env 文件
COPY .env.production ./.env
# 创建非root用户
RUN useradd -m -u 1000 appuser && \
chown -R appuser:appuser /app
# 切换到非root用户
USER appuser
# 暴露端口
EXPOSE 8000
# 健康检查(使用 Python 内置的 http.client无需额外依赖
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD python -c "import http.client; conn = http.client.HTTPConnection('localhost', 8000); conn.request('GET', '/health'); r = conn.getresponse(); exit(0 if r.status == 200 else 1)" || exit 1
# 启动命令
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]