【问题标题】:azureml how to create a webservice from docker imageazureml 如何从 docker 映像创建 web 服务
【发布时间】:2022-08-24 11:22:41
【问题描述】:

使用模型包获得了我已注册到 docker 映像中的所有机器学习模型。如何将此 docker 映像部署到 Web 服务

# Define the deployment configuration
aciconfig = AciWebservice.deploy_configuration(
      cpu_cores = 1,
      memory_gb = 1,
      dns_name_label = os.environ['ACI_DNS_NAME_LABEL'])


#create env 
environment = Environment('env')
environment.python.conda_dependencies = CondaDependencies.create(conda_packages=[
'pip==20.2.4'],
 pip_packages=[
 'azureml-defaults',
 'joblib',
 'numpy',
 'scikit-learn'])

    
inf_conf = InferenceConfig(entry_script="score.py",environment=environment)
#crete docker image    
docker_image = Model.package(ws,models_latest, inf_conf,image_name="imgname")
docker_image.wait_for_creation(show_output=True) 
docker_image.pull()

【问题讨论】:

    标签: python azure-machine-learning-service azureml-python-sdk


    【解决方案1】:

    在开始部署之前,我们需要有一个训练有素的模型来方便部署。由于经过训练的模型可用并且需要将流程部署为 Web 服务。

    查看为 Web 应用创建容器资源的过程。

    点击 ”创建资源

    点击 ”容器”在左侧面板中

    点击网络应用为了容器然后点击创造

    提供所需的详细信息并保留容器详细信息以供进一步使用。

    docker_image = Model.package(ws,models_latest, inf_conf,image_name="imgname")
    docker_image.wait_for_creation(show_output=True) 
    docker_image.pull()
    

    使用**image.pull()** 方法后,我们将收到有关我们创建的 docker 映像的进程通知。

    Status: Downloaded newer image for myworkspacef78fd10.azurecr.io/package:packagenumber
    

    下载 docker 镜像后,使用命令“docker images”获取本地镜像列表

    REPOSITORY          name.azurecr.io/package
    TAG                 Your docker tag
    IMAGE ID            Your Image ID
    CREATED             Time created
    SIZE                Size of the container
    

    数据包含<image id>,我们需要用下面的语法替换它

    docker run -p 6789:5001 --name containername <imageid>
    

    **6789** 是本地端口号,5001是 Web 服务侦听号码。

    创建 Dockerfile 和依赖项

    package = Model.package(ws, [model], inference_config, generate_dockerfile=True)
    package.wait_for_creation(show_output=True)
    # Download the package.
    package.save("./imagefiles")
    # Get the Azure container registry that the model/Dockerfile uses.
    acr=package.get_container_registry()
    print("Address:", acr.address)
    print("Username:", acr.username)
    print("Password:", acr.password)
    

    上面的代码块帮助我们在**imagefiles** 目录下下载构建镜像所需的文件。

    我们需要使用shell来验证docker镜像

    docker login <address> -u <username> -p <password>
    

    现在,构建 docker 镜像

    docker build --tag myimage <imagefiles>
    

    要运行容器,请使用以下命令,该命令基于端口和 Web 服务号进行侦听。

    docker run -p 6789:5001 --name mycontainer image_name:latest
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2019-06-13
      相关资源
      最近更新 更多