Skip to content

Commit

Permalink
Adds an example showcasing a two-stage mpm download-then-install work…
Browse files Browse the repository at this point in the history
…flow via two new Dockerfiles, suitable for "air-gapped" networks.

fixes #105
  • Loading branch information
epaganon authored and ggriffithsuk committed Aug 5, 2024
1 parent e8656e9 commit 2706bd3
Show file tree
Hide file tree
Showing 18 changed files with 570 additions and 23 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/README-from-matlab-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The workflow in this folder builds and tests the Dockerfile found in `alternates

The workflow is scheduled to run every Monday at 00:00.

Additionally, the workflow is triggered each time you push a change in the [`Dockerfile`](../../alternates/building-on-matlab-docker-image/Dockerfile) or in the [`tests` directory](../../tests/alternates/building-on-matlab-docker-image) to the repository.
Additionally, the workflow is triggered each time you push a change in the [`Dockerfile`](../../alternates/building-on-matlab-docker-image/Dockerfile) or in the [`tests` directory](../../tests) to the repository.

You can also trigger the workflow from the "Actions" tab.

Expand All @@ -17,13 +17,12 @@ You can also trigger the workflow from the "Actions" tab.
This workflow consists of the following steps:

1. Check-out the repository into a GitHub Actions runner.
2. Install Python and the PyPi packages listed in [`requirements.txt`](../../tests/requirements.txt).
3. Run the [test file](../../tests/alternates/building-on-matlab-docker-image/test_build.py) to check the messages displayed during the build phase.
4. Build the image from the Dockerfile.
5. Run the other test files stored in [tests/alternates/matlab-installer](../../tests/alternates/matlab-installer).
2. Build the image from the Dockerfile.
3. Install Python and the PyPi packages listed in [`requirements.txt`](../../tests/requirements.txt).
4. Run the test files stored in [tests/alternates/building-on-matlab-docker-image](../../tests/alternates/building-on-matlab-docker-image).

---

Copyright 2023 The MathWorks, Inc. All rights reserved.
Copyright 2023-2024 The MathWorks, Inc. All rights reserved.

---
29 changes: 29 additions & 0 deletions .github/workflows/README-matlab-container-offline-install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build and Test the alternates/matlab-container-offline-install/Dockerfile

> This folder is intended only for the administrators of the matlab-dockerfile repository.
The workflow in this folder builds and tests the Dockerfiles found in `alternates/matlab-container-offline-install`.

## Triggers and Scheduled Jobs

The workflow is scheduled to run every Monday at 00:00.

Additionally, the workflow is triggered each time you push a change in the [`archive.Dockerfile`](../../alternates/matlab-container-offline-install/archive.Dockerfile), the [`Dockerfile`](../../alternates/matlab-container-offline-install/Dockerfile) or in the [`tests` directory](../../tests/) to the repository.

You can also trigger the workflow from the "Actions" tab.

## Workflow Description

This workflow consists of the following steps:

1. Check-out the repository into a GitHub Actions runner.
2. Build the archive image.
3. Build the product image, in an offline environment.
4. Install Python and the PyPi packages listed in [`requirements.txt`](../../tests/requirements.txt).
5. Run the test files stored in [tests/alternates/matlab-container-offline-install](../../tests/alternates/matlab-container-offline-install).

---

Copyright 2024 The MathWorks, Inc. All rights reserved.

---
4 changes: 2 additions & 2 deletions .github/workflows/README-matlab-installer.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The workflow in this folder builds and tests the Dockerfile found in `alternates

The workflow is scheduled to run every Monday at 00:00.

Additionally, the workflow is triggered each time you push a change in the [`Dockerfile`](../../alternates/matlab-installer/Dockerfile) or in the [`tests` directory](../../tests/alternates/matlab-installer) to the repository.
Additionally, the workflow is triggered each time you push a change in the [`Dockerfile`](../../alternates/matlab-installer/Dockerfile) or in the [`tests` directory](../../tests) to the repository.

You can also trigger the workflow from the "Actions" tab.

Expand All @@ -25,6 +25,6 @@ This workflow consists of the following steps:

---

Copyright 2023 The MathWorks, Inc. All rights reserved.
Copyright 2023-2024 The MathWorks, Inc. All rights reserved.

