Main Content

Add Software Dependencies to MATLAB Image

By default, the MATLAB® Docker® image does not contain the software dependencies required for certain MATLAB workflows, such as a compiler for code generation. You can add such dependencies by customizing the MATLAB Docker image.

Prerequisites

  • You installed MATLAB Online Server™ and loaded the default MATLAB image from the MathWorks® container registry.

  • You have a supported container management tool installed on your machine. For more details, see MATLAB Online Server System Requirements.

  • Familiarity with Dockerfiles is helpful but not required. For more details, see Dockerfile reference.

Note

  • These instructions prefix some commands with sudo. Depending on how your environment is configured, sudo administration privileges might not be required in all cases.

  • These instructions assume you are using Docker as your container management tool. If you are using Podman instead, replace docker with podman in the commands shown.

Prepare MATLAB Image

  1. Navigate to your MATLAB Online Server folder. For example:

    cd ~/matlab_online_server
  2. List the Docker images loaded into your MATLAB Online Server installation.

    sudo ./mosadm list-docker-images

    Verify that the list includes these images:

    • The base image containing the libraries required to run MATLAB, such as the UI display and window manager. The tag you see after the colon (:) might vary.

      containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic
    • The image containing MATLAB itself. This image is a child of the base MATLAB image. The tag you see might vary.

      containers.mathworks.com/matlab-online-server/mos-matlab-image:1.13.0

    If you do not see these images listed, reload the Docker images that ship with the MATLAB Online Server installer.

    sudo ./mosadm load-docker-images
  3. Create a backup of the base image using this Docker command. Run this command as a single line. If necessary, update the tag name to the one you see.

    sudo docker tag containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic
      containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic-backup

Create Dockerfile Template

A Dockerfile is a text file containing commands that Docker executes prior to building the image. In this file, you can specify commands that build all the necessary dependencies for the MATLAB image.

  1. In your matlab_online_server folder, create a folder named docker and navigate into it.

    mkdir docker && cd docker
  2. Create a file named Dockerfile (no extension) and open it for editing.

    touch Dockerfile && nano Dockerfile
  3. Create a template Dockerfile by copying these lines into the file.

    FROM containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic
    USER root
    
    # START IMAGE CUSTOMIZATION
    
    
    
    # END IMAGE CUSTOMIZATION
    
    USER matlab
    WORKDIR /home
    CMD ["/usr/local/bin/startup.sh"]
    

    This table describes the actions that these commands perform.

    CommandDescription
    FROM mathworks/...image:1.13.0-bionic

    Specifies that you are building from the MATLAB image. Docker applies the customization commands to this image.

    USER root

    Temporarily enables root administration privileges so that Docker can execute the software dependency commands.

    Depending on your environment configuration and the commands specified, these privileges might not be necessary.

    After executing the Dockerfile, Docker disables these privileges.

    USER matlab
    WORKDIR /home
    CMD ["/usr/local/bin/startup.sh"]
    

    Enables the Docker image to build inside MATLAB Online Server. These lines must be at the end of the Dockerfile.

Add Software Dependency Commands

In the body of the Dockerfile, write the commands that add the software dependencies. These samples Dockerfiles show dependencies you can add for common MATLAB workflows. They are written for the Ubuntu® operating system, but you can perform similar commands in other Linux® operating systems.

Install Required Software Package

In this file, replace package with the name of the software package you want to install (for example, curl).

FROM containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic

USER root

# START IMAGE CUSTOMIZATION

RUN apt-get update && apt-get install -y package

# END IMAGE CUSTOMIZATION

USER matlab
WORKDIR /home
CMD ["/usr/local/bin/startup.sh"]

Install GCC Compiler to Enable MEX Workflows

These commands copy the gcc binaries onto your machine, set the necessary environment variables, and install the gcc compiler.

FROM containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic

USER root

# START IMAGE CUSTOMIZATION

ENV GCC_VERSION gcc-6.3.0
 
# Copy gcc-6.3 binaries from the gcc:6.3 image
COPY --from=gcc:6.3 /usr/local/bin /usr/bin/${GCC_VERSION}/bin
COPY --from=gcc:6.3 /usr/local/include /usr/bin/${GCC_VERSION}/include
COPY --from=gcc:6.3 /usr/local/lib/gcc /usr/bin/${GCC_VERSION}/lib/gcc
COPY --from=gcc:6.3 /usr/local/lib64 /usr/bin/${GCC_VERSION}/lib64
COPY --from=gcc:6.3 /usr/local/libexec /usr/bin/${GCC_VERSION}/libexec
COPY --from=gcc:6.3 /usr/local/share /usr/bin/${GCC_VERSION}/share
 
ENV GCC_HOME /usr/bin/${GCC_VERSION}/bin/
 
RUN update-alternatives --install /usr/bin/gcc gcc ${GCC_HOME}/gcc 90 \
    && update-alternatives --install /usr/bin/g++ g++ ${GCC_HOME}/g++ 90 \
    && update-alternatives --install /usr/bin/gfortran gfortran ${GCC_HOME}/gfortran 90

# END IMAGE CUSTOMIZATION

USER matlab
WORKDIR /home
CMD ["/usr/local/bin/startup.sh"]

Customize MATLAB and Java Options at Startup

These commands add scripts to the Docker image. Here, it is assumed that the MATLAB startup script (matlabrc.m) and Java options file (java.opts) are in the same folder as your Dockerfile.

FROM containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic

USER root

# START IMAGE CUSTOMIZATION

COPY matlabrc.m /MATLAB/toolbox/local/matlabrc.m
COPY java.opts /MATLAB/bin/glnxa64/java.opts

# END IMAGE CUSTOMIZATION

USER matlab
WORKDIR /home
CMD ["/usr/local/bin/startup.sh"]

Build MATLAB Image

After you update the Dockerfile with all required dependencies, build the base MATLAB image from the folder containing the Dockerfile. Make sure you have required disk space on the Docker data disk.

sudo docker build . --tag containers.mathworks.com/matlab-online-server/mos-matlab-omnibus-image:1.13.0-bionic

If you mounted MATLAB from an image available inside MATLAB Online Server (the default configuration), then you must also rebuild the MATLAB image. If you mounted MATLAB from an external source, skip this step. For information on configuring the MATLAB installation source, see Set MATLAB Installation Source. For example:

cd ..
sudo ./mosadm build-matlab-image /MATLAB

If you are using a remote Docker registry, push the customized images to your remote registry by using the mosadm push-docker-images command.

See Also

| |

Related Topics

External Websites