Skip to content

Commit

Permalink
Adds SupportPackage installation to alternates/building-on-matlab-doc…
Browse files Browse the repository at this point in the history
…ker-image
  • Loading branch information
philipc-mw authored and Prabhakar Kumar committed Dec 6, 2023
1 parent 7eb81ed commit 45e71d1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 46 deletions.
22 changes: 12 additions & 10 deletions alternates/building-on-matlab-docker-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
# Use lower case to specify the release, for example: ARG MATLAB_RELEASE=r2021b
ARG MATLAB_RELEASE=r2023b

# Specify the extra toolboxes to install into the image.
ARG ADDITIONAL_PRODUCTS="Symbolic_Math_Toolbox Statistics_and_Machine_Learning_Toolbox"
# Specify the extra products to install into the image. These products can either be toolboxes or support packages.
ARG ADDITIONAL_PRODUCTS="Symbolic_Math_Toolbox Deep_Learning_Toolbox_Model_for_ResNet-50_Network"

# This Dockerfile builds on the Ubuntu-based mathworks/matlab image.
# To check the available matlab images, see: https://hub.docker.com/r/mathworks/matlab
FROM mathworks/matlab:$MATLAB_RELEASE

# By default, the MATLAB container runs as user "matlab". To modify the MATLAB installation in the image, switch to root.
USER root

# Declare the global argument to use at the current build stage
ARG MATLAB_RELEASE
ARG ADDITIONAL_PRODUCTS

# By default, the MATLAB container runs as user "matlab". To install mpm dependencies, switch to root.
USER root

