How to install both MATLAB MCR and the dotnet runtime in a Docker container?

I am trying to create a Docker container where I can use dotnet to run a C# program that loads a MATLAB Runtime (MCR) DLL to process some data. (.net core 3.1, MATLAB 2014b)

I have created an image based on the official dotnet image for Ubuntuthat installs the MATLAB’s MCR runtime in accordance with an example I’ve found online. Here is my Dockerfile:

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-focal 
ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get -qq update && apt-get -qq install -y 
    unzip 
    xorg 
    wget 
    curl &&    
    mkdir /mcr-install && 
    mkdir /opt/mcr && 
    cd /mcr-install && 
    wget http://ssd.mathworks.com/supportfiles/downloads/R2014b/deployment_files/R2014b/installers/glnxa64/MCR_R2014b_glnxa64_installer.zip && 
    unzip -q MCR_R2014b_glnxa64_installer.zip && 
    ./install -destinationFolder /opt/mcr -agreeToLicense yes -mode silent && 
    cd / && 
    rm -rf mcr-install

ENV LD_LIBRARY_PATH /opt/mcr/v84/runtime/glnxa64:/opt/mcr/v84/bin/glnxa64:/opt/mcr/v84/sys/os/glnxa64:/opt/mcr/v84/extern/bin/glnxa64

When I run the container interactively and try to run dotnet in it I get this error:

❯ docker run -it dotnet-mcr:0.2 /bin/bash
[email protected]:/# dotnet --info
Failed to load ���, error: /opt/mcr/v84/sys/os/glnxa64/libstdc++.so.6: version `GLIBCXX_3.4.18' not found (required by /usr/share/dotnet/host/fxr/3.1.10/libhostfxr.so)
The library libhostfxr.so was found, but loading it from /usr/share/dotnet/host/fxr/3.1.10/libhostfxr.so failed
  - Installing .NET Core prerequisites might help resolve this problem.
     https://go.microsoft.com/fwlink/?linkid=2063370

It seems that the last line in my Dockerfile – the one that sets LD_LIBRARY_PATH – is essential for the MCR runtime but disrupts the dotnet executable.

How can I work around this issue? (Please bear in mind that I need to run my MCR DLL in a C# program)

Source: Docker Questions