Create Microservice Using Custom Base Layer
Supported Platforms: Windows®, Linux®, macOS
This example shows how to create a microservice Docker® image using a customized base layer. Creating a custom base layer allows you to use your preferred version of Linux or other operating system as the foundation and run commands or install additional dependencies.
Docker container images are composed of layers. In this example, you create three layers, each containing a set of file-system changes that build on top of the previous layers.
Custom base layer (your chosen OS)
Secondary layer (MATLAB® Runtime)
Application layer (your microservice)
Prerequisites
Verify that you have MATLAB Compiler SDK™ 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.
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
docs.docker.com/desktop/setup/install/windows-install/.To install Docker on WSL2, see
https://www.mathworks.com/matlabcentral/answers/1758410-how-do-i-install-docker-on-wsl2.
The computer you are using must be connected to the Internet.
Note
Some deployable archives, such as Simulink® Compiler™ artifacts, are not cross-platform compatible and must be built on Linux to use with Docker. For more information, see Limitations for MATLAB Compiler.
Create Production Server Archive Using compiler.build.productionServerArchive
In MATLAB, locate the MATLAB function that you want to deploy as a microservice. For this example,
create the function mirror.m using the following code.
function out = mirror(in)
out = in;Create a production server archive using
compiler.build.productionServerArchive.
buildResults = compiler.build.productionServerArchive("mirror.m","Verbose",true);
Build Custom Base Layer Using Docker
You can customize the base layer using a Dockerfile.
For this example, create a new file named Dockerfile.customdeps.
The file should specify the Linux distribution and install all of the dependencies
required for MATLAB Runtime.
You can add commands to the Dockerfile to customize the behavior of the base layer.
For example, add proxy information to the apt configuration file.
RUN echo 'Acquire::http::Proxy "http://yourproxyaddress:proxyport";' >> /etc/apt/apt.confYou can also install additional dependencies to fulfill any requirements for your deployment.
Packages are sourced from https://github.com/mathworks-ref-arch/container-images/tree/main/matlab-runtime-deps/ for Ubuntu 24.04. If your internal OS image is derived from any other Linux distribution, you must recreate the list of dependencies for that OS.
At the MATLAB command window, build the custom base layer image using Docker and the Dockerfile you created.
depsImageName = "mycompanybase:r2024b"; system("docker build -f Dockerfile.customdeps -t " + depsImageName + " .");
Note
If you are using WSL, type wsl docker build instead.
Create Secondary Layer with MATLAB Runtime
Create a MATLAB Runtime Docker image layer that uses the custom base layer you created. Use name-value arguments to specify the base layer image and the MATLAB Runtime image name.
runtimeImageName = "custom-matlabruntime:r2024b"; compiler.runtime.createDockerImage(buildResults, ... "BaseImage",depsImageName, ... "ImageName",runtimeImageName)
Note
If you are unable to reach the Docker repositories, then you must first run compiler.runtime.createInstallerDockerImage with the path to the
Linux
MATLAB Runtime installer.
Create Application Layer by Packaging MATLAB Microservice
Create a microservice Docker image for the production server archive. Specify the name of the
MATLAB Runtime
Docker image you created as an input to the
compiler.package.microserviceDockerImage function.
compiler.package.microserviceDockerImage(buildResults, ... "ImageName","mirror-micro","RuntimeImage",runtimeImageName);
You can deploy the microservice image using Docker and make calls to the service using any programming language that has HTTP libraries, including MATLAB Production Server™ client APIs.
For details about pushing your image to Docker Hub or your private registry, consult the Docker documentation.
Test Docker Image
Note
If Docker is running in a WSL2 session, preface the following commands with
wsl.
At the system command line, run the
mirror-micromicroservice image in Docker.docker run --rm -p 9900:9910 mirror-microPort 9910 is the default port exposed by the microservice within the Docker container. You can map it to any available port on your host machine. For this example, it is mapped to port 9900.
You can specify additional options in the Docker command. For a complete list of options, see Microservice Command Arguments.
Once the container is running in Docker, you can check the status of the service by opening the following URL in a web browser:
http://hostname:9900/api/health
Note
Use
localhostas the hostname if Docker is running on the same machine as the browser. If you are using Docker Desktop, you can access the container usinghost.docker.internal.If the service is ready to receive requests, you see the following message:
"status: ok"
Test the running service. In the terminal, use the
curlcommand to send a JSON query with the input argument4to the service through port 9900. For more information on constructing JSON requests, see JSON Representation of MATLAB Data Types (MATLAB Production Server).curl -v -H Content-Type:application/json -d '{"nargout":1,"rhs":[4]}' "http://<hostname>:9900/mirror/mirror"The output is the same as the input.
{"lhs":[{"mwdata":[4],"mwsize":[1,1],"mwtype":"double"}]}Note
To use
curlon Windows, use the following syntax:curl -v -H Content-Type:application/json -d "{\"nargout\":1,\"rhs\":[4]}" "http://<hostname>:9900/mirror/mirror"To stop the service, use the following command to display the container id.
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 065546e98708 mirror-micro "/opt/matlabruntime/…" 4 minutes ago Up 4 minutes 0.0.0.0:9900->9910/tcp silly_slawStop the service using the specified container id.
docker stop 065546e98708
See Also
compiler.build.productionServerArchive | compiler.runtime.createDockerImage | compiler.package.microserviceDockerImage
