Skip to content

Commit cc54447

Browse files
drobinson0michaelmcdonnellmw
authored andcommitted
Adds support for building Windows container images with MATLAB for non-interactive workflows
resolves #126
1 parent 534f700 commit cc54447

File tree

5 files changed

+242
-4
lines changed

5 files changed

+242
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
This repository shows you how to build and customize a Docker® container for MATLAB® and its toolboxes, using the [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md) (`mpm`). You can use this container image as a scalable and reproducible method to deploy and test your MATLAB code.
44

5-
Use the [Dockerfile](Dockerfile) in this top-level repository if you want a lightweight and simple way to create a MATLAB container image. You can also download prebuilt images based on the Dockerfile from [here](https://github.com/mathworks-ref-arch/matlab-dockerfile/pkgs/container/matlab-dockerfile%2Fmatlab).
6-
5+
Use the [Dockerfile](Dockerfile) in this top-level repository if you want a lightweight and simple way to create a MATLAB container image. You can also download prebuilt images based on the Dockerfile from [this GitHub® repository](https://github.com/mathworks-ref-arch/matlab-dockerfile/pkgs/container/matlab-dockerfile%2Fmatlab).
6+
7+
To build a Windows® container for MATLAB, see the [**windows folder**](windows). This container supports non-interactive MATLAB workflows only, such as running scripts, batch jobs, or automating tasks in continuous integration and continuous delivery (CI/CD) pipelines.
8+
79
For alternative resources, see the [**alternates folder**](alternates) that contains the following Dockerfiles:
810

911
* The Dockerfile in [matlab-installer](alternates/matlab-installer) uses the MATLAB installer rather than `mpm` to install MATLAB in the container. This allows you to install toolboxes that are not currently supported by mpm. Use this Dockerfile if you prefer using the MATLAB installer workflow, instead of `mpm`.
1012
* The Dockerfile in [building-on-matlab-docker-image](alternates/building-on-matlab-docker-image) shows you how to build on top of the [MATLAB Container Image on Docker Hub](https://hub.docker.com/r/mathworks/matlab). Use this Dockerfile if you want to install extra toolboxes on top of the `mathworks/matlab` container image. This Dockerfile contains the features of the MATLAB image on Docker Hub, allowing you to access the dockerised MATLAB through a browser, batch mode, or an interactive command prompt.
11-
* The Dockerfile in [non-interactive](alternates/non-interactive) allows you run MATLAB in non-interactive environments. This Dockerfile requires that you have a MATLAB batch licensing token to license MATLAB in the container. Use this Dockerfile in continuous integration and continuous delivery (CI/CD) pipelines or other automated environments where interactive licensing is not possible.
13+
* The Dockerfile in [non-interactive](alternates/non-interactive) allows you run MATLAB in non-interactive environments. This Dockerfile requires that you have a MATLAB batch licensing token to license MATLAB in the container. Use this Dockerfile in CI/CD pipelines or other automated environments where interactive licensing is not possible.
1214
* The Dockerfile in [matlab-container-offline-install](alternates/matlab-container-offline-install/) shows you how to build and customize a Docker container for MATLAB and its toolboxes in an offline environment. Use this Dockerfile if you must build your container image in an offline environment.
1315

1416
For more Docker related resources, see [More MATLAB Docker Resources](#more-matlab-docker-resources).
@@ -21,7 +23,7 @@ For more Docker related resources, see [More MATLAB Docker Resources](#more-matl
2123

2224
### Get Sources
2325

24-
Access the Dockerfile either by directly downloading this repository from GitHub®,
26+
Access the Dockerfile either by directly downloading this repository from GitHub,
2527
or by cloning this repository and
2628
then navigating to the appropriate folder.
2729
```bash

windows/Dockerfile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2025 The MathWorks, Inc.
2+
# This Dockerfile allows you to build a Windows® Docker® image with MATLAB® using the MATLAB Package Manager.
3+
# You can then use a MATLAB batch licensing token to license MATLAB in the container. Use the optional build arguments to
4+
# customize the version of MATLAB, and the list of products to install.
5+
6+
# Here is an example docker build command with the optional build arguments.
7+
# docker build --build-arg MATLAB_RELEASE=R2025a
8+
# --build-arg MATLAB_PRODUCT_LIST="MATLAB Deep_Learning_Toolbox Symbolic_Math_Toolbox"
9+
# -t my_matlab_image_name .
10+
11+
# To specify which MATLAB release to install in the container, edit the value of the MATLAB_RELEASE argument.
12+
# Use uppercase to specify the release, for example: ARG MATLAB_RELEASE=R2021b
13+
ARG MATLAB_RELEASE="R2025a"
14+
15+
# Specify the list of products to install into MATLAB.
16+
ARG MATLAB_PRODUCT_LIST="MATLAB"
17+
18+
# This Dockerfile uses the Windows base image which contains the full Windows API set. For details, see https://hub.docker.com/r/microsoft/windows
19+
FROM mcr.microsoft.com/windows:ltsc2019
20+
21+
ARG MATLAB_RELEASE
22+
ARG MATLAB_PRODUCT_LIST
23+
24+
# Set the shell to be powershell, and stop on errors
25+
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"]
26+
27+
# Run mpm to install MATLAB in the target location and delete the mpm installation afterwards.
28+
# If mpm fails to install successfully, then print the logfile in the terminal, otherwise clean up.
29+
RUN Invoke-WebRequest -OutFile "$Env:TEMP\mpm.exe" -Uri "https://www.mathworks.com/mpm/win64/mpm" ; \
30+
$PRODUCT_LIST = -split $Env:MATLAB_PRODUCT_LIST ; \
31+
& "$Env:TEMP\mpm.exe" install --release $Env:MATLAB_RELEASE --destination "C:\MATLAB" $PRODUCT_LIST ; \
32+
if ($LASTEXITCODE -ne 0) { \
33+
Get-Content "$Env:TEMP\mathworks_$Env:USERNAME.log"; throw \
34+
} else { \
35+
Remove-Item "$Env:TEMP\mpm.exe" ; Remove-Item "$Env:TEMP\mathworks_$Env:USERNAME.log" \
36+
}
37+
38+
# Install matlab-batch to enable the use of MATLAB batch licensing tokens.
39+
RUN Set-ExecutionPolicy RemoteSigned -Scope Process ; \
40+
Invoke-WebRequest -OutFile $Env:TEMP\install-matlab-batch.ps1 -Uri "https://raw.githubusercontent.com/mathworks-ref-arch/matlab-dockerfile/main/alternates/non-interactive/install/install-matlab-batch.ps1" ; \
41+
& $Env:TEMP\install-matlab-batch.ps1 ; \
42+
Remove-Item $Env:TEMP\install-matlab-batch.ps1
43+
44+
# The following environment variables allow MathWorks to understand how this MathWorks
45+
# product (MATLAB On Windows Dockerfile) is being used. This information helps us make MATLAB even better.
46+
# Your content, and information about the content within your files, is not shared with MathWorks.
47+
# To opt out of this service, delete the environment variables defined in the following line.
48+
# To learn more, see the Help Make MATLAB Even Better section in the accompanying README:
49+
# https://github.com/mathworks-ref-arch/matlab-dockerfile#help-make-matlab-even-better
50+
ENV MW_DDUX_FORCE_ENABLE=true MW_CONTEXT_TAGS=MATLAB:WINDOWSNONINTERACTIVE:DOCKERFILE:V1
51+
52+
# Set the entrypoint
53+
ENTRYPOINT ["powershell.exe"]

windows/Makefile.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2025 The MathWorks, Inc.
2+
# This script allows you to run build and test targets of a GitHub actions pipeline in an identical manner on your local machine.
3+
# This ensures that the build and test actions are easily reproducible outside of a CI/CD pipeline.
4+
param(
5+
[Parameter(Mandatory=$False, Position=1, ValueFromPipeline=$false)]
6+
[System.String]
7+
$Release = "R2025a",
8+
9+
[Parameter(Mandatory=$False, Position=1, ValueFromPipeline=$false)]
10+
[System.String]
11+
$ProductList = "MATLAB Text_Analytics_Toolbox Simulink_Coder Embedded_Coder MATLAB_Support_for_MinGW-w64_C/C++/Fortran_Compiler Requirements_Toolbox",
12+
13+
[Parameter(Mandatory=$False, Position=1, ValueFromPipeline=$false)]
14+
[System.String]
15+
$Token = "",
16+
17+
[Parameter(Mandatory=$False, Position=1, ValueFromPipeline=$false)]
18+
[System.String]
19+
$Target = "BuildAndTest"
20+
)
21+
22+
$script:ImageName="mathworks/matlab-on-windows"
23+
$script:Version="0.5.1"
24+
25+
function Build {
26+
$Tag = ${Release}.ToLower() + "_" + ${Version}
27+
& docker build --build-arg MATLAB_RELEASE=${Release} --build-arg MATLAB_PRODUCT_LIST="${ProductList}" -t "${ImageName}:${Tag}" .
28+
}
29+
30+
function Test {
31+
if (${Token} -eq "") {
32+
Write-Error "Token argument must be supplied to test command"
33+
throw "input error"
34+
}
35+
$Tag = ${Release}.ToLower() + "_" + ${Version}
36+
$container = New-PesterContainer -Path .\test\Test-MATLABWindowsContainer.tests.ps1 -Data @{ Release = ${Release}; ImageName = "${ImageName}:${Tag}"; Token = ${Token} }
37+
& Invoke-Pester -Container $container -Output Detailed
38+
}
39+
40+
function BuildAndTest {
41+
Build
42+
Test
43+
}
44+
45+
switch (${Target}) {
46+
'Build' { Build }
47+
'Test' { Test }
48+
'BuildAndTest' { BuildAndTest }
49+
default { BuildAndTest }
50+
}

windows/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Create a Windows Container Image for MATLAB
2+
3+
This repository shows you how to build and customize a Windows® Docker® container for MATLAB® and its toolboxes, using the [MATLAB Package Manager](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md) (`mpm`) and [MATLAB Batch Licensing (`matlab-batch`)](../alternates/non-interactive/MATLAB-BATCH.md). You can use this container image as a scalable and reproducible method to deploy and test your MATLAB code on a Windows platform.
4+
5+
Use this Dockerfile if you have a MATLAB batch licensing token to license MATLAB in your container. The MATLAB batch licensing project is still in the pilot phase. To inquire about eligibility requirements, fill out this form on the MathWorks® website: [Batch Licensing Pilot Eligibility](https://www.mathworks.com/support/batch-tokens.html).
6+
7+
### Requirements
8+
* MATLAB Batch Licensing Token. For more information, see [Use MATLAB Batch Licensing](../alternates/non-interactive/MATLAB-BATCH.md#matlab-batch-licensing-token).
9+
* Docker Desktop for Windows® (Windows 10 or later) or a Windows Server® build host (Windows Server 2016 or later).
10+
11+
## Build Instructions
12+
### Get the Dockerfile
13+
14+
Access this Dockerfile either by directly downloading this repository from GitHub®, or by cloning this repository and then navigating to the appropriate subfolder.
15+
```bash
16+
git clone https://github.com/mathworks-ref-arch/matlab-dockerfile.git
17+
cd matlab-dockerfile/windows/
18+
```
19+
20+
### Build and Run Docker Image
21+
22+
Build container with a name and tag of your choice.
23+
```powershell
24+
docker build -t matlab-on-windows:R2025a .
25+
```
26+
This [Dockerfile](./Dockerfile) defaults to building a Windows container for MATLAB R2025a. The Dockerfile is based on the Windows base image which contains the full Windows API set. For details, see the documentation on DockerHub for [Windows base image](https://hub.docker.com/r/microsoft/windows).
27+
28+
Test the container by running an example MATLAB command, such as `ver`. The entry point of the container is PowerShell.
29+
```powershell
30+
docker run --rm matlab-on-windows:R2025a matlab-batch "-licenseToken" "[email protected]::encodedToken" "ver"
31+
```
32+
For more information on running the container, see [Run the Container](#run-the-container).
33+
34+
## Customize the Image
35+
36+
By default, the [Dockerfile](Dockerfile) installs the latest available MATLAB release without any additional toolboxes or products, as well as the latest version of [**matlab-batch**](../alternates/non-interactive/MATLAB-BATCH.md).
37+
38+
Use the options below to customize your build.
39+
40+
### Customize MATLAB Release and MATLAB Product List
41+
The [Dockerfile](Dockerfile) supports these Docker build-time variables:
42+
43+
| Argument Name | Default Value | Description |
44+
|---|---|---|
45+
| [MATLAB_RELEASE](#build-an-image-for-a-different-release-of-matlab) | R2025a | MATLAB release to install, for example, `R2023b`. |
46+
| [MATLAB_PRODUCT_LIST](#build-an-image-with-a-specific-set-of-products) | MATLAB | Space-separated list of products to install. For help specifying the products to install, see the "products" input argument on the [documentation page for the mpm install function](https://www.mathworks.com/help/install/ug/mpminstall.html). For example: `MATLAB Simulink Deep_Learning_Toolbox Fixed-Point_Designer` |
47+
48+
Use these arguments with the `docker build` command to customize your image.
49+
Alternatively, the default values for these arguments can be changed directly in the [Dockerfile](Dockerfile).
50+
51+
> Note: MATLAB is installed to `C:\MATLAB` in the Windows container because the default `matlabroot` location can cause issues with compiler products. For details about the issues, see [Build Process Support for File and Folder Names](https://www.mathworks.com/help/coder/ug/enable-build-process-for-folder-names-with-spaces.html) and this [MATLAB Answer](https://www.mathworks.com/matlabcentral/answers/95399-why-is-the-build-process-failing-with-error-code-nmake-fatal-error-u1073-don-t-know-how-to-make). Additionally, you cannot use the suggested 8.3 short file names as a workaround because it is not supported in Windows containers. For details, see this [GitHub issue](https://github.com/microsoft/Windows-Containers/issues/507).
52+
53+
### Build an Image for a Different Release of MATLAB
54+
55+
For example, to build an image for MATLAB R2021b, use this command.
56+
```powershell
57+
docker build --build-arg MATLAB_RELEASE=R2021b -t matlab-windows:R2021b .
58+
```
59+
60+
For supported releases, see [MATLAB Batch Licensing support](../alternates/non-interactive/MATLAB-BATCH.md#limitations).
61+
62+
### Build an Image with a Specific Set of Products
63+
64+
For example, to build an image with MATLAB and the Statistics and Machine Learning Toolbox™, use this command.
65+
```powershell
66+
docker build --build-arg MATLAB_PRODUCT_LIST="MATLAB Statistics_and_Machine_Learning_Toolbox" -t matlab-stats-windows:R2025a .
67+
```
68+
69+
## Run the Container
70+
To start a container and run MATLAB with a MATLAB batch licensing token, open a Windows PowerShell and enter this command:
71+
```powershell
72+
# Start MATLAB, display 'hello world', and exit.
73+
docker run --rm matlab-on-windows:R2025a matlab-batch -licenseToken '[email protected]::encodedToken' '\"disp(''hello world'')\"'
74+
```
75+
Note that you must use the rules for using single and double quotation marks in PowerShell. For details, see [about quoting rules](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_quoting_rules).
76+
77+
Alternatively, you can run a script `myscript.m` containing MATLAB code:
78+
79+
```powershell
80+
# Launch MATLAB, run `myscript.m` and exit:
81+
docker run --mount "type=bind,src=C:\scripts,target=C:\pwd" --workdir "C:\pwd" --rm matlab-on-windows:R2025a matlab-batch -licenseToken '[email protected]::encodedToken' 'myscript'
82+
```
83+
84+
You can also set your MATLAB batch licensing token at the container level by setting the `MLM_LICENSE_TOKEN` environment variable. For example:
85+
86+
```powershell
87+
# Start MATLAB, display 'hello world', and exit.
88+
docker run -e MLM_LICENSE_TOKEN='[email protected]::encodedToken' --rm matlab-on-windows:R2025a matlab-batch '\"disp(''hello world'')\"'
89+
```
90+
91+
## Feedback
92+
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](https://github.com/mathworks-ref-arch/matlab-dockerfile/issues).
93+
94+
---
95+
Copyright 2025 The MathWorks, Inc.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2025 The MathWorks, Inc.
2+
# Requires v5.0 of pester
3+
param (
4+
[Parameter(Mandatory)]
5+
[string] $ImageName,
6+
7+
[Parameter(Mandatory)]
8+
[string] $Token,
9+
10+
[Parameter(Mandatory)]
11+
[string] $Release
12+
)
13+
14+
Describe "MATLAB Docker Container Tests" {
15+
It "Runs rand() and returns expected output" {
16+
$result = docker run -e "MLM_LICENSE_TOKEN=$Token" --rm "$ImageName" "matlab-batch.exe" "'rand()'"
17+
$LASTEXITCODE | Should -Be 0
18+
$result -join "`n" | Should -Match "0.8147"
19+
}
20+
21+
It "Runs ver and includes release string" {
22+
$result = docker run -e "MLM_LICENSE_TOKEN=$Token" --rm "$ImageName" "matlab-batch.exe" "'ver'"
23+
$LASTEXITCODE | Should -Be 0
24+
$result -join "`n" | Should -Match $Release
25+
}
26+
27+
It "Runs peaks and returns expected expression" {
28+
$result = docker run -e "MLM_LICENSE_TOKEN=$Token" --rm "$ImageName" "matlab-batch.exe" "'peaks'"
29+
$LASTEXITCODE | Should -Be 0
30+
$result -join "`n" | Should -Match "z = 3\*\(1-x\)\.\^2\.\*exp\(-\(x\.\^2\) - \(y\+1\)\.\^2\)"
31+
}
32+
33+
It "Creates a plot and confirms file existence" {
34+
$result = docker run -e "MLM_LICENSE_TOKEN=$Token" --rm "$ImageName" "matlab-batch.exe" '\"figure; annotation(''ellipse'', [.2 .2 .2 .2]); saveas(gcf, ''plot.png''); isfile(''plot.png'')\"'
35+
$LASTEXITCODE | Should -Be 0
36+
$result -join "`n" | Should -BeLike "*ans =*logical*1*"
37+
}
38+
}

0 commit comments

Comments
 (0)