A new feature in MATLAB Compiler™ R2020b lets you package your standalone applications into Docker container images. This allows your applications to be easily distributed and shared throughout your organization as well as in the cloud. The Docker container is completely self-contained and consists of operating system libraries, a MATLAB® Runtime optimized to run your application, and the application itself.
I want to tell you about a new feature in MATLAB compiler R2020B-- the ability to package your standalone applications as Docker container images. Now, the altering of the packaging must be performed from a Linux environment. However, the resulting Docker container can be run in Linux, Windows, or the Mac.
Note that UI based applications require an X-windows server, which is only natively supported on Linux. Some people have been successful using options like VcXsrv or Cygwin on Windows, as well as XQuartz on Mac OS. But these options are not fully tested and qualified at MathWorks. So your mileage may vary.
How does this all work? Well, a MATLAB user packages their standalone application using MATLAB compiler, and targets a Docker container image. Now this Docker container image that is created contains several layers. The first layer contains the operating system-- in this case, Linux libraries-- followed by a layer with the optimized MATLAB Runtime containing just the components and tool boxes that you have used in your code.
And then finally, there is an application executable layer. This topic container image is saved onto your local repository in your PC. And at some point, this container may be passed on to your IT department, which posts it into a corporate registry. The users in your organization can browse this registry. And at some point in time, may decide to run it using the Docker run command.
There are many benefits to packaging and distributing your applications as Docker containers. It is a consistent environment with all the dependencies pre-installed in it. And it's very easily distributed through the registry and very easily executed. Let's see how we can create a Docker container image using MATLAB compiler.
The first thing we need to do is to make sure that the MATLAB Runtime has been downloaded. And we can do so using the compiler dot runtime dot download command. If you have already downloaded the MATLAB Runtime, you will not have to perform this task again a second time.
We're going to take our magic square function here and generate a Docker container image. This is a very simple function that takes a single numeric input and returns a magic square. The first step we need to do is to compile this into a standalone application. And we do so using the compiler dot bill dot standalone application command, passing in the name of our M file and telling it to treat the inputs as numeric.
This standalone application is saved into the magic app variable. Once we have that, we can then call the compiler dot package dot Docker command, and pass in the name of our newly generated standalone application, as well as telling it to name the Docker image magic dash hat. Take note that Docker image names need to be all lowercase.
Now, this process might take longer if you are doing this for the first time, because the packaging process needs to create the underlying operating system library layers, as well as the underlying runtime layers. I have previously generated these, so my process took a lot quicker. Once this process finishes running, you will be provided with the Docker run command needed to execute your application.
Copy this into your clipboard. Note that you have to run x host plus if you want to run MATLAB app designer UI applications. Let's switch over to the terminal window to run our newly generated magic square container. We will first enter the x host plus command, followed by pasting in the Docker run command that we copied from MATLAB compiler.
Don't forget to provide the input parameter-- in this case 5. The Docker container runs and outputs the 5 by 5 magic square. Now, you might be wondering if you can run MATLAB App Designer UI apps. And the answer is yes. I'll run a container I packaged earlier with a UI app.
This is an application that runs a Simulink model of a mass spring damper generated with the help of Simulink compiler. As you can see, it runs just like a typical, standalone UI app. If you want to learn more about how to use Docker containers, please visit the Help pages found in the MATLAB compiler documentation.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .Select web site
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.