Search code examples
pythondockermatlabsdkmatlab-compiler

Cannot run Matlab SDK module for Python inside Docker, but no issues locally


I need to run a Matlab script inside Python. To achieve that, first I installed Matlab Compiler Runtime (MCR) using this documentation. Then, I used this documentation to create and install a python module of the matlab script. In particular, I used the Matlab Compiler SDK app to create the module, then installed the created directory using python setup.py install, setup.py is in the generated directory, and I could succesfully execute the script locally inside python using this code:

import test_function

mypkg = test_function.initialize()
arg1 = 1
arg2 = 2
out1, out2 = mypkg.test_function(arg1, arg2, nargout=2)
print(out1, out2)
mypkg.terminate()

Now I would like to execute the module from inside a Docker container. So, I installed the MCR installation zip file (Linux 64 from here), unzipped it and saved it to a directory matlab_runtime. In the Dockerfile, I copied the directory with the Matlab SDK module (test_function), copied the directory with MCR installation directory (matlab_runtime) and tried to install Matlab Runtime. Here are the changes to the Dockerfile:

FROM mathworks/matlab-runtime-deps:R2023b

# other not-related commands
# ...
# other not-related commands

RUN apt-get install -y python3.9 python3-pip
COPY test_function /app/test_function
COPY matlab_runtime /app/matlab_runtime
RUN ls /app/matlab_runtime
RUN pip install setuptools
WORKDIR /app
USER root
RUN chmod +x ./matlab_runtime/install
RUN python3 test_function/setup.py install
RUN sudo -H ./matlab_runtime/install

However I get this error: failed to solve: rpc error: code = Unknown desc = process "/bin/sh -c sudo -H ./matlab_runtime/install" did not complete successfully: exit code: 42.

I tried to use make in the Dockerfile:

RUN apt update && apt install -y make
RUN make ./matlab_runtime/install

Docker doesn't return any errors, but when I run a python script above inside the Docker container, I get the error: AttributeError: module 'LC_filter_for_AI' has no attribute 'initialize', when I check the module's methods using dir(test_function) I get

['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

while for the local installation of the module I get the methods

['_PathInitializer', '__builtins__', '__cached__', '__doc__', '__exit_packages', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_pir', 'atexit', 'glob', 'importlib', 'initialize', 'initialize_runtime', 'os', 'pdb', 'platform', 're', 'sys', 'terminate_runtime', 'weakref']

Solution

  • I could resolve the issue as follows. I changed my Dockerfile into this:

    FROM containers.mathworks.com/matlab-runtime:r2023a
    
    # other not-related commands
    # ...
    # other not-related commands
    
    RUN apt-get install -y python3.9 python3-pip
    COPY test_function /app/test_function
    RUN pip install setuptools
    WORKDIR /app/test_function
    USER root
    RUN python3 setup.py install
    ENV LD_LIBRARY_PATH=/opt/matlabruntime/R2023a/runtime/glnxa64:/opt/matlabruntime/R2023a/bin/glnxa64:/opt/matlabruntime/R2023a/sys/os/glnxa64:/opt/matlabruntime/R2023a/sys/opengl/lib/glnxa64:$LD_LIBRARY_PATH
    ENV AGREE_TO_MATLAB_RUNTIME_LICENSE=yes
    

    So, I used a prebuilt official MATLAB container image for Matlab Runtime (containers.mathworks.com/matlab-runtime:r2023a). Modify the tag after : to use a different version of MCR. For more information, check the documentation of the Matlab containers. Matlab runtime in my case was installed to /opt/matlabruntime/R2023a/. So I accordingly put LD_LIBRARY_PATH and all the required paths for the execution, and I had to agree to matlab runtime license for it to execute.

    After running the docker container again, I don't have the issue above.