Can I use MATLAB Engine API for Python in a Docker container with an individual license?

24 views (last 30 days)
I created a Docker container with a R2023b image and installed the MATLAB Engine API for Python. I can use my individual license to start a MATLAB session in the Docker container, but when I attempt to start a MATLAB session from Python I get the following error: "matlab.engine.EngineError: Unable to launch Simple server: Unable to launch /opt/matlab/R2023a/bin/matlab".
 
>>> import matlab.engine
>>> eng=matlab.engine.start_matlab()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-packages/matlab/engine/__init__.py", line 128, in start_matlab
eng = future.result()
File "/usr/local/lib/python3.8/dist-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/usr/local/lib/python3.8/dist-packages/matlab/engine/matlabfuture.py", line 87, in result
handle = pythonengine.getMATLAB(self._future)
matlab.engine.EngineError: Unable to launch Simple server: Unable to launch /opt/matlab/R2023a/bin/matlab
because: Timed out reading transport byte from transport
Can I use MATLAB Engine API for Python in a Docker container with my individual license?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Mar 2024
With an individual license you need to interactively log in to start a new MATLAB session in a Docker container. For this reason, you cannot start a new MATLAB session from Python using "matlab.engine.start_matlab()" with an individual license. In order to start a new MATLAB session from Python in a Docker container you need a license server, which allows you to start MATLAB non-interactively. However, with an individual license, you can interactively start a new MATLAB session and then connect to the existing MATLAB session from Python with the Engine API. Below is an example workflow for doing so.
 
1) Build and run a container with MATLAB and Python Engine installed (see Dockerfile example, below).
$ docker build -t <image-name> .
2) Start the Dockerfile and interactively log in to start the MATLAB session.
$ docker run -it --rm --shm-size=512M <image-name>
3) In MATLAB, run the following command to make the MATLAB instance a shared engine.
>> matlab.engine.shareEngine
3) Use "docker exec" from a terminal to start a bash shell in the same container.
$ docker exec -it <container-name> /bin/bash
4) Start Python in the bash shell and use "connect_matlab()" to connect to the existing MATLAB session.
>>> import matlab.engine
>>> eng = matlab.engine.connect_matlab()
>>> print(eng.sqrt(4.0))
2.0
Example Dockerfile
 
ARG MATLAB_RELEASE=R2023b
# Build MATLAB image
FROM mathworks/matlab:$MATLAB_RELEASE
# Declare global
ARG MATLAB_RELEASE
# Install MATLAB Engine API for Python
RUN /bin/sh -c 'cd /opt/matlab/$MATLAB_RELEASE/extern/engines/python && sudo python setup.py install'
For more information see:
https://www.mathworks.com/help/cloudcenter/ug/matlab-container-on-docker-hub.html
https://github.com/mathworks-ref-arch/matlab-dockerfile/blob/main/alternates/building-on-matlab-docker-image/Dockerfile

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!