本地更新Azure ML在线部署时出现gunicorn未找到错误
解决本地Azure ML在线端点更新时gunicorn找不到的问题
问题分析
本地使用az ml online-deployment update --local更新部署时,容器启动报错exec: gunicorn: not found,但云端部署正常。核心原因是本地容器不会自动激活conda环境,而云端Azure ML平台会自动处理环境激活和启动命令的路径映射,导致本地找不到conda环境中安装的gunicorn。
解决方案
1. 显式指定容器启动命令
修改blue-deployment.yml,在environment字段下添加启动命令,强制使用conda环境内的gunicorn:
$schema: https://azuremlschemas.azureedge.net/latest/managedOnlineDeployment.schema.json name: blue endpoint_name: my-endpoint model: path: ../../model-1/model/ code_configuration: code: ../../model-1/onlinescoring/ scoring_script: score.py environment: conda_file: ../../model-1/environment/conda.yml image: mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest # 添加启动命令,激活conda环境并启动gunicorn command: ["conda", "run", "-n", "model-env", "gunicorn", "--bind", "0.0.0.0:5001", "score:app"] instance_type: Standard_DS2_v2 instance_count: 1
conda run -n model-env:激活名为model-env的conda环境(对应你的conda.yml里的name字段)gunicorn --bind 0.0.0.0:5001 score:app:启动gunicorn服务,绑定5001端口,WSGI入口为score.py中的app对象(这是Azure ML评分脚本的默认要求)
2. 确保gunicorn正确安装到conda环境
在conda.yml的pip依赖列表中添加指定版本的gunicorn:
name: model-env channels: - conda-forge dependencies: - python=3.8 - numpy=1.21.2 - pip=21.2.4 - scikit-learn=0.24.2 - scipy=1.7.1 - pyodbc=4.0.34 - pip: - azureml-defaults==1.47.0 - inference-schema[numpy-support]==1.5 - joblib==1.0.1 - gunicorn==20.1.0 # 添加指定版本的gunicorn
3. 本地更新时复用镜像缓存
为了避免每次环境变更都重新构建完整镜像,更新时添加--use-docker-cache参数加速构建:
az ml online-deployment update --local -n blue --endpoint $ENDPOINT_NAME -f endpoints/online/managed/sample/blue-deployment.yml --use-docker-cache
验证步骤
- 执行更新命令后,用
docker ps查看容器状态,确认是否正常运行 - 查看容器日志:
docker logs <container-id>,检查是否有gunicorn启动成功的日志 - 测试本地端点:发送HTTP请求到
http://localhost:5001/score,验证服务是否正常响应
内容的提问来源于stack exchange,提问作者JonD03
相关产品推荐
相关产品推荐

