Run Microservice Created Using MATLAB Compiler SDK on Microsoft Azure
This example shows how to run a microservice created using MATLAB® Compiler SDK™ on Microsoft® Azure®.
Prerequisites
Verify that you have MATLAB Compiler SDK (R2022a or later) installed on the development machine.
Verify that you have Docker® installed and configured on the development machine by typing
[~,msg] = system('docker version')
in a MATLAB command window.
Note: If you are using WSL, use [~,msg] = system('wsl docker version')
instead.
If you do not have Docker installed, follow the instructions on the Docker website to install and set up Docker. See https://docs.docker.com/engine/install/.
To build microservice images on Windows®, you must install either Docker Desktop or Docker on Windows Subsystem for Linux v2 (WSL2).
To install Docker Desktop, see https://docs.docker.com/desktop/install/windows-install/.
To install Docker on WSL2, see https://www.mathworks.com/matlabcentral/answers/1758410-how-do-i-install-docker-on-wsl2.
Verify that you have the MATLAB Runtime installer. If you don't, you can download the installer from the MathWorks® website: https://www.mathworks.com/products/compiler/matlab-runtime.html.
Verify that you have a Microsoft Azure account.
Verify that you have installed the Azure Command-Line Interface (CLI). For details, see: https://learn.microsoft.com/en-us/cli/azure/install-azure-cli.
Create MATLAB Function
Create a MATLAB function from the MATLAB desktop. For this example, write a function named simpInterest.m
using the following code.
function i = simpInterest(p,r,t) i = p * (1 + (r * t)) - p; end
Create Deployable Archive
Package the simpInterest.m
function into a deployable archive using the compiler.build.productionServerArchive
function.
results = compiler.build.productionServerArchive("simpInterest.m","ArchiveName","financetools","Verbose","on")
Package Deployable Archive into Microservice Docker Image
Package the deployable archive into a microservice Docker image using the results
object that you created.
compiler.package.microserviceDockerImage(results,
"ImageName"
,
"financetools"
)
The function generates the following files within a folder named financetoolsmicroserviceDockerImage
:
applicationFilesForMATLABCompiler/financetools.ctf
— Deployable archive file.Dockerfile
— Docker file that specifies run-time options.GettingStarted.txt
— Text file that contains deployment information.
Verify that the financetools
image has been created by typing:
docker images
For details, see MATLAB Compiler SDK Create Microservice Docker Image.
Creating Azure Container Registry
Navigate to https://portal.azure.com/ and search for
container registry
in the search bar.Select the Container registries service from the results.
Click Create a new registry. Complete the Project details and Instance details.
Project details
Subscription: Select your subscription from the drop-down menu.
Resource group: Use an existing resource group or create a new one.
Instance details
Registry name: Provide a unique name. This example uses:
matlab
Location: Select your desired location.
Availability zones: Leave unchecked or check Enabled.
SKU: Select Standard.
In this example the registry is named matlab
.
Upload Microservice Docker Image to Azure Container Registry
Open a command window and log in to your Azure account using the command
az login
This opens a browser window for you to log in and authenticate. Once you are logged into the Azure portal, you need to log in to your container registry using the command:
az login acr --name <container_registry_name>
Replace <container_registry_name>
with the name of your container registry.
Tag the microservice Docker image with your container registry using the command:
docker tag financetools matlab.azurecr.io/financetools:1.0
Upload the microservice Docker image using:
docker push matlab.azurecr.io/financetools:1.0
Uploading the microservice Docker image to the cloud takes a few minutes. When complete, your Docker image is visible as a repository in your Azure container registry.
Run Microservice Docker Image
You can run your microservice Docker image using one of several different options on Azure. You can run it as an:
Azure Container Instance
Azure Kubernetes® Service
Azure Web App for Containers
Azure Batch
In this example, use the Azure Container Instance option, which lets you run a single container.
Note: You might have to enable admin access in the IAM section in your Azure Container Registry to run the container.
To run it as an Azure Container Instance:
From the Azure dashboard create a new Azure Container Instance.
In the Basics tab, complete the Project details and the Container details.
Project details
Subscription: Select appropriately
Resource group: Select appropriately
Container details
Container name:
financetools
Region: Use default or select appropriately
Availability: Use default or select appropriately
SKU:
Standard
or select appropriatelyImage source: Select Azure Container Registry
Registry:
<container-registry-name>
Image:
financetools
Image tag:
1.0
OS type: Use default
Size: Use default or change size appropriately
In the Networking tab complete the following details:
Networking type:
Public
DNS name label:
financetools
DNS name label scope reuse: Select
Tenant
Ports: Type
9910
Ports protocol: Select
TCP
Click on 'Review + Create'.
Click 'Create' to create the Azure Container Instance.
Once your Azure Container Instance is deployed, you can make requests to the instance using its Fully Qualified Domain Name (FQDN), which you can find on the resource page.
Make Request to Microservice Running in Azure Container Instance
You can make a request to the microservice using a command line tool or UI such as Postman. Pass in the 3 input variables for principal, interest, and time in JSON format, and receive a result with the simple interest amount as a JSON formatted result.
POST /financetools/simpInterest HTTP/1.1 Host: financetools.b9cshvg0azdbb6g6.eastus.azurecontainer.io:9910 Content-Type: application/json Content-Length: 42 {"nargout": 1, "rhs": [21000, 0.043, 12] } The format of the host URI is: FQDN:port/container_image_name/matlab_function_name To make a request using cURL: curl --location 'financetools.b9cshvg0azdbb6g6.eastus.azurecontainer.io:9910/financetools/simpInterest' \ --header 'Content-Type: application/json' \ --data '{"nargout": 1, "rhs": [21000, 0.043, 12] }'