# Install mpm dependencies
RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get update \
Expand All @@ -29,21 +29,25 @@ RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get autoremove \
&& rm -rf /var/lib/apt/lists/*

# Run mpm to install MathWorks products and toolboxes into the existing MATLAB installation directory,
# Run mpm to install MathWorks products into the existing MATLAB installation directory,
# and delete the mpm installation afterwards.
# Modify it by setting the ADDITIONAL_PRODUCTS defined above,
# e.g. ADDITIONAL_PRODUCTS="Statistics_and_Machine_Learning_Toolbox Parallel_Computing_Toolbox MATLAB_Coder".
# If mpm fails to install successfully then output the logfile to the terminal, otherwise cleanup.

# Switch to user matlab, and pass in $HOME variable to mpm,
# so that mpm can set the correct root folder for the support packages.
WORKDIR /tmp
USER matlab
RUN wget -q https://www.mathworks.com/mpm/glnxa64/mpm \
&& chmod +x mpm \
&& EXISTING_MATLAB_LOCATION=$(dirname $(dirname $(readlink -f $(which matlab)))) \
&& ./mpm install \
&& sudo HOME=${HOME} ./mpm install \
--destination=${EXISTING_MATLAB_LOCATION} \
--release=${MATLAB_RELEASE} \
--products ${ADDITIONAL_PRODUCTS} \
|| (echo "MPM Installation Failure. See below for more information:" && cat /tmp/mathworks_root.log && false) \
&& rm -f mpm /tmp/mathworks_root.log
&& sudo rm -f mpm /tmp/mathworks_root.log

# When running the container a license file can be mounted,
# or a license server can be provided as an environment variable.
Expand All @@ -68,7 +72,5 @@ RUN wget -q https://www.mathworks.com/mpm/glnxa64/mpm \
# https://github.com/mathworks-ref-arch/matlab-dockerfile#help-make-matlab-even-better
ENV MW_DDUX_FORCE_ENABLE=true MW_CONTEXT_TAGS=$MW_CONTEXT_TAGS,MATLAB:TOOLBOXES:DOCKERFILE:V1

# Now that the installation is complete, switch back to user "matlab"
USER matlab
WORKDIR /home/matlab
# Inherit ENTRYPOINT and CMD from base image.
27 changes: 15 additions & 12 deletions alternates/building-on-matlab-docker-image/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Building on MATLAB Docker image

The Dockerfile in this subfolder builds on the [MATLAB Container Image on Docker® Hub®](https://hub.docker.com/r/mathworks/matlab)
by installing MATLAB® toolboxes using [MATLAB Package Manager (*mpm*)](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md).
by installing MATLAB® toolboxes and support packages using [MATLAB Package Manager (*mpm*)](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md).

Use the Dockerfile as an example of how to build a custom image that contains the features of the MATLAB image on Docker Hub.
These features include accessing the dockerised MATLAB through a browser, batch mode, or an interactive command prompt.
Expand All @@ -25,35 +25,38 @@ cd matlab-dockerfile/alternates/building-on-matlab-docker-image
### Quick start
Build a container with a name and tag.
```bash
docker build -t matlab_with_toolboxes:r2023b .
docker build -t matlab_with_add_ons:r2023b .
```

You can then run the container with the "batch" option. Test the container by running an example MATLAB command such as `ver` to display the installed toolboxes.
```bash
docker run --init --rm -e MLM_LICENSE_FILE=27000@MyServerName matlab_with_toolboxes:r2023b -batch ver
docker run --init --rm -e MLM_LICENSE_FILE=27000@MyServerName matlab_with_add_ons:r2023b -batch ver
```
You can check the installed support packages using the MATLAB command `matlabshared.supportpkg.getInstalled`.

You can also run the container with the "browser" option to access MATLAB in a browser.
```bash
docker run --init --rm -it -p 8888:8888 matlab_with_toolboxes:r2023b -browser
docker run --init --rm -it -p 8888:8888 matlab_with_add_ons:r2023b -browser
```
For more information, see [Run the Container](#run-the-container).

## Customize the Image
### Customize Products to Install Using MATLAB Package Manager (mpm)
This Dockerfile installs any specified toolboxes or products
This Dockerfile installs any specified products
into the MATLAB installation on the MATLAB Docker Hub image.

To customize the build, either pass a list of toolboxes into the `ADDITIONAL_PRODUCTS`
To customize the build, either pass a list of products into the `ADDITIONAL_PRODUCTS`
argument when building the Docker image, or edit the default value of that argument in the Dockerfile.
The `ADDITIONAL_PRODUCTS` argument must be a space separated list surrounded by quotes.
By default, `ADDITIONAL_PRODUCTS` includes two example toolboxes, which you can replace.
By default, `ADDITIONAL_PRODUCTS` includes example products, which you can replace.
For example, to build an image containing MATLAB and the Deep Learning Toolbox:
```bash
docker build --build-arg ADDITIONAL_PRODUCTS="Deep_Learning_Toolbox" -t matlab_with_toolboxes:r2023b .
docker build --build-arg ADDITIONAL_PRODUCTS="Deep_Learning_Toolbox" -t matlab_with_add_ons:r2023b .
```

For a successful build, include at least one product.
`mpm` automatically installs any toolboxes and support packages
required by the products specified in `ADDITIONAL_PRODUCTS`.
For more information, see [MATLAB Package Manager (*mpm*)](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md).

You can modify the products argument of `mpm`, but not the destination folder default value, as this is
Expand All @@ -65,7 +68,7 @@ The [Dockerfile](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/ma
| Argument Name | Default value | Effect |
|---|---|---|
| [MATLAB_RELEASE](#build-an-image-for-a-different-release-of-matlab) | r2023b | The MATLAB release to install. Must be lower-case, for example: `r2020b`.|
| [ADDITIONAL_PRODUCTS](#customize-products-to-install-using-matlab-package-manager-mpm) | "Symbolic_Math_Toolbox Statistics_and_Machine_Learning_Toolbox" | A space separated list of toolboxes to install. |
| [ADDITIONAL_PRODUCTS](#customize-products-to-install-using-matlab-package-manager-mpm) | "Symbolic_Math_Toolbox Deep_Learning_Toolbox_Model_for_ResNet-50_Network" | A space separated list of toolboxes and support packages to install. For more details, see [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md)|
| [LICENSE_SERVER](#build-an-image-with-license-server-information) | *unset* | The port and hostname of a machine that is running a Network License Manager, using the `port@hostname` syntax. For example: `27000@MyServerName`. To use this build argument, the corresponding lines must be uncommented in the Dockerfile. |

Use these arguments with the `docker build` command to customize the image.
Expand All @@ -76,7 +79,7 @@ directly in the Dockerfile.

For example, to build an image for MATLAB R2020b, use the following command.
```bash
docker build --build-arg MATLAB_RELEASE=r2020b -t matlab_with_toolboxes:r2020b .
docker build --build-arg MATLAB_RELEASE=r2020b -t matlab_with_add_ons:r2020b .
```
For supported releases see [MATLAB Container Image on Docker Hub](https://hub.docker.com/r/mathworks/matlab).
### Build an Image with License Server Information
Expand All @@ -87,12 +90,12 @@ server or browser mode will not start successfully.

Build container with the License Server.
```bash
docker build -t matlab_with_toolboxes:r2023b --build-arg LICENSE_SERVER=27000@MyServerName .
docker build -t matlab_with_add_ons:r2023b --build-arg LICENSE_SERVER=27000@MyServerName .
```

Run the container, without needing to pass license information.
```bash
docker run --init matlab_with_toolboxes:r2023b -batch ver
docker run --init matlab_with_add_ons:r2023b -batch ver
```
## Run the Container
The Docker container you build using this Dockerfile inherits run options from its base image.
Expand Down
29 changes: 29 additions & 0 deletions tests/alternates/building-on-matlab-docker-image/productsTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
% Copyright 2023 The MathWorks, Inc.

classdef productsTest < matlab.unittest.TestCase


methods(Test)
function testInstalledToolboxesMatchExpected( testCase )
import matlab.unittest.constraints.IsSameSetAs

% names of the expected toolboxes
expectedTbxNames = [...
"Deep Learning Toolbox",...
"Symbolic Math Toolbox"...
];
installedTbxNames = matlab.addons.installedAddons().Name;
testCase.verifyThat(installedTbxNames, IsSameSetAs(expectedTbxNames));
end
function testInstalledSupportPackagesMatchExpected( testCase )
import matlab.unittest.constraints.IsSameSetAs

% names of the expected support packages
expectedSpkgNames = [...
"Deep Learning Toolbox Model for ResNet-50 Network"...
];
installedSpkgNames = matlabshared.supportpkg.getInstalled().Name;
testCase.verifyThat(installedSpkgNames, IsSameSetAs(expectedSpkgNames));
end
end
end
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# Copyright 2023 The MathWorks, Inc.

"""Test runner for the toolboxesTest.m test file"""
"""Test runner for the productsTest.m test file"""

from utils import dockertool
from pathlib import Path
import unittest


class TestInstalledToolboxes(unittest.TestCase):
"""A test runner class for the toolboxesTest.m test file"""
class TestInstalledProducts(unittest.TestCase):
"""A test runner class for the productsTest.m test file"""

def test_installed_toolboxes(self):
m_filename = "toolboxesTest.m"
def run_matlab_test(self, m_filename):
m_filepath = str(Path(__file__).parent.resolve() / m_filename)

runner = dockertool.MATLABTestRunner(m_filepath)
Expand All @@ -20,6 +19,9 @@ def test_installed_toolboxes(self):
self.assertEqual(exit_code, 0, logs)
self.assertIsNone(error, logs)

def test_installed_products(self):
m_filename = "productsTest.m"
self.run_matlab_test(m_filename)

if __name__ == "__main__":
unittest.main()
19 changes: 0 additions & 19 deletions tests/alternates/building-on-matlab-docker-image/toolboxesTest.m

This file was deleted.

0 comments on commit 45e71d1

Please sign in to comment.