使用Azure CLI自定义Docker容器在Azure ML训练时遇文件找不到错误
解决Azure ML中自定义Docker容器找不到Python脚本的问题
问题原因
Azure ML Command Job启动容器时,默认工作目录并非Docker镜像中设置的WORKDIR,而是/mnt/azureml。这就导致即使你的镜像中存在test_local.py,在Azure ML的默认工作路径下也无法找到;而本地docker run会默认使用镜像的WORKDIR,所以能正常执行。
解决方案
你可以通过以下几种方式解决:
1. 在yaml中指定镜像的工作目录
修改train.yaml的environment部分,添加working_directory参数,设置为你Docker镜像中test_local.py所在的目录(比如镜像的WORKDIR是/app):
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json type: command environment: image: user2001.azurecr.io/test/train:latest working_directory: /app # 替换为你镜像中脚本所在的实际目录 command: >- python test_local.py compute: azureml:test1234
2. 在命令中切换到脚本所在目录执行
修改command部分,先切换到脚本所在的绝对路径,再执行脚本:
command: >- cd /path/to/your/script && python test_local.py
将/path/to/your/script替换为test_local.py在镜像中的实际绝对路径。
3. 使用脚本的绝对路径直接执行
如果知道test_local.py在镜像中的绝对路径(比如/app/test_local.py),直接在命令中使用绝对路径:
command: >- python /app/test_local.py
验证步骤
- 先通过本地命令确认脚本在镜像中的绝对路径:
# 查看镜像的默认工作目录 docker run -it user2001.azurecr.io/test/train:latest pwd # 确认脚本位置 docker run -it user2001.azurecr.io/test/train:latest ls /app
- 根据获取到的路径,选择上述任一方案修改
train.yaml,重新提交任务即可。
内容的提问来源于stack exchange,提问作者Prateek Singh
相关产品推荐
相关产品推荐

