create microservice docker image
supported platform: linux®, windows®, macos
this example shows how to create a microservice docker® image. the microservice image created by matlab® compiler sdk™ provides an http/https endpoint to access matlab code.
you package a matlab function into a deployable archive, and then create a docker image that contains the archive and a minimal matlab runtime package. you can then run the image in docker and make calls to the service using any programming language that has http libraries, including matlab production server™ client apis.
this option is best for developers who want to incorporate a matlab algorithm or simulink® simulation within a larger application as a service, or to provide a synchronous request-response backend api service. to create a docker image that contains a standalone application, see .
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.
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
.
to install docker on wsl2, see
.
if the computer you are using is not connected to the internet, you must download the matlab runtime installer for linux from a computer that is connected to the internet and transfer the installer to the offline computer. then, run the command
(filepath)
, wherefilepath
is the path to the transferred matlab runtime installer archive.you can download the installer from the mathworks website.
create matlab function
in matlab, examine the matlab program that you want to package.
for this example, write a function named mymagic.m
using the
following code.
function y = mymagic(x)
y = magic(x);
at the matlab command prompt, enter mymagic(5)
.
the output is a 5-by-5 magic square matrix.
ans = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
create deployable archive
package the mymagic
function into a deployable archive using the
compiler.build.productionserverarchive
function.
specify additional options in the compiler.build
command by using
name-value arguments. for details, see .
optionally, you can add a function signature file to help clients use your matlab functions. for more details, see .
mpsresults = compiler.build.productionserverarchive('mymagic.m',... 'functionsignatures','mymagicfunctionsignatures.json',... 'archivename','magicarchive','verbose','on')
mpsresults = results with properties: buildtype: 'productionserverarchive' files: {'/home/mluser/work/magicarchiveproductionserverarchive/magicarchive.ctf'} includedsupportpackages: {} options: [1×1 compiler.build.productionserverarchiveoptions]
the object mpsresults
contains
information on the build type, generated files, included support packages, and build
options.
once the build is complete, the function creates a folder named
magicarchiveproductionserverarchive
in your current directory
that contains the deployable archive.
package deployable archive into docker image
build the microservice docker image using the mpsresults
object that you
created.
specify additional options in the compiler.build
command by using
name-value arguments. for details, see .
compiler.package.microservicedockerimage(mpsresults,'imagename','micro-magic')
the function generates the following files within a folder named
micro-magicmicroservicedockerimage
:
applicationfilesformatlabcompiler/magicarchive.ctf
— deployable archive file.dockerfile
— docker file that specifies run-time options.gettingstarted.txt
— text file that contains deployment information.
test docker image
note
if docker is running in a wsl2 session, preface the following commands with
wsl
.
in a linux terminal, verify that your
micro-magic
image is in your list of docker images.docker images
repository tag image id created size micro-magic latest 4401fa2bc057 23 seconds ago 1.42gb matlabruntime/r2023a/update0/4200000000000000 latest 5259656e4a32 24 hours ago 1.42gb
run the
micro-magic
microservice image in docker.docker run --rm -p 9900:9910 micro-magic
port 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 .
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
localhost
as the hostname if docker is running on the same machine as the browser.if the service is ready to receive requests, you see the following message:
"status: ok"
test the running service. in the terminal, use the
curl
command to send a json query with the input argument4
to the service through port 9900. for more information on constructing json requests, see (matlab production server).curl -v -h content-type:application/json -d '{"nargout":1,"rhs":[4]}' \ "http://
hostname
:9900/magicarchive/mymagic"the output is:
{"lhs":[{"mwdata":[16,5,9,4,2,11,7,14,3,10,6,15,13,8,12,1],\ "mwsize":[4,4],"mwtype":"double"}]}
note
to use
curl
on windows, use the following syntax:curl -v -h content-type:application/json -d "{\"nargout\":1,\"rhs\":[4]}" \ "http://
hostname
:9900/magicarchive/mymagic"to stop the service, use the following command to display the container id.
docker ps
container id image command created status ports names df7710d69bf0 micro-magic "/opt/matlabruntime/…" 6 minutes ago up 6 minutes 0.0.0.0:9900->9910/tcp epic_herschel
stop the service using the specified container id.
docker stop df7710d69bf0
share docker image
you can share your docker image in various ways.
push your image to the docker central registry dockerhub, or to your private registry. this is the most common workflow.
save your image as a tar archive and share it with others. this workflow is suitable for immediate testing.
for details about pushing your image to dockerhub or your private registry, consult the docker documentation.
save docker image as tar archive
to save your docker image as a tar archive, open a system command window, navigate to the docker context folder, and type the following.
docker save micro-magic -o micro-magic.tar
this command creates a file named micro-magic.tar
in the
current folder. set the appropriate permissions (for example, using
chmod
) prior to sharing the tarball with other users.
load docker image from tar archive
load the image contained in the tarball on the end user machine.
docker load --input micro-magic.tar
verify that the image is loaded.
docker images
run docker image
docker run --rm -p 9900:9910 micro-magic
see also
|
related topics
- client programming (matlab production server)
- (matlab production server)