Skip to content

Commit a0e3910

Browse files
author
Jos Martin
committed
Merge branch 'main' into 'main'
Auto-Locate MATLAB from base image & minor cleanup to increase uniformity See merge request cit/mathworks-ref-arch/matlab-integration-for-jupyter!1
2 parents a649591 + 2a925c7 commit a0e3910

File tree

7 files changed

+68
-30
lines changed

7 files changed

+68
-30
lines changed

matlab-vnc/Dockerfile

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
# Copyright 2020 The MathWorks, Inc.
22

3-
# Replace "matlab:r2020b" with the Docker image that contains MATLAB
4-
# MATLAB should be installed at /usr/local/MATLAB or other changes will need
5-
# to be made to this Dockerfile
6-
FROM matlab:r2020b AS matlab-install-stage
3+
# Argument shared across multi-stage build to hold location of installed MATLAB
4+
ARG BASE_ML_INSTALL_LOC=/tmp/matlab-install-location
5+
6+
# Replace "matlab" with the Docker image that contains MATLAB
7+
# MATLAB should be available on the path in the Docker image
8+
FROM matlab AS matlab-install-stage
9+
ARG BASE_ML_INSTALL_LOC
10+
11+
# Run code to locate a MATLAB install in the base image and softlink
12+
# to BASE_ML_INSTALL_LOC for a latter stage to copy
13+
RUN export ML_INSTALL_LOC=$(which matlab) \
14+
&& if [ ! -z "$ML_INSTALL_LOC" ]; then \
15+
ML_INSTALL_LOC=$(dirname $(dirname $(readlink -f ${ML_INSTALL_LOC}))); \
16+
echo "soft linking: " $ML_INSTALL_LOC " to" ${BASE_ML_INSTALL_LOC}; \
17+
ln -s ${ML_INSTALL_LOC} ${BASE_ML_INSTALL_LOC}; \
18+
elif [ $BASE_ML_INSTALL_LOC = '/tmp/matlab-install-location' ]; then \
19+
echo "MATLAB was not found in your image."; exit 1; \
20+
else \
21+
echo "Proceeding with user provided path to MATLAB installation: ${BASE_ML_INSTALL_LOC}"; \
22+
fi
723

824
FROM jupyter/base-notebook
25+
ARG BASE_ML_INSTALL_LOC
926

27+
# Switch to root user
1028
USER root
1129

1230
# Copy MATLAB install from Docker image
13-
COPY --from=matlab-install-stage /usr/local/MATLAB /usr/local/MATLAB
31+
COPY --from=matlab-install-stage ${BASE_ML_INSTALL_LOC} /usr/local/MATLAB
1432

1533
# Add MATLAB to the path
1634
RUN ln -s /usr/local/MATLAB/bin/matlab /usr/local/bin/matlab

matlab-vnc/Dockerfile.mounted

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
FROM jupyter/base-notebook
44

5+
# Switch to root user
56
USER root
67

7-
# Add MATLAB to the path
8+
# Put MATLAB on the PATH
89
RUN ln -s /usr/local/MATLAB/bin/matlab /usr/local/bin/matlab
910

1011
# Install MATLAB dependencies
@@ -93,7 +94,7 @@ RUN mkdir -p ${NOVNC_PATH} \
9394
| tar -zxf - -C ${NOVNC_PATH} --strip=1 \
9495
&& chown -R ${NB_USER}:users ${NOVNC_PATH}
9596

96-
# Change user to jovyan from root as we do not want any changes to be made as root in the container
97+
# Switch back to notebook user
9798
USER $NB_USER
9899

99100
# Get websockify

matlab-vnc/MATLAB_mounted.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ docker build -f Dockerfile.mounted -t matlab-vnc-notebook-nomatlab .
1212

1313
### Start the Docker Container
1414

