在Docker容器中为Rasa动作服务器安装Faiss失败问题排查
问题与解决方案:Rasa动作服务器集成Faiss构建失败
问题描述
尝试将Faiss库集成至Rasa动作服务器,因faiss-cpu官方建议通过conda安装,编写了包含Rasa SDK和FAISS的Dockerfile,但构建失败,日志显示找不到/opt/conda/bin/conda。需排查错误原因,并确认是否适合采用多阶段构建。
提供的Dockerfile内容:
FROM rasa/rasa-sdk:3.5.1 WORKDIR /app COPY ./requirements-actions.txt requirements.txt COPY . /app USER root RUN pip install --no-cache-dir -r requirements.txt RUN apt-get update && \ apt-get install -y curl bzip2 && \ curl https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh > /tmp/conda.sh && \ bash /tmp/conda.sh -b -p /opt/conda && \ /opt/conda/bin/conda update -n base conda && \ /opt/conda/bin/conda install -y -c pytorch faiss-cpu && \ apt-get remove -y --auto-remove curl bzip2 && \ apt-get clean && \ rm -fr /tmp/conda.sh ENV PATH="/opt/conda/bin:${PATH}" USER 1001 EXPOSE 5055 ENTRYPOINT ["python", "-m", "rasa_sdk", "--actions", "actions"]
构建日志报错:
#11 54.72 /bin/bash: line 1: /opt/conda/bin/conda: No such file or directory ------ executor failed running [/bin/bash -o pipefail -c apt-get update && apt-get install -y curl bzip2 && curl https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh > /tmp/conda.sh && bash /tmp/conda.sh -b -p /opt/conda && /opt/conda/bin/conda update -n base conda && /opt/conda/bin/conda install -y -c pytorch faiss-cpu && apt-get remove -y --auto-remove curl bzip2 && apt-get clean && rm -fr /tmp/conda.sh]: exit code: 127
问题分析与解决方案
1. 错误原因定位
核心问题是Miniconda安装过程未成功完成,导致/opt/conda/bin/conda文件不存在,可能的触发因素:
- Miniconda安装脚本下载不完整(网络波动导致
/tmp/conda.sh损坏) - 下载链接未跟随重定向(原curl命令缺少
-L参数,无法处理跳转) - 安装脚本执行时出现静默错误(概率较低,已切换至root用户)
2. 修复方案
方案一:确保Miniconda安装完整
修改RUN命令,添加重定向参数并增加安装后验证步骤:
RUN apt-get update && \ apt-get install -y curl bzip2 && \ # 添加-L参数跟随重定向,确保脚本下载完整 curl -L https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh > /tmp/conda.sh && \ bash /tmp/conda.sh -b -p /opt/conda && \ # 验证conda文件是否生成,方便排查 ls -la /opt/conda/bin/conda && \ /opt/conda/bin/conda update -n base conda -y && \ /opt/conda/bin/conda install -y -c pytorch faiss-cpu && \ apt-get remove -y --auto-remove curl bzip2 && \ apt-get clean && \ rm -fr /tmp/conda.sh
方案二:改用pip安装faiss-cpu(更简洁稳定)
虽然官方推荐conda,但faiss-cpu已提供PyPI包,直接通过pip安装可避免conda的复杂配置:
- 在
requirements-actions.txt中添加:
faiss-cpu>=1.7.4
- 删除Dockerfile中所有conda相关的RUN命令,仅保留pip安装步骤即可。
3. 多阶段构建的适用性
适合采用多阶段构建,尤其是使用conda安装的场景,优势如下:
- 分离依赖构建与运行环境,减少最终镜像体积
- 避免将conda的庞大冗余文件带入生产镜像
示例多阶段Dockerfile:
# 第一阶段:构建faiss-cpu依赖 FROM ubuntu:22.04 as builder RUN apt-get update && apt-get install -y curl bzip2 RUN curl -L https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh > /tmp/conda.sh && \ bash /tmp/conda.sh -b -p /opt/conda && \ /opt/conda/bin/conda install -y -c pytorch faiss-cpu && \ # 清理冗余包,减小镜像体积 rm -fr /tmp/conda.sh /opt/conda/pkgs/* # 第二阶段:构建Rasa动作服务器 FROM rasa/rasa-sdk:3.5.1 WORKDIR /app COPY ./requirements-actions.txt requirements.txt COPY . /app USER root RUN pip install --no-cache-dir -r requirements.txt # 从builder阶段复制faiss的安装文件(注意匹配Python版本,rasa-sdk:3.5.1使用Python3.10) COPY --from=builder /opt/conda/lib/python3.10/site-packages/faiss* /usr/local/lib/python3.10/site-packages/ USER 1001 EXPOSE 5055 ENTRYPOINT ["python", "-m", "rasa_sdk", "--actions", "actions"]
内容的提问来源于stack exchange,提问作者vonbecker
相关产品推荐
相关产品推荐

