You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加Llama Index依赖后Azure Functions在Docker中无法运行的解决办法

问题描述

我通过以下命令创建了一个Azure Function:

func init --worker-runtime python --docker
func new --name HttpExample --template "HTTP trigger"

随后使用以下Dockerfile构建镜像:

FROM mcr.microsoft.com/azure-functions/python:4-python3.10

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY requirements.txt /
RUN pip install -r /requirements.txt

COPY . /home/site/wwwroot

该简单Azure Function在本地通过func start运行正常,且能在Docker镜像中正常工作。

但之后开发了另一个引入多个Llama Index库的Azure Function应用,该应用在本地func start运行正常,却无法在Docker镜像中工作。访问http://localhost:8080/api/HttpExample时显示无页面,控制台出现“找不到作业函数”的警告。

核心代码如下:

import azure.functions as func
import datetime
import json
import logging
from model import predict

app = func.FunctionApp()

@app.route(route="HttpExample", auth_level=func.AuthLevel.ANONYMOUS)

def HttpExample(req: func.HttpRequest) -> func.HttpResponse:
    
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        name=predict(name)
        return func.HttpResponse(f"{name}")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in "
             "the query string or in the request body for a personalized response.",
             status_code=200
        )

model.py文件中引入了多个Llama Index库,requirements.txt内容如下:

azure-core
azure-functions
azure-identity
llama-index
llama-index-embeddings-azure-openai
llama-index-llms-azure-openai
llama-index-readers-file
llama-index-vector-stores-chroma
pypdf
解决方案

针对Docker环境下Azure Function找不到作业函数的问题,可按以下步骤排查修复:

1. 确保依赖完整安装

Llama Index相关库依赖复杂,Docker构建时可能因网络或版本冲突导致安装不完整。修改Dockerfile,添加详细日志并可选使用国内镜像加速:

FROM mcr.microsoft.com/azure-functions/python:4-python3.10

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

# 可选:添加国内PyPI镜像源加速安装
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

COPY requirements.txt /
# 安装时输出详细日志,便于排查失败项
RUN pip install -r /requirements.txt -v

COPY . /home/site/wwwroot

构建镜像时观察日志,确认所有依赖无报错安装完成。

2. 配置运行时兼容环境变量

当前代码使用v4编程式模型func.FunctionApp(),需确保Docker运行时支持该模式,在Dockerfile中添加环境变量:

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
    FUNCTIONS_WORKER_RUNTIME_VERSION=~4 \
    FUNCTIONS_V2_COMPATIBILITY_MODE=false

3. 排查model.py导入异常

model.py中Llama Index库的导入可能因Docker环境缺失系统依赖失败。在函数文件中添加导入日志定位问题:

import azure.functions as func
import datetime
import json
import logging

# 添加导入日志排查
try:
    from model import predict
    logging.info("Successfully imported model module")
except Exception as e:
    logging.error(f"Failed to import model module: {str(e)}", exc_info=True)

app = func.FunctionApp()

@app.route(route="HttpExample", auth_level=func.AuthLevel.ANONYMOUS)
def HttpExample(req: func.HttpRequest) -> func.HttpResponse:
    # 原有函数代码...

若日志显示系统依赖缺失(比如Chroma需要libpq-dev),则在Dockerfile中安装系统依赖:

FROM mcr.microsoft.com/azure-functions/python:4-python3.10

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

# 安装系统依赖(针对Chroma等库需求)
RUN apt-get update && apt-get install -y --no-install-recommends \
    libpq-dev \
    gcc \
    && rm -rf /var/lib/apt/lists/*

RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

COPY requirements.txt /
RUN pip install -r /requirements.txt -v

COPY . /home/site/wwwroot

4. 验证文件复制路径

确保所有代码文件(含model.py)被正确复制到/home/site/wwwroot,进入运行中的容器检查:

# 进入容器
docker exec -it <容器ID> /bin/bash
# 检查文件
ls /home/site/wwwroot

确认model.py和函数文件都在目标目录。

5. 调整函数装饰器写法

确保装饰器与函数定义紧密相邻,避免空行可能导致的识别问题:

@app.route(route="HttpExample", auth_level=func.AuthLevel.ANONYMOUS)
def HttpExample(req: func.HttpRequest) -> func.HttpResponse:
    # 函数代码...

内容的提问来源于stack exchange,提问作者Chirantan Datta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 21:55:05