---
5 changes: 3 additions & 2 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ on:
# Trigger the workflow is the Dockerfile or any file under tests/ is modified
paths:
- "Dockerfile"
- "tests/**"
- "!tests/**.md"
- "tests/*"
- "tests/utils/**"
- "!**.md"
schedule:
# Run at 00:00 on every Monday (1st Day of the Week)
- cron: "0 0 * * 1"
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/from-matlab-docker-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ on:
# Trigger the workflow when the Dockerfile or any file under tests/ is modified
paths:
- "alternates/building-on-matlab-docker-image/Dockerfile"
- "tests/**"
- "!tests/**.md"
- "tests/alternates/building-on-matlab-docker-image/*"
- "tests/utils/**"
- "!**.md"
schedule:
# Run at 00:00 on every Monday (1st Day of the Week)
- cron: "0 0 * * 1"
Expand Down
92 changes: 92 additions & 0 deletions .github/workflows/matlab-container-offline-install-build-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright 2024 The MathWorks, Inc.

name: Build and Test the "MATLAB Container Offline Install" Dockerfiles

# Trigger this workflow either manually or when a new change is pushed to the
# repo (except .md files)
on:
workflow_dispatch:
push:
# Trigger the workflow when the Dockerfile or any file under tests/ is modified
paths:
- "alternates/matlab-container-offline-install/archive.Dockerfile"
- "alternates/matlab-container-offline-install/Dockerfile"
- "tests/alternates/matlab-container-offline-install/*"
- "tests/utils/**"
- "!**.md"
schedule:
# Run at 00:00 on every Monday (1st Day of the Week)
- cron: "0 0 * * 1"

env:
ARCHIVE_BASE_NAME: mpm-archive
IMAGE_BASE_NAME: matlab
ALT_PATH: alternates/matlab-container-offline-install
MATLAB_PRODUCT_LIST: "MATLAB Symbolic_Math_Toolbox Deep_Learning_Toolbox_Model_for_ResNet-50_Network"
LICENSE_FILE_PATH: ${{ github.workspace }}/license.lic

jobs:
build-test-image:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
matlab-release: [r2024a, r2023b, r2023a, r2022b, r2022a, r2021b, r2021a, r2020b]

steps:
- name: Checkout repo
uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
driver: docker

- name: Build archive image
uses: docker/build-push-action@v5
with:
platforms: linux/amd64
context: ${{ env.ALT_PATH }}
file: ${{ env.ALT_PATH }}/archive.Dockerfile
load: true
build-args: |
MATLAB_RELEASE=${{ matrix.matlab-release }}
MATLAB_PRODUCT_LIST=${{ env.MATLAB_PRODUCT_LIST }}
tags: |
${{ env.ARCHIVE_BASE_NAME }}:${{ matrix.matlab-release }}
- name: Build final image offline
uses: docker/build-push-action@v5
with:
platforms: linux/amd64
context: ${{ env.ALT_PATH }}
load: true
network: none
build-args: |
MATLAB_RELEASE=${{ matrix.matlab-release }}
MATLAB_PRODUCT_LIST=${{ env.MATLAB_PRODUCT_LIST }}
ARCHIVE_BASE_IMAGE=${{ env.ARCHIVE_BASE_NAME }}:${{ matrix.matlab-release }}
tags: |
${{ env.IMAGE_BASE_NAME }}:${{ matrix.matlab-release }}
- name: Set up Python 3
uses: actions/setup-python@v5
with:
python-version: "3.10"

- name: Install test dependencies
run: |
python -m pip install --upgrade pip
if [ -f tests/requirements.txt ]; then pip install -r tests/requirements.txt; fi
- name: Generate license file
run: echo '${{ secrets.MATLAB_LICENSE_FILE_R2024A }}' > ${{ env.LICENSE_FILE_PATH }}

- name: Test container
working-directory: tests
env:
IMAGE_NAME: ${{ env.IMAGE_BASE_NAME }}:${{ matrix.matlab-release }}
run: |
python -m unittest ${{ env.ALT_PATH }}/test_container.py
python -m unittest ${{ env.ALT_PATH }}/test_installed_products.py
5 changes: 3 additions & 2 deletions .github/workflows/matlab-installer-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ on:
# Trigger the workflow when the Dockerfile or any file under tests/ is modified
paths:
- "alternates/matlab-installer/Dockerfile"
- "tests/**"
- "!tests/**.md"
- "tests/alternates/matlab-installer/*"
- "tests/utils/**"
- "!**.md"
schedule:
# Run at 00:00 on every Monday (1st Day of the Week)
- cron: "0 0 * * 1"
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/non-interactive-build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ on:
# Trigger the workflow when the Dockerfile or any file under tests/ is modified
paths:
- "alternates/non-interactive/Dockerfile"
- "tests/**"
- "!tests/**.md"
- "tests/alternates/non-interactive/*"
- "tests/utils/**"
- "!**.md"
schedule:
# Run at 00:00 on every Monday (1st Day of the Week)
- cron: "0 0 * * 1"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ RUN wget -q https://www.mathworks.com/mpm/glnxa64/mpm \
--destination=${MATLAB_INSTALL_LOCATION} \
--products ${MATLAB_PRODUCT_LIST} \
|| (echo "MPM Installation Failure. See below for more information:" && cat /tmp/mathworks_root.log && false) \
&& sudo rm -rf mpm /tmp/mathworks_root.log ${HOME}/.MathWorks \
&& sudo rm -rf mpm /tmp/mathworks_root.log \
&& sudo ln -s ${MATLAB_INSTALL_LOCATION}/bin/matlab /usr/local/bin/matlab

