1

I've a python script that executes a matlab script inside it with matlab.engine().This works on my machine but I'm not able to dockerize it.

The dockerfile will need two images -Matlab and Python. The Matlab needs to initialize with the license file and then the python setup script inside the Matlab folder is to be executed. After that, python script will be able to access Matlab.

Any help on creating the Dockerfile is appreciated. Or any leads to where can I find something like this. Thanks..

Tried with this:

Python code:

    import matlab.engine
    from flask import Flask
    
    @app.route("/matlab")
    def matlab_execute():
        eng = matlab.engine.start_matlab()
        eng.myscript(nargout=0)
        return "success"
    if __name__ == "__main__":        
        app.run()                     

Dockerfile:

    FROM mathworks/matlab:r2021a as matlab_image
    WORKDIR /matlab
    FROM python:3.8
    COPY --from=matlab_image /matlab /
    COPY myscript.m /
    COPY  py_script.py /
    WORKDIR /
    RUN pip install FLASK
    CMD [ "python", "./py_script.py" ]

But I get, "no module named matlab" when i run the container. How can I pass the matlab credentials to this ?

Sai
  • 45
  • 1
  • 7
  • Here on Stack Overflow it is expected that you present what you have tried already and ask a *specific* question based on that. – Klaus D. Aug 25 '21 at 05:36
  • Yes, thank you. Added that. – Sai Aug 25 '21 at 05:58
  • What's the reason for the two stages build? – Klaus D. Aug 25 '21 at 06:04
  • I need to have matlab engine running for the python script to execute matlab script inside it. So both the environments are needed- Python and Matlab. hence the two stages. – Sai Aug 25 '21 at 06:17
  • That's not how it works. Either you use the matlab container and install Python or you the Python container and install Matlab. First should be easier. – Klaus D. Aug 25 '21 at 06:41
  • It looks that you don't have matlab.engine module installed? Try to add `cd "matlabroot/extern/engines/python"` and `python setup.py install` into your Dockerfile. – 武状元 Woa Sep 14 '21 at 12:36

1 Answers1

0

The official MathWorks docker container Dockerfile is available on GitHub. Both a base image and dependencies image you can use are available on Dockerhub. Instructions on how to use the image can be found in both repositories.

A couple notes:

  1. The favored mechanism for passing in your license is to use a license server and pass in the environment variable via docker runtime flag, -eMLM_LICENSE_FILE=[PORT]@[LICENSE_SERVER_ADDRESS]. You can also pass in a file, but you need it to be using a license server since Docker containers are not able to give you a consistent flexlm hostid needed to activate a license.
  2. The new MPM tool which makes creating the MATLAB Docker container so much easier. In fact, it would be simple to modify the container to include the python you need. You could then either copy your code to the right place in the image for running it, in place of the entrypoint provided, or you can mount a volume with your code to actively develop it within that environment.
Matt
  • 161
  • 2
  • 12