Unrecognized function or variable when using `str2func` in Docker container
Show older comments
Hello. Inside a Docker container, I am attempting to run .m matlab files that are remotely pulled from a git repository so that a remote code can be changed continously without needed redeployment of a containerized instance of Matlab Production Server. Since addpath is not supported by the Matlab runtime, I created the following function to the run the pulled fnuctions that cannot be added the the path:
function [varargout] = evaluate_function_out_of_path(function_name, function_file_path, options)
arguments
function_name string
function_file_path string
options.function_inputs cell = {}
end
function_handle = str2func(function_name);
current_directory = cd;
cd(function_file_path);
[varargout{1:nargout}] = function_handle(options.function_inputs{:});
cd(current_directory);
end
While I am able to run evaluate_function_out_of_path locally without issue on pulled code not added to the search path, I am receving the following error when ran on a deployed Docker container:
Unrecognized function or variable 'my_function'
The code that I am pulling is not compiled. What I am worried about is that the pulled source code must be compiled first before it can be used during runtime. Is this assumption right? Oddly enough, I am able to run built in functions with str2func inside the container without any kind of compilation such as:
str2func("@() disp('hello world')")
Let me know if you need clarification; I'll edit the question accordingly. Thanks in advance!
8 Comments
Bruno Luong
on 10 Apr 2024
Edited: Bruno Luong
on 10 Apr 2024
I don't think you can run anyting that is NOT compiled in the same session than he main function. All the mfile are crypted and signed and the Runtime will not consider any outsider, formost for security reason.
Bruno Luong
on 11 Apr 2024
Instead of
fh = str2fun(fname)
% ...
fh(arg{:});
You can try without str2fun and directly call
feval(fname, arg{:})
My bet is that the issue still exist and it will refuse to run.
I'm not sure that the Production server product is meant to be used as a general purpose execution runtime to run arbitrary code.
The MATLAB container on dockerhub might be better suited for this purpose.
See https://hub.docker.com/r/mathworks/matlab
Look for the -batch flag in the documentation on the page listed above.
Chandler
on 11 Apr 2024
Nicole Bonfatti
on 12 Apr 2024
Hi Chandler,
I'm the product manager for MATLAB Production Server and I'd be interested in hearing more about your use case for uncompiled code behind MATLAB Production Server, but I also wanted to address something I noticed in your OP:
You shouldn't need to redeploy your MATLAB Production Server container when you make changes to your deployed code. Provided you've packaged an updated deployable archive (CTF file) you should be able to map a volume into the container and replace the existing deployable archive in that volume without redeploying (or even restarting) the container itself.
Cheers,
Nicole
Answers (0)
Categories
Find more on Containers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!