Developing in Matlab gives the developer access to high level high level mathematical programming environment. However integrating this code in modern software environment with multiple languages, frameworks and architectures creates enormous complexity. Docker simplifies and accelerates this workflow. Support for Docker was introduced in Matlab 2020b but it is also possible to compile Matlab and use it in a Docker container for earlier Matlab versions. In this post we will make a very simple 'Hello world' in Matlab application and compile it under Unix and run it in a Docker container. The example is made for Matlab 2019b but should work in any other Matlab version.

This post is the first part of the Matlab in Docker posts, in the next post we will make a Matlab application that runs in Docker and that is able to send a receive data to and from other services.

Prerequisites for this example are:

  1. Matlab and the Matlab Compiler for Unix
  2. Docker

Create the file hello.m with the following code:

function hello()

disp('Hello Matlab'); 

Code fragment 1: A simple Matlab application that prints “Hello Matlab” on the console.

This code can be compiled with the following command:

>> mcc -m -d sh_hello hello

Code fragment 2: Command to compile Matlab code in the file hello.m

Executing the mcc command in Code fragment 2 produces two files in the directory sh_hello:

  1. hello
    the compiled hello.m
  2. run_hello.sh
    a shell script to execute the hello application

Using the shell command in Code fragment 3 the hello application in the sh_hello directory can be executed.

> ./run_hello.sh

Code fragment 3: Command to execute the hello application.

A compiled application cannot be executed directly in Docker. The application must be packed in a Docker container. This packing consists of the configuration of a Dockerfile: a file that contains all the instructions to build a Docker image. An image typically consists of the operating system, utilities and all dependencies needed to run the application. Docker uses this image to automatically generate a container in which the application can be executed.

Code fragment 4 contains the Dockerfile to generate a Docker image with the Matlab hello world application.

FROM alpine:latest

 

LABEL Description="Matlab 2019b hello world"

 

COPY run_hello.sh .

COPY hello .

 

RUN apt-get update

RUN apt-get install -y libxt-dev

RUN chmod a+x /run_hello.sh

RUN chmod a+x /hello

 

CMD ["./run_hello.sh", "/opt/mcr/v97"]

Code fragment 4: Dockerfile for the execution of a compiled Matlab R2019b application in a Docker container.

This Dockerfile consists of the following parts:

  • The first line with the command FROM specifies which image is used as base for this image. This is often the desired operating system, for example: alpine, ubuntu or debian, seeDocker Hub.
  • Add metadata with the command LABEL. For example the version or information about the maintainer.
  • Using the COPY and RUN commands to copy required files to the image and execute shell commands, for example to set permissions. The command “apt-get install -y libxt-dev” copies a library in the image that is necessary for Matlab.
  • ENV: Define environment variables with this command, these variable can be used in the call to the Matlab shell script.
  • CMD: The command to execute to run the compiled Matlab application followed by the input arguments, in this case the path to the MCR for Matlab 2019b.

The Dockerfile can be used to build a container with the instruction in Code fragment 5. This command generates a container with the tag hello based on the Dockerfile in the current directory (indicated with the “.”)

> docker build --tag hello .

Code fragment 5: Command to build a Docker image.

The generated image can be used to build and run a container: 

> docker run hello

Code fragment 6: Command to execute a Docker container with tag hello.

Use the command in Code fragment 7 to get an overview of the available functions for Docker containers and docker images. 

> docker image --help

> docker container --help

Code fragment 7: Get help for Docker image and container methods.

One of the problems of using Matlab in a Docker container is that the complete MCR is part of the image and causes it to be several GB's big. This violates idea behind Docker containers, which should be small and fast. There are a few workarounds for this. One solution is to mount the MCR on the computer where Docker is running.

> docker run -v [path_to_mcr]

Code fragment 8: Mounting the MCR in a Docker image.

Note that starting at version 2020b Docker images can be generated with the Matlab compiler. As part of this new feature The Mathworks has optimized the required MCR in the Docker image and is much smaller.