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
Docker
Azure CLI
.NET Core 2.0
Docker Hub Account
- Build The Project
- Create Model
- Test Web API Locally
- Package Web API
- Test Web API Docker Image Locally
- Push Docker Image to Docker Hub
- Deploy to Azure Container Instances
- Test Deployed Application
git clone https://github.com/lqdev/mlnetacidemo.gitcd mlnetacidemo
dotnet restore
dotnet buildBefore we start our API, we need to create our machine learning model. This is done via the model project.
dotnet run -p model/model.csprojThe 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.
dotnet run -p api/api.csprojUse 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,
}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 .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>:latestUse 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
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>:latestThe 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.
az loginaz group create --name mlnetacidemogroup --location eastusaz group deployment create --resource-group mlnetacidemogroup --template-file azuredeploy.jsonGive 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