15-
Execute the command below to start a Docker container, bind mount the directory `/usr/local/MATLAB/R2020a` (which must contain a MATLAB R2020b or later 64 bit Linux installation) into the directory `/usr/local/MATLAB` inside the container, and bind port 8888 on the host to port 8888 of the container (by default, Jupyter's app-server runs on port 8888 inside the container):
15+
Execute the command below to start a Docker container, bind mount the root directory of your local install of MATLAB `/usr/local/MATLAB/R2020b` into the directory `/usr/local/MATLAB` inside the container, and bind port 8888 on the host to port 8888 of the container (by default, Jupyter's app-server runs on port 8888 inside the container):
1616

1717
```bash
18-
docker run -it -v /usr/local/MATLAB/R2020a:/usr/local/MATLAB:ro -p 8888:8888 matlab-vnc-notebook-nomatlab
18+
docker run -it -v /usr/local/MATLAB/R2020b:/usr/local/MATLAB:ro -p 8888:8888 matlab-vnc-notebook-nomatlab
1919
```
2020

2121
Access the Jupyter Notebook by following one of the URLs displayed in the output of the ```docker run``` command.

matlab-vnc/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,28 @@ The `Dockerfile` in this repository builds upon the base image `jupyter/base-not
1919

2020
To build the Docker image, follow these steps:
2121

22-
1. Select a base container image with a MATLAB R2020b or later (64 bit Linux) installation. If you need to create one, follow the steps at [Create a MATLAB Container Image](https://github.com/mathworks-ref-arch/matlab-dockerfile).
22+
1. Select a base container image with a MATLAB (64 bit Linux) installation. If you need to create one, follow the steps at [Create a MATLAB Container Image](https://github.com/mathworks-ref-arch/matlab-dockerfile).
2323

2424
2. Open the `Dockerfile`, replace the name `matlab` with the name of the MATLAB image from step 1:
2525

2626
```
2727
FROM matlab AS matlab-install-stage
2828
```
2929

30-
3. If MATLAB is not installed at the location `/usr/local/MATLAB` in the image built in step 1, edit the following line to point to you MATLAB installation:
30+
3. MATLAB must be on the system path in the image built in step 1.
31+
If not, then edit the following line to point to your MATLAB installation:
3132

3233
```
33-
COPY --from=matlab-install-stage /usr/local/MATLAB /usr/local/MATLAB
34+
ARG BASE_ML_INSTALL_LOC=/tmp/matlab-install-location
3435
```
3536

36-
For example, if MATLAB is installed at the location `/opt/matlab/R2020a` change the line above to:
37+
For example, if MATLAB is installed at the location `/opt/matlab/R2020b` change the line above to:
3738

3839
```
39-
COPY --from=matlab-install-stage /opt/matlab/R2020a /usr/local/MATLAB
40+
ARG BASE_ML_INSTALL_LOC=/opt/matlab/R2020b
4041
```
4142

42-
4. Optionally, to preconfigure a network license manager, open the `Dockerfile`, uncomment the line below, and replace `port@hostname` with the licence server address:
43+
4. (Optional) To preconfigure a network license manager, open the `Dockerfile`, uncomment the line below, and replace `port@hostname` with the licence server address:
4344

4445
```
4546
# ENV MLM_LICENSE_FILE port@hostname

matlab/Dockerfile

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
# Copyright 2020 The MathWorks, Inc.
22

3+
# Argument shared across multi-stage build to hold location of installed MATLAB
4+
ARG BASE_ML_INSTALL_LOC=/tmp/matlab-install-location
5+
36
# Replace "matlab" with the Docker image that contains MATLAB
4-
# MATLAB should be installed at /usr/local/MATLAB or other changes will need
5-
# to be made to this Dockerfile
7+
# MATLAB should be available on the path in the Docker image
68
FROM matlab AS matlab-install-stage
9+
ARG BASE_ML_INSTALL_LOC
10+
11+
# Run code to locate a MATLAB install in the base image and softlink
12+
# to BASE_ML_INSTALL_LOC for a latter stage to copy
13+
RUN export ML_INSTALL_LOC=$(which matlab) \
14+
&& if [ ! -z "$ML_INSTALL_LOC" ]; then \
15+
ML_INSTALL_LOC=$(dirname $(dirname $(readlink -f ${ML_INSTALL_LOC}))); \
16+
echo "soft linking: " $ML_INSTALL_LOC " to" ${BASE_ML_INSTALL_LOC}; \
17+
ln -s ${ML_INSTALL_LOC} ${BASE_ML_INSTALL_LOC}; \
18+
elif [ $BASE_ML_INSTALL_LOC = '/tmp/matlab-install-location' ]; then \
19+
echo "MATLAB was not found in your image."; exit 1; \
20+
else \
21+
echo "Proceeding with user provided path to MATLAB installation: ${BASE_ML_INSTALL_LOC}"; \
22+
fi
723

824
FROM jupyter/base-notebook
25+
ARG BASE_ML_INSTALL_LOC
926

1027
# Switch to root user
1128
USER root
1229

30+
# Copy MATLAB install from supplied Docker image
31+
COPY --from=matlab-install-stage ${BASE_ML_INSTALL_LOC} /usr/local/MATLAB
32+
33+
# Put MATLAB on the PATH
34+
RUN ln -s /usr/local/MATLAB/bin/matlab /usr/local/bin/matlab
35+
1336
# Install MATLAB dependencies
1437
# Reference: https://github.com/mathworks-ref-arch/container-images/tree/master/matlab-deps
1538
RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install --no-install-recommends -y \
@@ -71,12 +94,6 @@ RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install -
7194
&& apt-get clean \
7295
&& rm -rf /var/lib/apt/lists/*
7396

74-
# Put MATLAB on the PATH
75-
RUN ln -s /usr/local/MATLAB/bin/matlab /usr/local/bin/matlab
76-
77-
# Copy MATLAB install from supplied Docker image
78-
COPY --from=matlab-install-stage /usr/local/MATLAB /usr/local/MATLAB
79-
8097
# Switch back to notebook user
8198
USER $NB_USER
8299

matlab/Dockerfile.mounted

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ FROM jupyter/base-notebook
55
# Switch to root user
66
USER root
77

8+
# Put MATLAB on the PATH
9+
RUN ln -s /usr/local/MATLAB/bin/matlab /usr/local/bin/matlab
10+
811
# Install MATLAB dependencies
912
# Reference: https://github.com/mathworks-ref-arch/container-images/tree/master/matlab-deps
1013
RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install --no-install-recommends -y \
@@ -66,9 +69,6 @@ RUN export DEBIAN_FRONTEND=noninteractive && apt-get update && apt-get install -
6669
&& apt-get clean \
6770
&& rm -rf /var/lib/apt/lists/*
6871

69-
# Put MATLAB on the PATH
70-
RUN ln -s /usr/local/MATLAB/bin/matlab /usr/local/bin/matlab
71-
7272
# Switch back to notebook user
7373
USER $NB_USER
7474

matlab/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@ To build the Docker image, follow these steps:
2727
FROM matlab AS matlab-install-stage
2828
```
2929

30-
3. If MATLAB is not installed at the location `/usr/local/MATLAB` in the image built in step 1, edit the following line to point to you MATLAB installation:
30+
3. MATLAB must be on the system path in the image built in step 1.
31+
If not, then edit the following line to point to your MATLAB installation:
3132

3233
```
33-
COPY --from=matlab-install-stage /usr/local/MATLAB /usr/local/MATLAB
34+
ARG BASE_ML_INSTALL_LOC=/tmp/matlab-install-location
3435
```
3536

3637
For example, if MATLAB is installed at the location `/opt/matlab/R2020b` change the line above to:
3738

3839
```
39-
COPY --from=matlab-install-stage /opt/matlab/R2020b /usr/local/MATLAB
40+
ARG BASE_ML_INSTALL_LOC=/opt/matlab/R2020b
4041
```
4142

42-
4. Optionally, to preconfigure a network license manager, open the `Dockerfile`, uncomment the line below, and replace `port@hostname` with the licence server address:
43+
4. (Optional) To preconfigure a network license manager, open the `Dockerfile`, uncomment the line below, and replace `port@hostname` with the licence server address:
4344

4445
```
4546
# ENV MLM_LICENSE_FILE port@hostname

0 commit comments

Comments
 (0)