Correct way of composing a Dockerfile which uses a Java application and the Matlab Runtime Compiler as a single Microservice

At the moment I have a Spring Boot/Java application that uses code that I successfully exported out of Matlab using the Library Compiler SDK. At the moment I am able to use the code locally but want to move this to a docker environment. I first tried to use a simple Dockerfile for the Java application to achieve this that looks like this:

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

But doing so results in an error when the Matlab function is called:

[dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.UnsatisfiedLinkError: Failed to find the required library libmwmclmcrrt.so.9.8 on java.library.path.

This library is typically installed along with MATLAB or the MATLAB Runtime. Its absence may indicate an issue with that installation or

the current path configuration, or a mismatch with the architecture of the Java interpreter on the path.

MATLAB Runtime version this component is attempting to use: 9.8.

Java interpreter architecture: glnxa64.

] with root cause


java.lang.UnsatisfiedLinkError: Failed to find the required library libmwmclmcrrt.so.9.8 on java.library.path.

This library is typically installed along with MATLAB or the MATLAB Runtime. Its absence may indicate an issue with that installation or

the current path configuration, or a mismatch with the architecture of the Java interpreter on the path.

MATLAB Runtime version this component is attempting to use: 9.8.

Java interpreter architecture: glnxa64.

It appears to use the Matlab function properly the Matlab Runtime is needed. Luckily I was able to find the following link that builds the runtimes in an image:
https://github.com/demartis/matlab_runtime_docker

Based on the link above I am able to make a Docker image for the Matlab Compiler Runtime:

FROM debian:stretch-slim
MAINTAINER Riccardo De Martis <[email protected]>
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get -q update && 
    apt-get install -q -y --no-install-recommends 
      xorg 
      unzip 
      wget 
      curl && 
    apt-get clean && 
    rm -rf /var/lib/apt/lists/*

# Download the MCR from MathWorks site an install with -mode silent
RUN mkdir /mcr-install && 
    mkdir /opt/mcr && 
    cd /mcr-install && 
    wget --no-check-certificate -q http://ssd.mathworks.com/supportfiles/downloads/R2020a/Release/0/deployment_files/installer/complete/glnxa64/MATLAB_Runtime_R2020a_glnxa64.zip && 
    unzip -q MATLAB_Runtime_R2020a_glnxa64.zip && 
    rm -f MATLAB_Runtime_R2020a_glnxa64.zip && 
    ./install -destinationFolder /opt/mcr -agreeToLicense yes -mode silent && 
    cd / && 
    rm -rf mcr-install

# Configure environment variables for MCR
ENV LD_LIBRARY_PATH /opt/mcr/v98/runtime/glnxa64:/opt/mcr/v98/bin/glnxa64:/opt/mcr/v98/sys/os/glnxa64:/opt/mcr/v98/extern/bin/glnxa64
                    
ENV XAPPLRESDIR /etc/X11/app-defaults

The problem I am facing right now is what the correct way is to compose a single Dockerfile that incorporates both Docker containers. I tried to make a single multi-stage build that looks like this but still it generates the error mentioned above:

FROM debian:stretch-slim
MAINTAINER Riccardo De Martis <[email protected]>
ENV DEBIAN_FRONTEND noninteractive

RUN apt-get -q update && 
    apt-get install -q -y --no-install-recommends 
      xorg 
      unzip 
      wget 
      curl && 
    apt-get clean && 
    rm -rf /var/lib/apt/lists/*

# Download the MCR from MathWorks site an install with -mode silent
RUN mkdir /mcr-install && 
    mkdir /opt/mcr && 
    cd /mcr-install && 
    wget --no-check-certificate -q http://ssd.mathworks.com/supportfiles/downloads/R2020a/Release/0/deployment_files/installer/complete/glnxa64/MATLAB_Runtime_R2020a_glnxa64.zip && 
    unzip -q MATLAB_Runtime_R2020a_glnxa64.zip && 
    rm -f MATLAB_Runtime_R2020a_glnxa64.zip && 
    ./install -destinationFolder /opt/mcr -agreeToLicense yes -mode silent && 
    cd / && 
    rm -rf mcr-install

# Configure environment variables for MCR
ENV LD_LIBRARY_PATH /opt/mcr/v98/runtime/glnxa64:/opt/mcr/v98/bin/glnxa64:/opt/mcr/v98/sys/os/glnxa64:/opt/mcr/v98/extern/bin/glnxa64
                    
ENV XAPPLRESDIR /etc/X11/app-defaults

At the moment I don’t have an idea what I am doing wrong and was wondering if this issue could be resolved easily or maybe I need to look into another way of deploying the Java application.

Source: Docker Questions