Access command line of a Docker Slave running Matlab via Jenkins pipeline

I am using the Matlab Docker Image to run Matlab in a Container and do calculations within it.
https://github.com/mathworks-ref-arch/matlab-dockerfile

When you commonly start the container with docker run() it spins up a command line with running Matlab which is waiting for user Input.

My goal is to use the Docker Image as a build slave in Jenkins, so it spinns up the Container and kills it automatically after usage. The desired Matlab commands should be implemented in the Jenkins Job. So far it is all set up and Jenkins is starting Matlab in a Docker slave environment and then exits:

[Pipeline] sh
+ chmod +x startmatlab.sh [Pipeline] sh
+ ./startmatlab.sh MATLAB is selecting SOFTWARE OPENGL rendering.
                            < M A T L A B (R) >
                  Copyright 1984-2019 The MathWorks, Inc.
                  R2019b (9.7.0.1190202) 64-bit (glnxa64)
                              August 21, 2019

  To get started, type doc. For product information, visit www.mathworks.com.

[Pipeline] }  
[Pipeline] //  stage 
[Pipeline] }  
[Pipeline] //  node 
[Pipeline]  End of Pipeline 
[withMaven]  pipelineGraphPublisher - triggerDownstreamPipelines 
[withMaven]  downstreamPipelineTriggerRunListener - completed in 58 ms Finished: SUCCESS

The startmatlab.sh is the script provided by Mathworks to start matlab in the container and looks like this.

#!/bin/bash
#
# Copyright 2019 The MathWorks, Inc.
ECHO=echo

#=======================================================================
build_cmd () { # Takes the cmd input string and outputs the same
    # string correctly quoted to be evaluated again.
    #
    # Always returns a 0
    #
    # usage: build_cmd
    #

    # Use version of echo here that will preserve
    # backslashes within $cmd. - g490189

            $ECHO "$1" | awk '
#----------------------------------------------------------------------------
        BEGIN { squote = sprintf ("%c", 39)   # set single quote
                dquote = sprintf ("%c", 34)   # set double quote
              }
          NF != 0 { newarg=dquote             # initialize output string to
                                              # double quote
          lookquote=dquote                    # look for double quote
          oldarg = $0
          while ((i = index (oldarg, lookquote))) {
             newarg = newarg substr (oldarg, 1, i - 1) lookquote
             oldarg = substr (oldarg, i, length (oldarg) - i + 1)
             if (lookquote == dquote)
                lookquote = squote
             else
                lookquote = dquote
             newarg = newarg lookquote
          }
          printf " %s", newarg oldarg lookquote }
#----------------------------------------------------------------------------
        '
            return 0
}

ARGLIST=""

while [ $# -gt 0 ]; do
    case "$1" in
        -r|-batch)
            QUOTED_CMD=`build_cmd "$2"`
            ARGLIST="${ARGLIST} $1 `$ECHO ${QUOTED_CMD}`"
            shift
            ;;
        *)
            ARGLIST="${ARGLIST} $1"
    esac
    shift
done


eval exec "matlab ${ARGLIST}"


exit

In my Jenkins Job I defined a stage that is just executing that script and starts Matlab which is working fine:

stage('test2'){
    sh 'chmod +x startmatlab.sh'
    sh './startmatlab.sh'
    sh '1+1'
}

The only thing is that I am not able to get any commands from the pipeline in Jenkins to be executed within the containers command line. I tried to just do a simple ‘1+1’ and put it under the ‘./startmatlab.sh’ without success.

/home/jenkins/workspace/DR/[email protected]/durable-f5af635b/script.sh: 1: /home/jenkins/workspace/DR/[email protected]/durable-f5af635b/script.sh: 1+1: not found

So my question is if anyone can help me resolve how or where I have to put my commands to be executed here…

My Thanks in advance!

Source: Docker Questions