Deploying .NET Machine Learning Models with ML.NET, ASP.NET Core, Docker and Azure Container Instances (ACI)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.vscode
api
model
.dockerignore
.gitignore
Dockerfile
LICENSE
README.md
azuredeploy.json
mlnetacidemo.sln

README.md

Deploying .NET Machine Learning Models With ML.NET, ASP.NET Core, Docker and Azure Container Instances

This is a sample solution of how machine learning models built with ML.NET framework can be exposed to clients via an ASP.NET Core Web API that has been packaged into a Docker container and deployed to Azure Container Instances service. A more detailed walk-through can be found in this blog post at the following link

Requirements

Docker
Azure CLI
.NET Core 2.0
Docker Hub Account

Project Instructions

  1. Build The Project
  2. Create Model
  3. Test Web API Locally
  4. Package Web API
  5. Test Web API Docker Image Locally
  6. Push Docker Image to Docker Hub
  7. Deploy to Azure Container Instances
  8. Test Deployed Application

1. Build The Solution

Clone Repository

git clone https://github.com/lqdev/mlnetacidemo.git

Build Solution

cd mlnetacidemo
dotnet restore
dotnet build

2. Create Model

Before we start our API, we need to create our machine learning model. This is done via the model project.

dotnet run -p model/model.csproj

3. Test Web API Locally

The model project should have created a file called model.zip inside of its directory. Copy model.zip file inside the model project directory to the api project directory.

Start Web API

dotnet run -p api/api.csproj

Send Test Request

Use POSTMAN or Insomnia REST Clients to send a POST request to http://localhost:5000/api/predict that includes the following body

{
	"SepalLength": 3.3,
	"SepalWidth": 1.6,
	"PetalLength": 0.2,
	"PetalWidth": 5.1,
}

4. Package Web API

Using the Docker CLI tool, and your own Docker Hub username and image name of your choice, the following command will create a Docker image of the application using the Dockerfile in the root solution directory.

docker build -t <DOCKERUSERNAME>/<IMAGENAME>:latest .

5. Test Web API Docker Image Locally

Start Docker Image

Using your DockerHub username and recently created image name from the last example, enter the following command to start the Docker image locally and bind it to port 5000.

docker run -p 5000:80 <DOCKERUSERNAME>/<IMAGENAME>:latest

Send Test Request

Use POSTMAN or Insomnia REST Clients to send a POST request to http://localhost:5000/api/predict that includes the following body

{
	"SepalLength": 3.3,
	"SepalWidth": 1.6,
	"PetalLength": 0.2,
	"PetalWidth": 5.1,
}

To stop the container, use Ctrl + C

6. Push Docker Image to Docker Hub

To make our Docker image accessible to everyone else and the Azure Container Instance service, we need to push it to Docker Hub. This can be done with the following command

docker login
docker push <DOCKERUSERNAME>/<IMAGENAME>:latest

7. Deploy to Azure Container Instances

Prepare Manifest File

The azuredeploy.json file helps define the deployment configurations for the application. However, in order to specify your Docker image as the one that will be deployed, the containerimage property needs to be replaced with your Docker Hub username and name of the image you just pushed to Docker Hub.

Deploy Application

Login

az login

Create Resource Group

az group create --name mlnetacidemogroup --location eastus

Deploy

az group deployment create --resource-group mlnetacidemogroup --template-file azuredeploy.json

8. Test Deployed Application

Give it a few minutes for your deployment to initialize. If the deployment was successful, you should see some output on the command line. Look for the ContainerIPv4Address property. This is the IP Address where your container is accessible. In POSTMAN or Insomnia, replace the URL to which you previously made a POST request to with http://<ContainerIPv4Address>/api/predict where ContainerIPv4Address is the value that was returned to the command line after the deployment. If successful, the response should be just like previous requests Iris-virginica.

Once you’re finished, you can clean up resources with the following command:

az group delete --name mlnetacidemogroup