# Note: Uncomment one of the following two ways to configure the license server.
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ This repository shows you how to build and customize a Docker® container for

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](https://github.com/mathworks-ref-arch/matlab-dockerfile/pkgs/container/matlab-dockerfile%2Fmatlab).
You can also download prebuilt images based on this Dockerfile from [here](https://github.com/mathworks-ref-arch/matlab-dockerfile/pkgs/container/matlab-dockerfile%2Fmatlab). For alternative resources, see [More MATLAB Docker Resources](#more-matlab-docker-resources).

### Requirements
* [A Running Network License Manager for MATLAB](https://www.mathworks.com/help/install/administer-network-licenses.html)
* For more information, see [Using the Network License Manager](#use-the-network-license-manager)
* For more information, see [Using the Network License Manager](#use-the-network-license-manager)
* Docker

## Build Instructions
Expand Down Expand Up @@ -60,7 +60,7 @@ The [Dockerfile](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/ma
| [MATLAB_RELEASE](#build-an-image-for-a-different-release-of-matlab) | r2024a | The MATLAB release you want to install, in lower-case. For example: `r2019b`|
| [MATLAB_PRODUCT_LIST](#build-an-image-with-a-specific-set-of-products) | MATLAB | Products to install as a space-separated list. For more information, see [MPM.md](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/MPM.md). For example: `MATLAB Simulink Deep_Learning_Toolbox Fixed-Point_Designer`|
| [MATLAB_INSTALL_LOCATION](#build-an-image-with-matlab-installed-to-a-specific-location) | /opt/matlab/r2024a | The path to install MATLAB. |
| [LICENSE_SERVER](#build-an-image-with-license-server-information) | *unset* | The port and hostname of the machine that is running the Network License Manager, using the `port@hostname` syntax. For example: `27000@MyServerName` |
| [LICENSE_SERVER](#build-an-image-configured-to-use-a-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](https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/Dockerfile).
Expand All @@ -83,7 +83,7 @@ For example, to build an image with MATLAB installed at /opt/matlab, use this co
docker build --build-arg MATLAB_INSTALL_LOCATION='/opt/matlab' -t matlab:r2024a .
```

#### Build an Image with License Server Information
#### Build an Image Configured to Use a License Server

Including the license server information with the `docker build` command means you do not have to pass it when running the container.
```bash
Expand Down Expand Up @@ -182,6 +182,7 @@ see the examples in the [**alternates folder**](alternates) of this repository:
* [matlab-installer](alternates/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](alternates/building-on-matlab-docker-image) is an example of a Dockerfile that builds on top of the [MATLAB Container Image on Docker Hub](https://hub.docker.com/r/mathworks/matlab), to install extra toolboxes.
* [non-interactive](alternates/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.
* [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.
* Enable additional capabilities using the [MATLAB Dependencies repository](https://github.com/mathworks-ref-arch/container-images/tree/master/matlab-deps).
For some workflows and toolboxes, you must specify dependencies. You must do this if you want to do any of the following tasks:
Expand Down
2 changes: 1 addition & 1 deletion alternates/building-on-matlab-docker-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ RUN wget -q https://www.mathworks.com/mpm/glnxa64/mpm \
--release=${MATLAB_RELEASE} \
--products ${ADDITIONAL_PRODUCTS} \
|| (echo "MPM Installation Failure. See below for more information:" && cat /tmp/mathworks_root.log && false) \
&& sudo rm -rf mpm /tmp/mathworks_root.log ${HOME}/.MathWorks
&& sudo rm -rf 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 Down
84 changes: 84 additions & 0 deletions alternates/matlab-container-offline-install/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2024 The MathWorks, Inc.
# This Dockerfile allows you to build a Docker® image with MATLAB® installed using the MATLAB Package
# Manager. Use the optional build arguments to customize the version of MATLAB, list of products to
# install, and the location at which to install MATLAB.

# Here is an example docker build command with the optional build arguments.
# docker build --build-arg MATLAB_RELEASE=r2024a
# --build-arg MATLAB_PRODUCT_LIST="MATLAB Deep_Learning_Toolbox Symbolic_Math_Toolbox"
# --build-arg MATLAB_INSTALL_LOCATION="/opt/matlab/R2024a"
# -t my_matlab_image_name .

# To specify which MATLAB release to install in the container, edit the value of the MATLAB_RELEASE argument.
# Use lowercase to specify the release, for example: ARG MATLAB_RELEASE=r2021b
ARG MATLAB_RELEASE=r2024a

# Specify the list of products to install into MATLAB.
ARG MATLAB_PRODUCT_LIST="MATLAB"

# Specify the MATLAB Install Location.
ARG MATLAB_INSTALL_LOCATION="/opt/matlab/${MATLAB_RELEASE}"

# Specify the archive image containing the installation files
ARG ARCHIVE_BASE_IMAGE="mpm-archive:${MATLAB_RELEASE}"

# Specify license server information using the format: port@hostname
ARG LICENSE_SERVER

# Specify the archive base image as a stage to allow it to be mounted in a later stage
FROM ${ARCHIVE_BASE_IMAGE} AS archive

# When you start the build stage, this Dockerfile by default uses the Ubuntu-based matlab-deps image.
# To check the available matlab-deps images, see: https://hub.docker.com/r/mathworks/matlab-deps
FROM mathworks/matlab-deps:${MATLAB_RELEASE}

# Declare build arguments to use at the current build stage.
ARG MATLAB_PRODUCT_LIST
ARG MATLAB_INSTALL_LOCATION
ARG LICENSE_SERVER

# Add "matlab" user and grant sudo permission.
RUN adduser --shell /bin/bash --disabled-password --gecos "" matlab \
&& echo "matlab ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/matlab \
&& chmod 0440 /etc/sudoers.d/matlab

# Set user and work directory.
USER matlab
WORKDIR /home/matlab

# Run mpm to install MATLAB in the target location and delete the mpm installation afterwards.
# If mpm fails to install successfully, then print the logfile in the terminal, otherwise clean up.
# Pass in $HOME variable to install support packages into the user's HOME folder.
RUN --mount=type=bind,from=archive,source=/,target=/mpm-download/ \
sudo HOME=${HOME} /mpm-download/mpm install \
--source=/mpm-download/archives \
--destination=${MATLAB_INSTALL_LOCATION} \
${MATLAB_PRODUCT_LIST} \
|| (echo "MPM Installation Failure. See below for more information:" && cat /tmp/mathworks_root.log && false) \
&& sudo rm -rf /tmp/mathworks_root.log \
&& sudo ln -s ${MATLAB_INSTALL_LOCATION}/bin/matlab /usr/local/bin/matlab


# Note: Uncomment one of the following two ways to configure the license server.

# Option 1. Specify the host and port of the machine that serves the network licenses
# if you want to store the license information in an environment variable. This
# is the preferred option. You can either use a build variable, like this:
# --build-arg LICENSE_SERVER=27000@MyServerName or you can specify the license server
# directly using: ENV MLM_LICENSE_FILE=27000@flexlm-server-name
ENV MLM_LICENSE_FILE=$LICENSE_SERVER

# Option 2. Alternatively, you can put a license file into the container.
# Enter the details of the license server in this file and uncomment the following line.
# COPY network.lic ${MATLAB_INSTALL_LOCATION}/licenses/

# The following environment variables allow MathWorks to understand how this MathWorks
# product is being used. This information helps us make MATLAB even better.
# Your content, and information about the content within your files, is not shared with MathWorks.
# To opt out of this service, delete the environment variables defined in the following line.
# See the Help Make MATLAB Even Better section in the accompanying README to learn more:
# https://github.com/mathworks-ref-arch/matlab-dockerfile/alternates/matlab-container-offline-install#help-make-matlab-even-better
ENV MW_DDUX_FORCE_ENABLE=true MW_CONTEXT_TAGS=$MW_CONTEXT_TAGS,MATLAB:FROM_SOURCE:DOCKERFILE:V1

ENTRYPOINT ["matlab"]
CMD [""]
Loading

0 comments on commit 2706bd3

Please sign in to comment.