PackageMATLABStandalone Applications intoDockerImages
Supported Platform:Linux®only.
This example shows how to package a MATLAB®standalone application into a Docker®image.
This option is best for developers who want to distribute an application in a standardized format with all dependencies included, or to run batch jobs in an orchestrator. To create a microservice Docker image that provides an HTTP/HTTPS endpoint, seeCreate Microservice Docker Image(MATLAB Compiler SDK).
Prerequisites
Verify that you have Docker installed on your Linux machine by typing
docker
in the terminal. If you do not have Docker installed, you can follow the instructions on the Docker website to install and set up Docker.Test your Docker installation by typing the following at the system terminal:
docker run hello-world
Hello from Docker! This message shows that your installation appears to be working correctly.
Verify thatMATLAB Runtimeinstaller is available on your machine. You can verify its existence by executing the
compiler.runtime.download
函数在MATLAB命令提示符。如果有an existing installer on the machine, the function returns its location. Otherwise, it downloads theMATLAB Runtimeinstaller matching the version and update level of MATLAB from where the command is executed.If the computer you are using is not connected to the Internet, you must download theMATLAB Runtimeinstaller from a computer that is connected to the Internet. After downloading theMATLAB Runtimeinstaller, you need to transfer the installer to the offline computer. You can download the installer from the MathWorks website.
Create Function inMATLAB
Write a MATLAB function calledmymagic
and save it asmymagic.m
.
functionmymagic(x) y = magic(x); disp(y)
测试功能ion at the MATLAB command prompt.
mymagic(5)
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 Standalone Application
Package themymagic
function into a standalone application using thecompiler.build.standaloneApplication
function.
res = compiler.build.standaloneApplication('mymagic.m','TreatInputsAsNumeric',真正的)
res = Results with properties: BuildType: 'standaloneApplication' Files: {3×1 cell} Options: [1×1 compiler.build.StandaloneApplicationOptions]
TheResults
objectres
returned at the MATLAB command prompt contains information about the build.
Once the build is complete, the function creates a folder namedmymagicstandaloneApplication
in your current directory to store the standalone application.
Package Standalone Application intoDockerImage
CreateDockerOptions
Object
Prior to creating a Docker image, create aDockerOptions
object using thecompiler.package.DockerOptions
function and pass theResults
objectres
and an image namemymagic-standalone-app
as input arguments. Thecompiler.package.DockerOptions
function lets you customize Docker image packaging.
opts = compiler.package.DockerOptions(res,'ImageName','mymagic-standalone-app')
opts = DockerOptions with properties: EntryPoint: 'mymagic' ExecuteDockerBuild: on ImageName: 'mymagic-standalone-app' DockerContext: './mymagic-standalone-appdocker'
CreateDockerImage
Create a Docker image using thecompiler.package.docker
function and pass theResults
objectres
and theDockerOptions
objectopts
as input arguments.
compiler.package.docker(res,'Options',opts)
Generating Runtime Image Cleaning MATLAB Runtime installer location. It may take several minutes... Copying MATLAB Runtime installer. It may take several minutes... ... ... ... Successfully built 6501fa2bc057 Successfully tagged mymagic-standalone-app:latest DOCKER CONTEXT LOCATION: /home/user/MATLAB/work/mymagic-standalone-appdocker SAMPLE DOCKER RUN COMMAND: docker run --rm -e "DISPLAY=:0" -v /tmp/.X11-unix:/tmp/.X11-unix mymagic-standalone-app
Once packaging is complete, the function creates a folder namedmymagic-standalone-appdocker
in your current directory. This folder is the Docker context and contains theDockerfile. Thecompiler.package.docker
function also returns the location of the Docker context and a sample Docker run command. You can use the sample Docker run command to test whether your image executes correctly. If the application requires input arguments, append them to the sample command.
During the packaging process, the necessary bits forMATLAB Runtimeare packaged as a parent Docker image and the standalone application is packaged as a child Docker image.
TestDockerImage
Open a Linux terminal and navigate to the Docker context folder. Verify that themymagic-standalone-app
Docker image is listed in your list of Docker images.
$docker images |
REPOSITORY TAG IMAGE ID CREATED SIZE mymagic-standalone-app latest 6501fa2bc057 23 seconds ago 1.03GB matlabruntime/r2023a/update0/4000000000000000 latest c6eb5ba4ae69 24 hours ago 1.03GB
After verifying that themymagic-standalone-app
Docker image is listed in your list of Docker images, execute the sample run command with the input argument5
:
$docker run --rm -e "DISPLAY=:0" -v /tmp/.X11-unix:/tmp/.X11-unix mymagic-standalone-app 5 |
No protocol specified out = 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
The standalone application is packaged and can now be run as a Docker image.
Note
When running applications that generate plots or graphics, execute thexhost
program with the+
option prior to running your Docker image.
xhost +
xhost
program controls access to the X display server, thereby enabling plots and graphics to be displayed. The+
option indicates that everyone has access to the X display server. If you run thexhost
program with the+
option prior to running applications that do not generate plots or graphics, the messageNo protocol specified
is no longer displayed.
ShareDockerImage
You can share your Docker image in various ways.
Push your image to the Docker's 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 Docker's central registry or your private registry, consult the Docker documentation.
SaveDockerImage as Tar Archive
To save your Docker image as a tar archive, open a Linux terminal, navigate to the Docker context folder, and type the following.
$docker save mymagic-standalone-app -o mymagic-standalone-app.tar |
A file namedmymagic-standalone-app.tar
is created in your current folder. Set the appropriate permissions usingchmod
prior to sharing the tarball with other users.
LoadDockerImage from Tar Archive
Load the image contained in the tarball on the end-user's machine and then run it.
$docker load --input mymagic-standalone-app.tar |
Verify that the image is loaded.
$docker images |
RunDockerImage
$xhost +$docker run --rm -e "DISPLAY=:0" -v /tmp/.X11-unix:/tmp/.X11-unix mymagic-standalone-app 5 |
See Also
compiler.package.docker
|compiler.package.DockerOptions
|compiler.build.standaloneApplication
|compiler.runtime.download
Related Topics
- Create Microservice Docker Image(MATLAB Compiler SDK)