This repository shows you how to build and customize a Docker® container for MATLAB® and its toolboxes, using the MATLAB Package Manager (mpm).
You can use this container image as a scalable and reproducible method to deploy and test your MATLAB code.
You can also download prebuilt images based on this Dockerfile from here.
- A Running Network License Manager for MATLAB
- For more information, see Using the Network License Manager
- Docker
Access this Dockerfile either by directly downloading this repository from GitHub®, or by cloning this repository and then navigating to the appropriate folder.
git clone https://github.com/mathworks-ref-arch/matlab-dockerfile.git
cd matlab-dockerfileBuild container with a name and tag of your choice.
docker build -t matlab:r2023b .Run the container. Test the container by running an example MATLAB command such as ver.
docker run --init --rm -e MLM_LICENSE_FILE=27000@MyServerName matlab:r2023b -batch verThe Dockerfile defaults to building a container for MATLAB R2023b.
The example command ver displays the version number of MATLAB and other installed products. For more information, see ver. For more information on running the container, see the section on Running the Container.
Note
Using the
--initflag in thedocker runcommand ensures that the container stops gracefully when adocker stopordocker killcommand is issued. For more information, see the following links:
By default, the Dockerfile installs MATLAB for the latest available MATLAB release without any additional toolboxes or products in the /opt/matlab/${MATLAB_RELEASE} folder.
Use the options below to customize your build.
The Dockerfile supports the following Docker build-time variables:
| Argument Name | Default value | Description |
|---|---|---|
| MATLAB_RELEASE | r2023b | The MATLAB release you want to install, in lower-case. For example: r2019b |
| MATLAB_PRODUCT_LIST | MATLAB | Products to install as a space-separated list. For more information, see MPM.md. For example: MATLAB Simulink Deep_Learning_Toolbox Fixed-Point_Designer |
| MATLAB_INSTALL_LOCATION | /opt/matlab/r2023b | The path to install MATLAB. |
| LICENSE_SERVER | unset | The port and hostname of the machine that is running the Network License Manager, using the port@hostname syntax. For example: 27000@MyServerName |
Use these arguments with the the docker build command to customize your image.
Alternatively, you can change the default values for these arguments directly in the Dockerfile.
For example, to build an image for MATLAB R2019b, use this command.
docker build --build-arg MATLAB_RELEASE=r2019b -t matlab:r2019b .For example, to build an image with MATLAB and Simulink®, use this command.
docker build --build-arg MATLAB_PRODUCT_LIST='MATLAB Simulink' -t matlab:r2023b .For example, to build an image with MATLAB installed at /opt/matlab, use this command.
docker build --build-arg MATLAB_INSTALL_LOCATION='/opt/matlab' -t matlab:r2023b .Including the license server information with the docker build command means you do not have to pass it when running the container.
# Build container with the License Server.
docker build --build-arg LICENSE_SERVER=27000@MyServerName -t matlab:r2023b .
# Run the container, without needing to pass license information.
docker run --init --rm matlab:r2023b -batch verThis container requires a Network License Manager to license and run MATLAB. You will need either the port and hostname of the Network License Manager, or a network.lic file.
Step 1: Contact your system administrator, who can provide one of the following:
-
The address to your server, and the port it is running on. For example:
27000@MyServerName.example.com -
A
network.licfile which contains the following lines:# Sample network.lic SERVER MyServerName.example.com <optional-mac-address> 27000 USE_SERVER
-
A
license.datfile. Open thelicense.datfile, find theSERVERline, and either extract theport@hostname, or create anetwork.licfile by copying theSERVERline and adding aUSE_SERVERline below it.# snippet from sample license.dat SERVER MyServerName.example.com <mac-address> 27000
Step 2: Use port@hostname or the network.lic file with either the docker build or the docker run command.
With the docker build command, either:
-
Specify the
LICENSE_SERVERbuild-arg.# Example docker build -t matlab:r2023b --build-arg LICENSE_SERVER=27000@MyServerName .
-
Use the
network.licfile:- Place the
network.licfile in the same folder as the Dockerfile. - Uncomment the line
COPY network.lic /opt/matlab/licenses/in the Dockerfile. - Run the docker build command without the
LICENSE_SERVERbuild-arg:
# Example docker build -t matlab:r2023b .
- Place the
With the docker run command, use the MLM_LICENSE_FILE environment variable. For example:
docker run --init --rm -e MLM_LICENSE_FILE=27000@MyServerName matlab:r2023b -batch verIf you did not provide the license server information when building the image, then provide it when running the container. Set the environment variable MLM_LICENSE_FILE using the -e flag, with the network license manager's location in the format port@hostname.
# Start MATLAB, print version information, and exit:
docker run --init --rm -e MLM_LICENSE_FILE=27000@MyServerName matlab:r2023b -batch verYou can run the container without specifying MLM_LICENSE_FILE if you provided the license server information when building the image, as shown in the examples below.
To start the container and run MATLAB in an interactive command prompt, execute:
docker run --init -it --rm matlab:r2023bTo start the container, run a MATLAB command, and then exit, execute:
# Container runs the command RAND in MATLAB and exits.
docker run --init --rm matlab:r2023b -batch randTo override the default behavior of the container and run MATLAB with any set of arguments, such as -logfile, execute:
docker run --init -it --rm matlab:r2023b -logfile "logfilename.log"To learn more, see the documentation: Commonly Used Startup Options.
-
Explore prebuilt MATLAB Docker Containers on Docker Hub: https://hub.docker.com/r/mathworks
- MATLAB Containers on Docker Hub hosts container images for multiple releases of MATLAB.
- MATLAB Deep Learning Containers on Docker Hub hosts container images with toolboxes suitable for Deep Learning.
-
This Dockerfile builds on the matlab-deps container image and installs MATLAB. For other possibilities, see the examples in the alternates folder of this repository:
- matlab-installer is an example of a Dockerfile that uses the matlab installer rather than mpm to install MATLAB in the container, allowing the installation of some toolboxes that are not currently supported by mpm.
- building-on-matlab-docker-image is an example of a Dockerfile that builds on top of the MATLAB Container Image on Docker Hub, to install extra toolboxes.
- non-interactive is an example of a Dockerfile that licenses MATLAB using MATLAB batch licensing tokens, facilitating the execution of MATLAB in non-interactive environments.
-
Enable additional capabilities using the MATLAB Dependencies repository. For some workflows and toolboxes, you must specify dependencies. You must do this if you want to do any of the following tasks:
- Install extended localization support for MATLAB.
- Play media files from MATLAB.
- Generate code from Simulink.
- Use mex functions with gcc, g++, or gfortran.
- Use the MATLAB Engine API for C and Fortran®.
- Use the Polyspace® 32-bit tcc compiler.
The matlab-deps repository repository lists Dockerfiles for various releases and platforms. To view the Dockerfile for R2023b, click here.
These Dockerfiles contain commented lines with the libraries that support these additional capabilities. Copy and uncomment these lines into your Dockerfile.
You can help improve MATLAB by providing user experience information on how you use MathWorks products. Your participation ensures that you are represented and helps us design better products. To opt out of this service, delete the following line in the Dockerfile:
ENV MW_DDUX_FORCE_ENABLE=true MW_CONTEXT_TAGS=MATLAB:DOCKERFILE:V1To learn more, see the documentation: Help Make MATLAB Even Better - Frequently Asked Questions.
We encourage you to try this repository with your environment and provide feedback. If you encounter a technical issue or have an enhancement request, create an issue here.
Copyright 2021-2024 The MathWorks, Inc.