无法在 Docker 内运行适用于 Python 的 Matlab SDK 模块,但本地没有问题

问题描述 投票:0回答:1

我需要在 Python 中运行 Matlab 脚本。为了实现这一目标,我首先使用 this 文档安装了 Matlab Compiler Runtime (MCR)。然后,我使用 this 文档创建并安装 matlab 脚本的 python 模块。特别是,我使用 Matlab Compiler SDK 应用程序创建模块,然后使用

python setup.py install
安装创建的目录,
setup.py
位于生成的目录中,我可以使用以下代码在 python 中成功本地执行脚本:

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()

现在我想从 Docker 容器内部执行该模块。因此,我安装了 MCR 安装 zip 文件(来自此处的 Linux 64),将其解压缩并将其保存到目录

matlab_runtime
。在Dockerfile中,我复制了Matlab SDK模块的目录(
test_function
),复制了MCR安装目录(
matlab_runtime
)的目录,并尝试安装Matlab Runtime。以下是对 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

但是我收到此错误:

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

我尝试在 Dockerfile 中使用

make

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

Docker 不会返回任何错误,但是当我在 Docker 容器内运行上面的 python 脚本时,我收到错误:

AttributeError: module 'LC_filter_for_AI' has no attribute 'initialize'
,当我使用 dir(test_function) 检查模块的方法时,我得到

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

对于模块的本地安装,我得到了方法

['_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']
python docker matlab sdk matlab-compiler
1个回答
0
投票

我可以按如下方式解决该问题。我将我的

Dockerfile
更改为:

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

因此,我为 Matlab Runtime 使用了预构建的官方 MATLAB 容器映像 (containers.mathworks.com/matlab-runtime:r2023a)。修改

:
之后的标签以使用不同版本的 MCR。有关更多信息,请查看 Matlab 容器的文档。在我的例子中,Matlab 运行时安装到 /opt/matlabruntime/R2023a/。因此,我相应地放置了
LD_LIBRARY_PATH
以及执行所需的所有路径,并且我必须同意 matlab 运行时许可证才能执行它。

再次运行docker容器后,就没有出现上述问题了

© www.soinside.com 2019 - 2024. All rights reserved.